POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Posted Candy?
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]相关的知识,希望对你有一定的参考价值。
Knights of the Round Table
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 12439 | Accepted: 4126 |
Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
- The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
- An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).
The input is terminated by a block with n = m = 0 .
The input is terminated by a block with n = m = 0 .
Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.
Sample Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0
Sample Output
2
Hint
Huge input file, ‘scanf‘ recommended to avoid TLE.
Source
白书
题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1。2.总人数为奇数。3.有仇恨的骑士不能挨着坐。问有几个骑士不能和任何人形成任何的圆圈。
求点双连通分量,然后每个分量二分图染色,如果不是二分图,则一定可以构造出奇环经过每个点
点双连通分量就是求割点,割点分开的每一块就是一个bcc,用一个栈存边(没有公共边,割点是公共点,每个bcc只有一个割点)
注意割点会被ok[]=1多次,所以不能在过程中纪录ans
还有染色后记得把col[割点]=0,以后还会用
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define C(x) memset(x,0,sizeof(x)) using namespace std; const int N=1005,M=1e6+5; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n=0,m,u,v,g[N][N]; struct edge{ int v,ne; }e[M<<1]; int h[N],cnt=0; inline void ins(int u,int v){ cnt++; e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt; } void buildGraph(){ memset(h,0,sizeof(h)); cnt=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(!g[i][j]) ins(i,j); } struct data{ int u,v; data(int a=0,int b=0):u(a),v(b){} }st[M<<1]; int top=0; int dfn[N],low[N],iscut[N],belong[N],dfc=0,bcc=0; int col[N],ok[N]; bool color(int u,int id){//printf("%d %d %d\n",u,col[u],id); for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(belong[v]!=id) continue; if(col[v]==col[u]) return 0;//first do this if(!col[v]){ col[v]=3-col[u]; if(!color(v,id)) return 0; } } return 1; } void dfs(int u,int fa){ dfn[u]=low[u]=++dfc; int child=0; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(!dfn[v]){ st[++top]=data(u,v); child++; dfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>=dfn[u]){ iscut[u]=1; bcc++; while(true){ int tu=st[top].u,tv=st[top--].v; if(belong[tu]!=bcc) belong[tu]=bcc; if(belong[tv]!=bcc) belong[tv]=bcc; if(tu==u&&tv==v) break; } col[u]=1; if(!color(u,bcc)) for(int i=1;i<=n;i++) if(belong[i]==bcc) ok[i]=1; col[u]=0;//for cut vertex } }else if(dfn[v]<dfn[u]&&v!=fa){ st[++top]=data(u,v);//notice!!! low[u]=min(low[u],dfn[v]); } } if(child==1&&fa==0) iscut[u]=0; } void BCC(){ dfc=bcc=0;top=0; C(dfn);C(low);C(iscut);C(belong); C(col);C(ok); for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i,0); } int main(){ while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){ memset(g,0,sizeof(g)); for(int i=1;i<=m;i++){u=read();v=read();g[u][v]=g[v][u]=1;} buildGraph(); BCC(); int ans=0; for(int i=1;i<=n;i++) if(!ok[i]) ans++; printf("%d\n",ans); } }
以上是关于POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]的主要内容,如果未能解决你的问题,请参考以下文章
poj2942 Knights of the Round Table
poj2942:Knights of the Round Table——题解
poj2942 Knights of the Round Table
POJ 2942Knights of the Round Table(二分图判定+双连通分量)