hdu 1856 More is better(数据结构:并查集)

来源:互联网 发布:unity3d游戏开发流程 编辑:程序博客网 时间:2024/06/18 02:59

Memory Limit: 327680/102400 K (Java/Others)

注意题头标明的内存...

看到别人开10,000,000大的数组直接吓尿了

看到评论里面的人说数据太水100,000的数组就够了

不知道这个坑爹的题目把数据搞那么大干什么

代码如下:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAXN 100100using namespace std;int cnt[MAXN];int p[MAXN];int find(int x) {    return p[x] == -1 ? x : p[x] = find(p[x]);}int main(void) {    int n, i, a, b, max_ans;    while(cin >> n) {        memset(p, -1, sizeof(p));        for(i=1; i<=MAXN; ++i)            cnt[i] = 1;        while(n--) {            cin >> a >> b;            a = find(a);            b = find(b);            if(a != b) {                p[b] = a;                cnt[a] += cnt[b];            }        }        max_ans = 0;        for(i=1; i<=MAXN; ++i) {            if(p[i] == -1)                max_ans = max(max_ans, cnt[i]);        }        cout << max_ans << endl;    }    return 0;}


0 0
原创粉丝点击