hdu1856(并查集的简单应用)
2014-04-26 14:08
281 查看
这算是我第一次自己ac并查集的题目,虽然题目不难,但也算是个突破吧
题意:求所有连通分量中节点数目的最大值,我用到了一个rank数组,用来记录父亲节点有几个儿子节点
代码如下:
题意:求所有连通分量中节点数目的最大值,我用到了一个rank数组,用来记录父亲节点有几个儿子节点
代码如下:
#include<iostream> #include<algorithm> #include<string> #include<stack> #include<queue> #include<set> #include<map> #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<time.h> #include<math.h> #define eps 1e-9 #define N 10000005 #define pi acos(-1.0) #define P system("pause") using namespace std; int f ,rank ; int find(int x) { if(f[x] == x) return x; //我自己记录了f[x] == x的节点的孩子节点个数 //int t = find(f[x]); // rank[f[x]] = rank[x] + rank[f[x]]; return f[x] = find(f[x]); } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int n; while(scanf("%d",&n) != EOF) { int i; for(i = 0; i <= N; i++){ f[i] = i; rank[i] = 1; } for(i = 0; i < n; i++) { int a,b; scanf("%d%d",&a,&b); int x,y; x = find(a); y = find(b); if(x != y) { f[x] = y; rank[y] = rank[x] + rank[y]; } // for(int j = 1; j <= 10; j++) // cout<<f[j]<<" ";cout<<endl; // for(int j = 1; j<= 10; j++) // cout<<rank[j]<<" ";cout<<endl; } int maxx = 0; for( i = 0; i <= N; i++) if(rank[i] > maxx) maxx = rank[i]; printf("%d\n",maxx); } // P; return 0; }
相关文章推荐
- UVALive 6091 并查集简单应用
- uva 10158 War(并查集的简单应用)
- poj 1611 The Suspects 初级->数据结构->简单并查集的应用
- L2-010. 排座位 (并查集简单应用)
- hdu 1213 How Many Tables(并查集的简单应用)
- POJ 2236 Wireless Network 【并查集的简单应用 判断是否在同一连通分量】
- 并查集最简单应用_不要抽烟
- 克鲁斯卡尔算法(kruskal)(并查集的简单应用)
- 九度1012 畅通工程---简单并查集应用
- RQNOJ36 数石子 并查集 简单应用
- HDU1272---(并查集)简单应用
- 畅通工程 HDU - 1232--并查集简单应用
- HDU - 1272 小希的迷宫之独木桥(并查集的简单应用)
- 并查集及其简单应用
- POJ 1988 Cube Stacking【并查集的简单应用 堆木块】
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- 并查集及其简单应用:优化kruskal算法
- hdoj 1272 小希的迷宫 又一个并查集的简单应用
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- 食物链(并查集的简单应用)