POJ-2942 Knights of the Round Table
来源:互联网 发布:空间谱估计理论与算法 编辑:程序博客网 时间:2024/05/17 08:57
题目:
http://poj.org/problem?id=2942题意:
骑士们要开会,有的骑士之间有矛盾,要求每次会议的人数是奇数并且做成一圈之后相邻的骑士之间不能有矛盾,问有多少骑士一次会议都无法参加而被开除?思路:
这两天一直在做这道题,前面做的双连通分量的题都是为了做这道,确实出的很好。
这道题首先想到的是所有骑士之间建立补图,就是将没有矛盾的骑士之间连线,表示他们可以坐到一起。
然后考虑把这些骑士连成一个奇环之后就是能够开会的人了,把所有能开会的人都标记之后剩下的就是会被开除的人数。
枚举所有可能的环一定会超时,就要考虑怎么求奇环。
首先明白什么是无向图的点的双连通分量及求法看这里:http://blog.csdn.net/kopyh/article/details/48321131
所以明显只要在一个点的双连通分量中有一个奇环那么这个双连通分量中的所有点都一定在某个奇环中,所以只要找到所有的双连通分量然后判奇环就行了。
判奇环很容易用二分图的交叉染色就行了。
至此AC,这道题真是耗费许多精力,,,,
代码:
#define N 1123int n,m;int flag,sum,ave,ans,res,len,ans1,ans2;int a[N],b[N],c[N],ff[N];int f[N][N];struct node{ int x,y; node(int a=0,int b=0) { x=a;y=b; }}tn;vector<int>g[N];stack<node>st;int dfn[N],low[N];int index;void init(){ for(int i=0;i<N;i++) g[i].clear(); while(!st.empty())st.pop();memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(f,0,sizeof(f));memset(c,0,sizeof(c));sum=0;index = 1;}bool dye(int u){ int v; for(int i=0;i<g[u].size();i++) { v = g[u][i]; if(!b[v]) continue; if(ff[v]==ff[u]) return false; if(ff[v]!=-1) continue; ff[v] = ff[u]^1; if(!dye(v)) return false; } return true;}void judge(int u,int v){ memset(b,0,sizeof(b)); res=0; int x,y; while(!st.empty()) { tn = st.top(); st.pop(); x=tn.x;y=tn.y; if(!b[y])a[res++]=y,b[y]=1; if(x==u) break; if(!b[x])a[res++]=x,b[x]=1; } memset(ff,-1,sizeof(ff)); ff[a[0]] = 0; if(!dye(a[0])) for(int i=0;i<res;i++) c[a[i]] = 1;}void tarjan(int x,int fa){low[x] = dfn[x] = index++;int len = g[x].size();for(int i=0;i<len;i++){ int t=g[x][i]; if(t==fa) continue;if(!dfn[t] && dfn[t]<dfn[x]){ st.push(node(x,t));tarjan(t,x);low[x] = min(low[x], low[t]);if(dfn[x]<=low[t]) judge(x,t);}else if(dfn[t] < dfn[x]) { st.push(node(x,t)); low[x] = min(low[x], dfn[t]); }}}void solve(){ for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i,i);}int main(){ int i,j,k,kk,t,x,y,z; while(scanf("%d%d",&n,&m)!=EOF&&n) { init(); for(i=0;i<m;i++) { scanf("%d%d",&x,&y); f[x][y]=f[y][x]=1; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i!=j && !f[i][j]) g[i].push_back(j); solve(); sum=0; for(i=1;i<=n;i++) if(!c[i]) sum++; printf("%d\n",sum); } 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 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
- 【hdoj 1164】Eddy's research I
- 非ARC情况下,设置使用僵尸对象,系统自动报错
- 30款高质量的HTML5和CSS3响应式模板
- POJ - 1258 Agri-Net(最小生成树简单题)
- ReactiveCocoa学习笔记
- POJ-2942 Knights of the Round Table
- nginx配置性能优化welly版
- js取url的参数
- svn 命令行下常用的几个命令
- 编译OpenWRT过程问题解决
- HDU 4763 Theme Section
- windows 无法安装到这个磁盘,选中的磁盘具有MBR分区表。在EFI系统上,windows只能安装在GPT磁盘上
- Centos下重要日志文件及查看方式
- IDEA编译smalidea源码