SDUT Ubiquitous Religions (query set + hash)
Ubiquitous Religions Time Limit: 1000 MS Memory limit: 65536 K Topic description There are so large different religions in the world today that it is difficult to keep track of them all. you are interested in finding out how many different religions students in your university believe in. you know that there are n students in your university (0 <n <= 50000 ). it is infeasible for you to ask every student their religious beliefs. furthermore, inclustudents are not comfortable expressing their beliefs. one way to avoid these problems is to ask m (0 <= m <= n (n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church ). from this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. you may assume that each student subscribes to at most one religion. enter The input consists of a number of instances. each case starts with a line specifying the integers n and m. the next m lines each consists of two integers I and j, specifying that students I and j believe in the same religion. the students are numbered 1 to n. the end of input is specified by a line in which n = m = 0. output For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in. sample Input
10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Sample output
Case 1: 1Case 2: 7
Prompt to n college students and then give the following m inquiries. Each time two students a and B represent a college students numbered a and B believe in the same religion, ask how many different religions are there at most. It is to ask how many different sets are there. And scan it again + hash
# Include
# Include
# Include
# Include
# Include using namespace std; int father [100010], num [100010]; void Make_set (int n) // initialize {for (int I = 1; I <= n; I ++) father [I] = I;} int Find (int x) // query {if (x! = Father [x]) father [x] = Find (father [x]); // return father [x];} void Union (int x, int y) // and {int fx = Find (x); int fy = Find (y); if (fx = fy) return; father [fy] = fx ;} int max (int x, int y) {return x> y? X: y;} int main () {int n, m, I, x, y, T = 1; while (~ Scanf ("% d", & n, & m) {if (! N &&! M) break; memset (num, 0, sizeof (num); Make_set (n); while (m --) {scanf ("% d", & x, & y); Union (x, y) ;}for (I = 1; I <= n; I ++) {int f = Find (I ); num [f] ++;} int cnt = 0; for (I = 1; I <= n; I ++) if (num [I]) cnt ++; printf ("Case % d: % d \ n", T ++, cnt) ;}return 0 ;}