POJ 2942 Knights of the Round Table
来源:互联网 发布:java线程池使用场景 编辑:程序博客网 时间:2024/05/16 06:14
缩块找奇环.....
Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 9128 Accepted: 2943
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 51 41 52 53 44 50 0
Sample Output
2
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
Central Europe 2005
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=1100;struct Edge{ int to,next;}edge[maxn*21];int Adj[maxn],Size=0;void init(){ Size=0; memset(Adj,-1,sizeof(Adj));}void Add_Edge(int u,int v){ edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;}bool A[maxn][maxn];int n,m;int DFN[maxn],Instack[maxn],Low[maxn],Belong[maxn],Color[maxn],ex[maxn];int Stack[maxn],tempStack[maxn];int Index,top,temptop,scc;bool is_odd_cycle(int u,int fa,int SCC,int cor){ Color[u]=cor; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(Belong[v]!=SCC||v==fa) continue; if(Color[v]==-1) { if(is_odd_cycle(v,u,SCC,1-cor)==true) return true; } else { if(Color[v]==Color[u]) return true; } } return false;}void Tarjan(int u,int fa){ DFN[u]=Low[u]=++Index; Instack[u]=true; Stack[top++]=u; int v; for(int i=Adj[u];~i;i=edge[i].next) { v=edge[i].to; if(!DFN[v]) { Tarjan(v,u); Low[u]=min(Low[u],Low[v]); if(Low[v]>=DFN[u]) { temptop=0; scc++; int tempoint=-999; do { tempoint=Stack[--top]; Belong[tempoint]=scc; Instack[tempoint]=false; tempStack[temptop++]=tempoint; }while(tempoint!=v); tempStack[temptop++]=u; memset(Color,-1,sizeof(Color)); if(temptop>=3&&is_odd_cycle(u,u,scc,1)) { for(int j=0;j<temptop;j++) { ex[tempStack[j]]=1; } } } } else { Low[u]=min(Low[u],DFN[v]); } }}void scc_solve(){ memset(DFN,0,sizeof(DFN)); memset(Instack,0,sizeof(Instack)); memset(Low,0,sizeof(Low)); memset(Belong,0,sizeof(Belong)); Index=top=scc=temptop=0; for(int i=1;i<=n;i++) if(!DFN[i]) Tarjan(i,i);}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(A,0,sizeof(A)); for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); A[a][b]=A[b][a]=true; } init(); for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(A[i][j]==true) continue; Add_Edge(i,j); Add_Edge(j,i); } } memset(ex,0,sizeof(ex)); scc_solve(); int ans=n; for(int i=1;i<=n;i++) { if(ex[i]) ans--; } printf("%d\n",ans); } return 0;}
1 0
- POJ 2942 Knights of the Round Table
- poj 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ 2942: Knights of the Round Table
- poj 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ-2942 Knights of the Round Table
- poj 2942 Knights of the Round Table
- poj 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ 2942 Knights of the Round Table
- POJ-2942-The Knights Of The Round Table
- POJ 2492 Knights of the Round Table
- Knights of the Round Table POJ
- poj 2942 Knights of the Round Table 点双连通
- poj 2942——Knights of the Round Table
- POJ 2942 Knights of the Round Table - from lanshui_Yang
- PAAS平台的web应用性能测试与分析
- Android 双卡双待识别
- 【规范代码】关于vector的去重及排序
- Ubuntu系统将TaintDroid刷到Android4.3模拟器中并测试其功能
- Action处理数据成json,供前台使用
- POJ 2942 Knights of the Round Table
- 基于linux的mini2440触摸屏驱动分析(yz版)
- The main differences between Unmount , Eject and Safely Remove Drive
- CentOS 6中yum本地源的配置
- 笔记23--android AsyncTask
- 第九周 项目1--定义一目运算符-和Complex中的<< >>的重载
- GNU Make学习总结(一)
- hdu2222 ac自动机。。。。
- 日媒:阿里巴巴上市融资或超Facebook