HDU-3749(点双连通分量-并查集)
来源:互联网 发布:人工智能权威杂志 编辑:程序博客网 时间:2024/05/01 12:47
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<stack>#define mm(x) memset(x,0,sizeof(x))using namespace std;const int N=5000+5;vector<int>G[N];vector<int>belong_col[N];int dfn[N],low[N],paint[N],num[N],index,col,node[N];int father[N];int n;struct edge{int f,t;};stack<edge>s;int MIN(int a,int b){if(a<b) return a;return b;}int find_set(int x){if(x!=father[x])father[x]=find_set(father[x]);return father[x];}int cal(){int num=0;for(int i=0;i<n;i++){if(node[i])num++;}return num;}void Init(int n){for(int i=0;i<=n;i++){belong_col[i].clear();G[i].clear();}for(int i=0;i<=n;i++)father[i]=i;while(!s.empty()) s.pop();mm(dfn);mm(low);mm(num);index=col=1;}void Tarjan(int u,int f){dfn[u]=low[u]=index++;for(int i=0;i<G[u].size();i++){int v=G[u][i];if(!dfn[v]){edge e;e.f=u;e.t=v;s.push(e);Tarjan(v,u);low[u]=MIN(low[u],low[v]);if(low[v]>=dfn[u]){mm(node);while(1){edge e=s.top();s.pop();node[e.f]++;node[e.t]++;belong_col[e.f].push_back(col);belong_col[e.t].push_back(col);if(e.f==u) break;}num[col]=cal();//一个点-双连通子图的点的个数col++;}}else if(v!=f&&dfn[u]>dfn[v]){edge e;e.f=u;e.t=v;low[u]=MIN(low[u],dfn[v]);}}}int main(){int m,q;int ca=1;while(~scanf("%d%d%d",&n,&m,&q),n+m+q){Init(n);while(m--){int a,b;scanf("%d%d",&a,&b);G[a].push_back(b);G[b].push_back(a);int ra=find_set(a);int rb=find_set(b);if(ra!=rb){father[ra]=rb;}}for(int i=0;i<n;i++){if(!dfn[i])Tarjan(i,i);}printf("Case %d:\n",ca++);while(q--){int a,b;scanf("%d%d",&a,&b);if(find_set(a)!=find_set(b)){printf("zero\n");continue;}int mark=0,falg=0;for(int i=0;i<belong_col[a].size();i++){for(int j=0;j<belong_col[b].size();j++){if(belong_col[a][i]==belong_col[b][j]){if(num[belong_col[a][i]]>2){printf("two or more\n");mark=1;break;}}}if(mark){falg=1;break;}}if(!falg) printf("one\n");}}return 0;}
0 0
- HDU-3749(点双连通分量-并查集)
- HDU 3749(点双连通分量-并查集)
- HDU-3749 Financial Crisis(并查集+点双连通分量)
- hdu 3749 点双连通分量
- poj3177 边双连通分量+缩点(并查集实现)
- [双连通分量 缩点 并查集] POJ 3177 Redundant Paths & 3352 Road Construction
- HDU3749 Financial Crisis(点双连通分量+并查集)
- poj3694 Network 双连通分量、并查集
- Redundant Paths-POJ3177(并查集+双连通分量)
- HDU 3749 Financial Crisis 点双连通分量
- HDU 3749 Financial Crisis(点-双连通分量)
- HDU 3749 Financial Crisis 点双连通分量
- HDU 3749 Financial Crisis(点-双连通分量)
- hdu 3749 Financial Crisis(Tarjan,点双连通分量)
- HDU 3749 Financial Crisis (点双连通分量)
- HDU 3394 Railway 点双连通分量
- HDU 3394 Railway 点双连通分量
- HDU 3394 Railway 点双连通分量
- final关键字
- How long does it take to make a context switch?
- 字符串比较器
- C#特性(Attribute)之预定义特性(Conditional)
- 编程之美初赛第二场 攻城略地(贪心,图论)
- HDU-3749(点双连通分量-并查集)
- 和lastTime相比是否已过去一个小时
- 正则表达式 学习笔记
- R语言学习-对象处理实用函数
- Leetcode23: Count and Say
- Performance Measurement on ARM
- Struts2教程10:国际化
- virtualBox ubuntu 文件共享
- Android 框架炼成 教你如何写组件间通信框架EventBus