Union-Find Sets HDU 1856   2013-01-17


union-find sets hdu 1856 more is better

More is better

Mr Wang wants some boys to help him with a project. ...

Sample Input

4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8

Sample Output

4
2

Code

#include <iostream>  
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");

using namespace std;
const int N = 100005;
struct Node {
int par;
int sum;
};
int SUM;

Node p[2*N + 5];
void makeSet(int n) {
for(int i = 0; i <= 2*n; i++) {
p[i].par = i;
p[i].sum = 1;
}
SUM = 1;
}
int find(int a) {
if(a == p[a].par) return a;
return p[a].par = find(p[a].par);
}
void union1(int a, int b) {
int fa = find(a);
int fb = find(b);
if(fa != fb) {
p[fa].par = fb;
p[fb].sum += p[fa].sum;
}
if(p[fb].sum > SUM) {
SUM = p[fb].sum;
}
}
int main() {
int n, a, b;
while(scanf("%d", &n) == 1) {
makeSet(n);
while(n--) {
scanf("%d%d", &a, &b);
union1(a, b);
}
printf("%d\n", SUM);
}
return 0;
}

分享到:


  如果您觉得这篇文章对您的学习很有帮助, 请您也分享它, 让它能再次帮助到更多的需要学习的人. 您的支持将鼓励我继续创作 !
本文基于署名4.0国际许可协议发布,转载请保留本文署名和文章链接。 如您有任何授权方面的协商,请邮件联系我。