T了三个点,如何优化?

P3367 【模板】并查集

geray_king @ 2019-03-03 21:19:48

T了2 9 10 三个点,怎么破

#include<bits/stdc++.h>
using namespace std;
int f[10010];
int n,m;
int find_father(int x)
{
    if(f[x]==x)return x;
    return find_father(f[x]);
}
/*void Union(int x,int y)
{
    int a1=find_father(x);
    int b=find_father(y);
    if(a1!=b)
    f[b]=f[a1];
}*/
int main()
{   cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
    }
    for(int i=0;i<m;i++)
    {   int a,s,d;
        cin>>a>>s>>d;
        if(a==1)
        {
        f[find_father(s)]=find_father(d);
        }
        if(a==2)
        {
            if(find_father(s)==find_father(d))
            printf("Y\n");
            else 
            printf("N\n");
        }
    }
    return 0;
 } 

by 两年打铁 @ 2019-03-03 21:20:59

路径压缩啊


by WA鸭鸭 @ 2019-03-03 21:21:29

@geray_king

int find_father(int x)
{
    if(f[x]==x)return x;
    return find_father(f[x]);
}

return find_father(f[x]);

改成

return f[x]=find_father(f[x]);

by geray_king @ 2019-03-03 21:24:19

@WA鸭鸭 A过了,感谢


by WA鸭鸭 @ 2019-03-03 21:26:47

@geray_king 不用谢qwq


by Catalan1906 @ 2019-03-03 21:33:24

@geray_king 改成按秩合并


by NaCly_Fish @ 2019-03-03 21:36:05

@Neptune_Disaster orz N_D AK IOI


by Catalan1906 @ 2019-03-03 21:37:27

@NaCly_Fish Orz N_F AK IOI

N_D can't AK IOI, she is juruo.


by 花里心爱 @ 2019-03-03 21:38:00

@NaCly_Fish @Neptune_Disaster 您们又在AKIOI了Orz


by Catalan1906 @ 2019-03-03 21:38:46

@Irressey @NaCly_Fish 您们又在AKIOI了Orz


by geray_king @ 2019-03-03 21:51:43

@Neptune_Disaster ??神仙对话?蒟蒻看不懂


| 下一页