安好en @ 2019-05-22 15:41:35
using namespace std; struct en { int x; int y; int z; }a[10001]; int father[10001],n,m; int findc(int x); int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;++i)father[i]=i; for(int i=1;i<=m;++i) { cin>>a[i].z>>a[i].x>>a[i].y; if(a[i].z==2) { int r1=findc(a[i].x); int r2=findc(a[i].y); if(r1==r2)cout<<"Y\n"; else cout<<"N\n"; } if(a[i].z==1)father[a[i].y]=a[i].x; } } int findc(int x) { if(x!=father[x])father[x]=findc(father[x]); return father[x]; }
by songyihang @ 2019-05-22 15:54:23
看题解吧·
by Strong_Jelly @ 2019-05-22 15:55:31
@安好en 请点击
by 安好en @ 2019-05-22 16:00:41
@shurongwang A了,O(∩_∩)O谢谢,嘿嘿
by 三生万物 @ 2019-05-22 16:01:43
直接把
father[a[i].y]=a[i].x;
改成
father[findc(a[i].y)]=findc(a[i].x);
会爆内存 这道题的x, y, z其实没有必要存,然后改一下就好了
by 安好en @ 2019-05-22 16:03:22
@三生万物 好生可爱
by 安好en @ 2019-05-22 16:03:37
手动吐舌
by 三生万物 @ 2019-05-22 16:07:04
我真的迷之尴尬QAQ
生平第一次回帖没认出楼主
/捂脸QAQ
by 安好en @ 2019-05-22 16:58:49
@神兵qqq1112 欧克欧克
by CJXJR @ 2019-06-07 12:54:07
#include<bits/stdc++.h>
using namespace std; struct en { int x; int y; int z; }a[10001]; int father[10001],n,m; int findc(int x); int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;++i)father[i]=i; for(int i=1;i<=m;++i) { cin>>a[i].z>>a[i].x>>a[i].y; if(a[i].z==2) { int r1=findc(a[i].x); int r2=findc(a[i].y); if(r1==r2)cout<<"Y\n"; else cout<<"N\n"; } if(a[i].z==1)father[a[i].y]=a[i].x; } } int findc(int x) { if(x!=father[x])father[x]=findc(father[x]); return father[x]; }