poj 2942 Knights of the Round Table(边双连通分量)
来源:互联网 发布:csgo处理器优化 编辑:程序博客网 时间:2024/05/18 01:47
Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 10346 Accepted: 3403
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
题意:
一群武士 某些武士之间互相仇视 如果在一起会发生斗争事件
参加圆桌会议的条件:
1)圆桌上任意任意两个相邻的武士不能互相仇视
2)同一个圆桌上的武士数量必须是奇数
思路:
将能坐在一起的武士分为一组 将所有的武士分组 得到双连通分量
然后 判断双连通分量中是否存在奇圈 如果存在 则这一组武士都能参与回忆
奇圈的判断方法 用交叉染色判断是否是二分图 二分图中肯定没有奇圈
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1010#define MAXM 1000010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;void read(int &x){ char ch; x=0; while(ch=getchar(),ch!=' '&&ch!='\n') { x=x*10+ch-'0'; }}int n,m,tol,cnt,top;int head[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],col[MAXN],mark[MAXN],stack[MAXN],odd[MAXN];int mp[MAXN][MAXN];struct Edge{ int from,to,next,vis;}edge[2*MAXM];void addedge(int u,int v){ edge[tol].to=v; edge[tol].from=u; edge[tol].next=head[u]; edge[tol].vis=0; head[u]=tol++; edge[tol].to=u; edge[tol].from=v; edge[tol].next=head[v]; edge[tol].vis=0; head[v]=tol++;}int find(int u){ for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(mark[v]) { if(col[v]==-1) { col[v]=!col[u]; return find(v); } else if(col[v]==col[u]) return 1; } } return 0;}void color(int u){ MEM(mark,0); int i; do { i=stack[top--]; mark[edge[i].from]=1; mark[edge[i].to]=1; }while(edge[i].from!=u); MEM(col,-1); col[u]=0; if(find(u)) { for(int j=1;j<=n;j++) { if(mark[j]) odd[j]=1; } }}void dfs(int u){ dfn[u]=low[u]=++cnt; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].vis) continue; edge[i].vis=edge[i^1].vis=1; stack[++top]=i; if(!dfn[v]) { dfs(v); low[u]=min(low[u],low[v]); if(low[v]>=dfn[u]) color(u); } else { low[u]=min(low[u],dfn[v]); } }}int main(){// fread; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; MEM(mp,0); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); mp[u][v]=mp[v][u]=1; } tol=0; MEM(head,-1); for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { if(mp[i][j]==0) addedge(i,j); } } MEM(dfn,0); MEM(odd,0); cnt=0; top=0; for(int i=1;i<=n;i++) { if(!dfn[i]) dfs(i); } int ans=0; for(int i=1;i<=n;i++) { if(!odd[i]) ans++; } printf("%d\n",ans); } return 0;}
0 0
- poj 2942 Knights of the Round Table 双连通分量
- POJ 2942 Knights of The Round Table <双连通分量>
- poj 2942 Knights of the Round Table(边双连通分量)
- 【双连通分量】 POJ Knights of the Round Table
- poj 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(双连通分量+tarjan+二分图判定)
- 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 补图+点双连通分量+判定二分图
- 内存卡数据丢失删除了该如何恢复
- 《从0到1》读书笔记第11章“顾客不会自动上门”: 当幸福来敲门(完)
- SSH HTTPS 公钥、秘钥、对称加密、非对称加密、 总结理解
- 给定一个file, 查找出里面出现频率最高的10个单词
- 再按一次退出程序
- poj 2942 Knights of the Round Table(边双连通分量)
- udp多人聊天室 1-server n-client
- WCF 返回对象
- Spring3核心技术之事务管理机制
- Win8+VS2010(32位)下编译x265
- Android OpenGL es 纹理坐标设定与贴图规则
- UITableView右滑删除
- java第八章集合中Properties的用法
- hdoj 2682 Tree