三个TLE求助

P3367 【模板】并查集

冬瓜皮 @ 2018-11-07 22:28:43

#include<iostream>
using namespace std;
int f[10005];
inline int find(int k){
    if(f[k]==k) return k;
    else return find(f[k]);
}
int main(void){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)   f[i]=i;
    int z,x,y;
    for(register int i=0;i<m;i++){
        cin>>z>>x>>y;
        if(z==1)    f[find(x)]=find(y);
        if(z==2){
            if(find(x)==find(y))
            cout<<'Y'<<endl;
            else    cout<<'N'<<endl;
        }
    }
    return 0;
}

by limi_sanhua @ 2018-11-07 22:32:10

@冬瓜皮 要加路径压缩


by 冬瓜皮 @ 2018-11-07 22:32:45

@礼弥酱 对照了下题解,懂了%%%


|