救命!!!看了半个小时了,40MLE
查看原帖
救命!!!看了半个小时了,40MLE
476445
Saraphines楼主2023/7/25 11:08
#include<bits/stdc++.h>
using namespace std;
int pre[10010];
int find(int x) {
    if(pre[x] == x)return x;
    return pre[x] = find(pre[x]);
}
void join(int x, int y) {
    int fx = pre[x];
    int fy = pre[y];
    if(fx != fy){
        pre[fy] = fx;
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        pre[i] = i;
    }
    for (int i = 0; i < m; i++) {
        int op, a, b;
        cin >> op >> a >> b;
        if(op == 1){
            join(a, b);
        }
        else{
            if(find(a) == find(b)){
                cout<<"Y"<<endl;
                }
                else{
                    cout<<"N" <<endl;
                }
            }
    }
}
2023/7/25 11:08
加载中...