新人求教???

P3367 【模板】并查集

Mosklia @ 2018-04-20 10:50:32

如下, 是本蒟蒻的代码

#include<iostream>
using namespace std;
const int maxn = 10000000 + 5;//这是数组的大小
int fa[maxn];
void Swap(int &a, int &b){
    int c = a;
    a = b;
    b = c;
}
inline int find(int num){
    if(fa[num] == num)return num;
    return fa[num] = find(fa[num]);
}
inline void merge(int a, int b){
    int t1 = find(a), t2 = find(b);
    if(t1 == t2) return;
    fa[t2] = t1;
}
int main(){
    int n, m;
    cin>> n>> m;
    for(int i = 1; i <= m; ++i) fa[i] = i;
    while(m--){
        int a, b, c;
        cin>> a>> b>> c;
        if(a == 2) cout<< (find(b) == find(c) ? 'Y' : 'N')<<endl;
        else {
            if(b > c) Swap(b, c);
            merge(b, c);
        }
    }
    return 0;
}

已知题目中说 x <= 10^4,那么为什么我把数组开到 10^6才行???在10^5时#2, #9,#10RE???


by Mosklia @ 2018-04-20 10:51:11

@panda_2134 help!!!


by Mosklia @ 2018-04-20 10:57:31

好吧, 自己写错了

for(int i = 1; i <= m; ++i) fa[i] = i;

写错了, 应该是

i <= n;

。。。。。。。。。。。。


by panda_2134 @ 2018-04-20 11:14:13

...


|