HDU 3560 Graph’s Cycle Component 【并查集】
来源:互联网 发布:国外文献数据库 编辑:程序博客网 时间:2024/06/07 08:35
并查集分组,deg必需为2才是circle
#include<iostream>#include<cstdio>using namespace std;const int maxn=100000+10;int p[maxn],r[maxn],deg[maxn];bool is_circle[maxn];void init(int n){ memset(p,-1,n<<2); memset(r,0,n<<2); memset(deg,0,n<<2);}int find(int x){ int px=x,tmp; while(p[px]!=-1) px=p[px]; while(x!=px) { tmp=p[x]; p[x]=px; x=tmp; } return px;}void unio(int x,int y){ x=find(x),y=find(y); if(x==y) return; if(r[x]>r[y]) { p[y]=x;return; } else { p[x]=y;if(r[x]==r[y]) r[y]++; return; }}int main(){ int n,m; while(cin>>n>>m) { if(n==0&&m==0) return 0; init(n); int x,y; int a=0,b=0; for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); unio(x,y); deg[x]++,deg[y]++; } for(int i=0;i<n;i++) if(p[i]==-1) { a++; is_circle[i]=deg[i]==2; } for(int i=0;i<n;i++) find(i); for(int i=0;i<n;i++) if(p[i]!=-1&°[i]!=2) is_circle[p[i]]=false; for(int i=0;i<n;i++) if(p[i]==-1&&is_circle[i]==true) b++; cout<<a<<' '<<b<<endl; }}
- HDU 3560 Graph’s Cycle Component 【并查集】
- HDU 3560 并查集 Graph’s Cycle Component
- HDU 3560 Graph’s Cycle Component(并查集)
- hdu 3560 Graph’s Cycle Component 并查集
- HDU 3560--Graph’s Cycle Component【并查集(判环)】
- HDOJ 题目Graph’s Cycle Component(并查集)
- Graph’s Cycle Component (并查集)
- Graph’s Cycle Component(并查集优化)
- hdu3560Graph’s Cycle Component 并查集
- Graph’s Cycle Component
- HDU3560 Graph’s Cycle Component
- B. Mr. Kitayuta's Colorful Graph(并查集)
- Mr. Kitayuta's Colorful Graph(巧用并查集)
- HDU 5354 Bipartite Graph[cdq+并查集]
- 【hdu 5354】Bipartite Graph【分治 并查集】
- HDU 3276 Graph and Queries [离线+并查集+treap]
- HDU 5631 Rikka with Graph(并查集)
- HDU 5631 Rikka with Graph (并查集)
- dip、dp、sp、pt和px的区别
- linux gcc汇编的问题
- spring-mvc注解方式注入
- li中嵌套ul在ie6,7下的bug
- c++开发python扩展模块:cannot open file ‘python25_d.lib’解决办法
- HDU 3560 Graph’s Cycle Component 【并查集】
- 题目:一个5位数,判断它是不是回文数。
- Win32的基本概念和c++的重要性质
- 『每日一题 2012-02-13』整数划分问题
- UBUNTU下Apache2安装
- RelativeLayout常用属性介绍
- Android的xml文件中@、@android:type、@*、?、@+的含义和区别
- GD Star Rating的widget停留在 loading...或a WordPress rating system
- netbean 和ireport有什么不同么?