Tag: and query set
More is better
Time Limit: 5000/1000 MS (Java/others) memory limit: 327680/102400 K (Java/Others)
Total submission (s): 15097 accepted submission (s): 5561
Problem descriptionmr 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 shoshould be friends (direct or indirect), or there is only one boy left. given all the direct friend-pairs, you shoshould decide the best way.
Inputthe first line of the input contains an integer N (0 ≤ n ≤0 ≤ 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)
Outputthe output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
Sample Input
41 23 45 61 641 23 45 67 8
Sample output
42 Hint A and B are friends (direct or indirect), B and C are friends (direct or indirect), then A and C are also friends (indirect ). in the first sample {1, 2, 5, 6} is the result. in the second sample {1, 2}, {3, 4}, {5, 6}, {7, 8} are four kinds of answers.# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <limits. h> # include <ctype. h> # include <string. h> # include <string> # include <math. h> # include <algorithm> # include <iostream> # include <deque> # include <vector> # include <stack> # include <queue> # include <set> # include <map> using namespace STD; # define maxn 10000010int father [maxn]; int ans [maxn]; int A [100010], B [100010]; int max1 (int A, int B) {retu Rn A> B? A: B;} int find (INT X) {If (X! = Father [x]) {FATHER [x] = find (father [x]);} return father [X];} int main () {int N, I; while (~ Scanf ("% d", & N) {If (n = 0) {printf ("1 \ n"); continue;} for (I = 1; I <maxn; I ++) {ans [I] = 1 ;}for (I = 1; I <maxn; I ++) {FATHER [I] = I ;} for (I = 0; I <n; I ++) {scanf ("% d", & A [I], & B [I]);} int sum =-1; for (I = 0; I <n; I ++) {int F1 = find (A [I]); int F2 = find (B [I]); If (F1! = F2) {If (ANS [F1] <ans [F2]) {ans [F2] + = ans [F1]; father [F1] = F2; sum = max1 (sum, ANS [F2]);} else {ans [F1] + = ans [F2]; father [F2] = F1; sum = max1 (sum, ans [F1]) ;}} printf ("% d \ n", sum) ;}return 0 ;}
Check the HDU 1856