Joe666 @ 2023-08-18 19:43:10
#include<iostream>
using namespace std;
int n, m, x, y, z, father[100001];
int find(int x){
if(father[x] == x) return x;
return father[x] = find(father[x]);
}
int union_set(int x, int y){
father[find(x)] = find(y);
}
int main(){
cin>>n>>m;
for(int i = 1;i <= n;i++) father[i] = i;
while(m--){
cin>>x>>y>>z;
if(x == 1) union_set(y, z);
else cout<<(find(y) == find(z) ? "Y\n":"N\n");
}
}
rt,不开O2就AC,一开O2就RE,这是为什么呀!求救......
by WYZ20030051 @ 2023-08-18 19:45:09
union_set没返回值
by Tokai__Teio @ 2023-08-18 19:45:48
@Joe666 是不是你的路径压缩有点问题?
by WYZ20030051 @ 2023-08-18 19:46:23
@Joe666 你应该写成void类型
by MiPloRAs_3316 @ 2023-08-18 19:47:45
@Joe666 O2 对于代码的正确性有更高的要求
by Joe666 @ 2023-08-18 19:47:46
@ikura 哪里有问题,求救......
by Joe666 @ 2023-08-18 19:49:18
@WYZ20030051 哦 已A,此贴接
by Joe666 @ 2023-08-18 19:49:47
结
by DELA @ 2023-08-18 20:18:55
@Joe666 提醒一句:有时候返回值类型写错开 O2 不一定是 RE,还有可能是 TLE MLE 等等莫名其妙的错误。
我以前数据结构题树状数组返回值类型写错导致一直全 TLE,以为是别的问题调了好久。。。。
by Joe666 @ 2023-08-18 21:03:39
@DELA 感谢提醒