POJ 2942Knights of the Round Table(tarjan求点…
2018-06-17 21:09:02来源:未知 阅读 ()
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 13954 | Accepted: 4673 |
Description
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 is terminated by a block with n = m = 0 .
Output
Sample Input
5 5 1 4 1 5 2 5 3 4 4 5 0 0
Sample Output
2
Hint
Source
// luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> //#define getchar() (S == T && (T = (S = BB) + fread(BB, 1, 1 << 15, stdin), S == T) ? EOF : *S++) //char BB[1 << 15], *S = BB, *T = BB; using namespace std; const int MAXN=1e5+10; 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; } struct node { int u,v,nxt; }edge[MAXN]; int head[MAXN],num=1; inline void AddEdge(int x,int y) { edge[num].u=x; edge[num].v=y; edge[num].nxt=head[x]; head[x]=num++; } int N,M; int angry[1001][1001]; int dfn[MAXN],low[MAXN],tot=0,point[MAXN],color[MAXN],in[MAXN],ans[MAXN]; stack<int>s; void pre() { memset(angry,0,sizeof(angry)); num=1; memset(head,-1,sizeof(head)); memset(ans,0,sizeof(ans)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); } bool MakeColor(int now,int how) { color[now]=how; for(int i=head[now];i!=-1;i=edge[i].nxt) { if(!in[edge[i].v]) continue; if(!color[edge[i].v]&&!MakeColor(edge[i].v,how^1)) return 0; else if(color[edge[i].v]==color[now]) return 0; } return 1; } void tarjan(int now,int fa) { dfn[now]=low[now]=++tot; s.push(now); for(int i=head[now];i!=-1;i=edge[i].nxt) { if(!dfn[edge[i].v]&&edge[i].v!=fa) { tarjan(edge[i].v,now); low[now]=min(low[now],low[edge[i].v]); if(low[edge[i].v]>=dfn[now]) { memset(in,0,sizeof(in));//哪些在双联通分量里 memset(color,0,sizeof(color)); int h=0,cnt=0; do { h=s.top();s.pop(); in[h]=1; point[++cnt]=h; }while(h!=edge[i].v);//warning if(cnt<=1) continue;//必须构成环 in[now]=1;point[++cnt]=now; if(MakeColor(now,1)==0) for(int j=1;j<=cnt;j++) ans[point[j]]=1; } } if(edge[i].v!=fa) low[now]=min(low[now],dfn[edge[i].v]); } } int main() { #ifdef WIN32 freopen("a.in","r",stdin); #else #endif while(scanf("%d%d",&N,&M)) { if(N==0&&M==0) break; pre(); for(int i=1;i<=M;i++) { int x=read(),y=read(); angry[x][y]=angry[y][x]=1; } for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) if(i!=j&&(!angry[i][j])) AddEdge(i,j); for(int i=1;i<=N;i++) if(!dfn[i]) tarjan(i,0); int out=0; for(int i=1;i<=N;i++) if(!ans[i]) out++; printf("%d\n",out); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- POJ-3278 2020-04-01
- SGU 132 Another Chocolate Maniac 2020-03-06
- 协程的原理(Coroutine Theory) 2020-02-23
- 二叉堆(2)LeftistHeap 2020-02-19
- Asteroids!_poj2225 2020-02-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash