【tarjan求双连通分量+染色判二分图】POJ 2942
来源:互联网 发布:js调用图片保存的方法 编辑:程序博客网 时间:2024/06/05 05:26
这题debug的好郁闷啊!!!先是染色有问题,然后就是各种小错误....
判奇圈可以用二分图标准,因为奇圈肯定不是二分图,之前还要分离各双连通分量.......染色法判二分图一定要先对分量里每一个点都染一次色再枚举任意两个相邻的点看是否有同色,就是因为这里wa了n次!!!
#define N 1005vector<int> v[N];vector<int> lin[N];int dfn[N],low[N];bool instack[N],iscut[N];int num_rt;int cnt;int step;stack<int> sta;bool g[N][N];void init(int n){ for(int i=0;i<=n;i++){ instack[i] = 0; iscut[i] = 0; dfn[i] = 0; low[i] = 0; } step = 0; cnt = 0; while(!sta.empty())sta.pop();}void tarjan(int u,int fa){ low[u] = dfn[u] = ++step; sta.push(u); instack[u] = 1; int i; for(i=0;i<v[u].size();i++){ int to = v[u][i]; if(to==fa)continue; if(!dfn[to]){ tarjan(to,u); low[u] = min(low[u],low[to]); if(low[to]>=dfn[u]){ if(fa==-1)num_rt++; else iscut[u] = 1; while(1){ int tmp = sta.top(); sta.pop(); iscut[tmp] = 0; instack[tmp] = 0; lin[cnt].push_back(tmp); if(tmp==to)break; } lin[cnt].push_back(u); cnt++; } } else if(instack[to]) low[u] = min(low[u],dfn[to]); }}int col[N];bool dfs(int id,int u,int c){ int i; col[u] = c; for(i=0;i<lin[id].size();i++){ int to = lin[id][i]; if(u==to || col[to]!=-1)continue; if(g[u][to]){ dfs(id,to,c^1); } } return true;}bool ok[N];int solve(int n){ int i,j; for(i=1;i<=n;i++){ if(!dfn[i]){ num_rt = 0; tarjan(i,-1); if(num_rt>=2){ iscut[i] = 1; } } } int ans = 0; memset(ok,false,sizeof(ok)); bool tag = 0; for(i=0;i<cnt;i++){ for(j=0;j<=n;j++)col[j]=-1; int sz = lin[i].size(); dfs(i,lin[i][0],1); bool tag = 0; for(j=0;j<sz;j++){ for(int k=0;k<sz;k++){ int a = lin[i][j],b = lin[i][k]; if(a!=b && g[a][b] && col[a]==col[b]){ tag = 1; break; } } if(tag)break; } if(tag){ for(j=0;j<sz;j++){ ok[lin[i][j]] = 1; } } } for(i=1;i<=n;i++){ if(!ok[i])ans++; } return ans;}int main(){ int n,m; while(scanf("%d%d",&n,&m) &&(n+m)){ int i,j; memset(g,true,sizeof(g)); init(n); while(m--){ int x,y; scanf("%d%d",&x,&y); g[x][y] = g[y][x] = false; } for(i=0;i<=n;i++){ v[i].clear(); lin[i].clear(); } for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(g[i][j] && i!=j){ v[i].push_back(j); } } } printf("%d\n",solve(n)); } return 0;}
- 【tarjan求双连通分量+染色判二分图】POJ 2942
- poj 2942 双连通分量+二分图的染色判断
- poj 2942 点双连通分量+二分图染色
- POJ 2942 Knights of the Round Table (点-双连通分量 + 交叉法染色判二分图)
- poj 2942 双连通分量+染色判二部图(驱除骑士)
- POJ 2942 Knights of the Round Table [二分图染色][点双连通分量]
- [POJ]2942 圆桌骑士 点双连通分量 + 二分图染色
- poj 2942 Knights of the Round Table(双连通分量+tarjan+二分图判定)
- 【poj 2942 】 Knights of the Round Table 【tarjan求bcc+黑白染色判二分图】
- UVALIVE 3523(双连通分量+二分图染色)
- UVALIVE 3523 双连通分量+二分图染色
- poj2942 双连通分量+求奇圈二分染色
- poj 2942(奇圈,双连通分量,交叉染色)
- TARJAN的求双连通分量算法
- tarjan求点的双连通分量
- poj 3177 & 3352 【无向图双连通分量Tarjan】
- 双连通分量-tarjan
- 双连通分量-tarjan
- 深入Android【八】 —— Activity间数据传输
- CoreData使用须知
- latex 自己定制命令 \newcommand
- Kernel32--内核库函数提供的API
- 什么叫原子表和原子函数?
- 【tarjan求双连通分量+染色判二分图】POJ 2942
- Oracle 实例恢复时 前滚(roll forward) 后滚(roll back) 问题
- centos6升级到6.2
- 网络监控
- sql模糊查询
- Firefox11 Could not find a compatible GRE between version 1.9.9 and 1.9.9
- 有关刷router
- linux下C语言 undefined reference to stdscr wgetch wrefresh mvwprintw 解决
- poj 1920 Towers of Hanoi