输出有问题,求改

P3367 【模板】并查集

Warma_Lovely @ 2024-09-24 21:34:18

这里有一只正在打板的蒟蒻

#include<iostream>
#include<cmath>
#define maxn 999999
using namespace std;
int n, m,f[maxn];
int find(int a) {
    if (f[a] == a) {
        return a;
    }
    else {
        f[a] = find(f[a]);
    }
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        f[i] = i;
    }
    for(int i = 0; i < m; i++) {
        int x, y, z;
        cin >> z >> x >> y;
        if (z == 1) {
            f[find(x)] = find(y);
        }
        else {
            if (find(x) == find(y) ) {
                cout << "Y" << endl;
            }
            else {
                cout << "N" << endl;
            }
        }
    }
    return 0;
}

by Warma_Lovely @ 2024-09-24 21:55:45

现在是这样的

#include<iostream>
#include<cmath>
#define maxn 99999
using namespace std;
int n, m,f[maxn];
int find(int a) {
    if (f[a] == a) {
        return f[a];
    }
    else {
        f[a] = find(f[a]);
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        f[i] = i;
    }
    for (int i = 1; i <= m; i++) {
        int x, y, z;
        cin >> z >> x >> y;
        if (z == 1) {
            f[find(x)] = find(y);
        }
        else {
            if (find(x) == find(y)) {
                cout << "Y" << endl;
            }
            else {
                cout << "N" << endl;
            }
        }
    }
    return 0;
}

然而还是不行,RE

气死我了


by Just_int_mian @ 2024-09-24 21:57:50

@Warma_Lovely 不对啊

else 里要写 return f[a] 啊...


by Just_int_mian @ 2024-09-24 21:59:05

@imzfx_Square 那你去看看 warma 吧。

真的超级~warma


by imzfx_Square @ 2024-09-24 22:05:51

@Just_int_mian 等有时间了再看吧,现在主要还是练。

我是一只因为很菜所以菜就多练的蒟蒻


by Warma_Lovely @ 2024-09-24 22:06:40

@Just_int_mian 我觉得睡一觉可能就会了

安详


上一页 |