Test instructions
There are n students, m groups, each student may belong to more than one group, now No. 0 has SARS;
And a group of No. 0 will get, and then a group of No. 0, his group of members also plenary, asked the final total of several people have SARS;
Ideas:
Just add a weight, that is, how many individuals in this group, such as R[i] indicates how many people are in the group where I reside;
So each time and time, but also to the number of the group and the past;
There are several people in the group where the last array 0 is located;
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int N = 30000 + 10;int P[n], R[n], N, m;void init (int x) {for (int i = 0; I <= x; i++) {r[i] = 1;p[i] = i;}} int find_p (int x) {return x = = P[x]? x:p[x] = Find_p (P[x]);} void Union (int x,int y) {int px = find_p (x); int py = find_p (y); if (px! = py) {P[PX] = py;r[py] + = r[px];}} int main () {int k,f;while (scanf ("%d%d", &n,&m) && (n + m)) {init (n); for (int i = 0; i < m; i++) {scanf ("% D ", &k); scanf ("%d ", &f); int tmp;for (int j = 0; J < K-1; J + +) {scanf ("%d ", &tmp); Union (f,tmp);}} int ans = find_p (0);p rintf ("%d\n", R[ans]);}}
poj1611 (with right and check set)