More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 19370 Accepted Submission(s): 7133
Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex,
the more boys come, the better it will be. Of course there are certain requirements.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
Sample Input
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
Sample Output
4 2就是求并查集的秩形成其结构后用find统计个数#include<cstdio> #include<iostream> using namespace std; int pre[100005]; bool flag[100005]; int g[100005]={0}; int find(int x) { int r=x; while (r != pre[r]) { r = pre[r]; } int i=x,j; while (i != r) { j = pre[i]; pre[i] = r; i = j; } return r; } void mix(int x,int y) { int fx=find(x),fy=find(y); if (fx != fy) { pre[fx] = fy; } } int main() { int n=0; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=100000;i++) { pre[i]=i; g[i]=0; flag[i]=0; } int max=1; int MAX=-999; for(int i=1;i<=n;i++) { int x=0; int y=0; scanf("%d%d",&x,&y); mix(x,y); if(x>max) max=x; if(y>max) max=y; } for(int i=1;i<=max;i++) { g[find(i)]++; } for(int i=1;i<=max;i++) { if(g[i]>MAX) MAX=g[i]; } printf("%d\n",MAX); } return 0; }2别人的代码直接结构中统计/*我的思路大概是,先建立一个并查集数组,一个标记数组(主要为了防止超时),一个组员数量数组。首先先把并查集数组初始话为本身,并把组员数量初始化为1,之后就是按代码上理解下思路*/ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #define MAX 10000010 int tree[MAX],big[MAX];//并查集数组,标记数组,和老大手下多少人 bool mark[MAX]; int find(int a) { int x=a; while(tree[x]!=x) x=tree[x]; tree[a]=x;//这里很重要,没这个就超时 return x; } int father(int a,int b) { int i,j; i=find(a); j=find(b); if(i!=j) { tree[i]=j; big[j]+=big[i];//换老大的时候记得把对方的组员放到另一个老大那里 } return 0; }//函数不解释 int main() { int t,i,a,b,max; while(~scanf("%d",&t)) { if(t==0)//输入0返回1 { printf("1\n"); continue; } for(i=0;i<MAX;i++)//初始化 { tree[i]=i; big[i]=1; } while(t--) { scanf("%d%d",&a,&b); father(a,b); mark[a]=1; mark[b]=1; } max=0; for(i=0;i<MAX;i++) if(mark[i]&&i==tree[i]) { if(big[i]>max) max=big[i]; } printf("%d\n",max); memset(mark,0,sizeof(mark)); } }