标签:
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu
Description
Input
Output
Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
问题分析:简单并查集的运用,维护一个size数组,在连接完所有的树后,查找0号学生所在树的根部,利用size数组即可得到答案;
#include "cstdio" int size [30000]; int person[30000]; void pbegin(int n) { for (int i=0;i<n;i++) { person[i] = i; size[i] = 1; } } int findroot (int u) { while (u != person[u]) { person[u] = person[person[u]]; u = person[u]; } return u; } void un(int x,int y) { int a,b; a = findroot (x); b = findroot (y); if (a == b) return; if (size[a] > size[b]) { person[b] = a; size[a] += size[b]; } else { person[a] = b; size[b]+= size[a]; } } int main() { int n,m; int t,x,y; while (scanf ("%d%d",&n,&m) && (n || m)) { pbegin(n); for (int i=0;i<m;i++) { if (m == 0) { printf ("1"); continue; } scanf ("%d%d",&t,&x); for (int j=1;j<t;j++) { scanf ("%d",&y); un(x,y); x=y; } } printf ("%d\n",size[findroot(0)]); } return 0; }
暑假集训(2)第二弹 ----- The Suspects(POJ1611)
标签:
原文地址:http://www.cnblogs.com/huas-zlw/p/5696962.html