UVAlive3523 Knights of the Round Table(bcc)
来源:互联网 发布:如何编写安卓软件 编辑:程序博客网 时间:2024/06/10 20:22
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18122
【思路】
点-双连通分量
求出bcc,判断每个bcc是否为二分图,如果不是二分图则bcc中一定存在一个奇圈,则bcc中的任意一点一定位于一个奇圈上。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<stack> 4 #include<vector> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 2000+10; 9 10 struct Edge{ int u,v; 11 }; 12 13 int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt; 14 vector<int> G[maxn],bcc[maxn]; 15 16 stack<Edge> S; 17 18 int dfs(int u,int fa) { 19 int lowu=pre[u]=++dfs_clock; 20 int ch=0; 21 for(int i=0;i<G[u].size();i++) { 22 int v=G[u][i]; 23 Edge e=(Edge) {u,v}; 24 if(!pre[v]) { 25 S.push(e); 26 ch++; 27 int lowv=dfs(v,u); 28 lowu=min(lowu,lowv); 29 if(lowv>=pre[u]) { 30 iscut[u]=1; 31 bcc_cnt++; bcc[bcc_cnt].clear(); 32 for(;;) { 33 Edge x=S.top(); S.pop(); 34 if(bccno[x.u]!=bcc_cnt) bcc[bcc_cnt].push_back(x.u),bccno[x.u]=bcc_cnt; 35 if(bccno[x.v]!=bcc_cnt) bcc[bcc_cnt].push_back(x.v),bccno[x.v]=bcc_cnt; 36 if(x.u==u && x.v==v) break; 37 } 38 } 39 } 40 else if(pre[v]<pre[u] && v!=fa) { 41 S.push(e); lowu=min(lowu,pre[v]); 42 } 43 } 44 if(fa<0 && ch==1) iscut[u]=0; 45 return lowu; 46 } 47 void find_bcc(int n) { 48 memset(pre,0,sizeof(pre)); 49 memset(iscut,0,sizeof(iscut)); 50 memset(bccno,0,sizeof(bccno)); 51 dfs_clock=bcc_cnt=0; 52 for(int i=0;i<n;i++) 53 if(!pre[i]) dfs(i,-1); 54 } 55 56 int color[maxn],odd[maxn]; 57 bool judge(int u,int b) { 58 for(int i=0;i<G[u].size();i++) { 59 int v=G[u][i]; if(bccno[v]!=b) continue; 60 if(color[v]==color[u]) return false; 61 if(!color[v]) { 62 color[v]=3-color[u]; 63 if(!judge(v,b)) return false; 64 } 65 } 66 return true; 67 } 68 69 int n,m; 70 int A[maxn][maxn]; 71 72 void init() { 73 memset(A,0,sizeof(A)); 74 for(int i=0;i<n;i++) G[i].clear(); 75 } 76 77 int main() { 78 while(scanf("%d%d",&n,&m)==2 && n ) { 79 init(); 80 int u,v; 81 for(int i=0;i<m;i++) { 82 scanf("%d%d",&u,&v); 83 u--,v--; 84 A[u][v]=A[v][u]=1; 85 } 86 for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) 87 if(!A[i][j]) G[i].push_back(j),G[j].push_back(i); 88 find_bcc(n); 89 memset(odd,0,sizeof(odd)); 90 for(int i=1;i<=bcc_cnt;i++) { 91 memset(color,0,sizeof(color)); 92 for(int j=0;j<bcc[i].size();j++) bccno[bcc[i][j]]=i; 93 int u=bcc[i][0]; 94 color[u]=1; 95 if(!judge(u,i)) 96 for(int j=0;j<bcc[i].size();j++) odd[bcc[i][j]]=1; 97 } 98 int ans=n; 99 for(int i=0;i<n;i++) if(odd[i]) ans--;100 printf("%d\n",ans);101 }102 return 0;103 }
0 0
- UVAlive3523 Knights of the Round Table(bcc)
- UVALive3523-Knights of the Round Table(BCC+二分图判定)
- #UVALive3523#Knights of the Round Table(点双连通分量 + 二分图染色判奇环)
- UVALive 3523 Knights of the Round Table(BCC+二分图)
- UVALive3523 [Knights of the Round Table] tarjan求无向图双联通分量
- 【poj 2942 】 Knights of the Round Table 【tarjan求bcc+黑白染色判二分图】
- Knights of the Round Table
- Knights of the Round Table
- Knights of the Round Table
- poj 2942 Knights of the Round Table 【无向图求BCC + 黑白染色判断二分图】
- (挑战编程_13_3)The Knights Of The Round Table
- The Knights of the Round Table
- poj2942: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 2492 Knights of the Round Table
- UVA1364 Knights of the Round Table
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
- UVALive4513 Stammering Aliens(哈希法,后缀数组)
- bzoj1503 [NOI2004]郁闷的出纳员(名次树+懒惰标记)
- bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)
- bzoj2730 [HNOI2012]矿场搭建 (UVAlive5135 Mining Your Own Business)
- UVAlive3523 Knights of the Round Table(bcc)
- 隐式启动Activity
- UVAlive11324 The Largest Clique(scc+dp)
- IllegalStateException: Can not perform this action after onSaveInstanceState
- lightoj 1145 - Dice (I) 前缀和优化DP
- UVAlive4287 Proving Equivalences(scc)
- UVAlive3211 Now or later(2-SAT)
- UVAlive3713 Astronauts(2-SAT)
- bzoj2208 [Jsoi2010]连通数(scc+bitset)