Warma_Lovely @ 2024-09-24 21:34:18
这里有一只正在打板的蒟蒻
#include<iostream>
#include<cmath>
#define maxn 999999
using namespace std;
int n, m,f[maxn];
int find(int a) {
if (f[a] == a) {
return a;
}
else {
f[a] = find(f[a]);
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
f[i] = i;
}
for(int i = 0; i < m; i++) {
int x, y, z;
cin >> z >> x >> y;
if (z == 1) {
f[find(x)] = find(y);
}
else {
if (find(x) == find(y) ) {
cout << "Y" << endl;
}
else {
cout << "N" << endl;
}
}
}
return 0;
}
by Just_int_mian @ 2024-09-24 21:36:29
@Warma_Lovely 题目中的点好像是从
沃玛可爱()
by Warma_Lovely @ 2024-09-24 21:38:30
@Just_int_mian 知道了,再改改
你也看沃玛
by Warma_Lovely @ 2024-09-24 21:42:47
@Just_int_mian 好像还是不太行,它是这样的
4 7
2 1 2
N
1 1 2
2 1 2
Y
1 3 4
2 1 4
N
1 2 3
2 1 4
Y
by imzfx_Square @ 2024-09-24 21:45:24
@Warma_Lovely 写 find
函数的时候路径压缩要 return f[a];
by imzfx_Square @ 2024-09-24 21:46:01
@Just_int_mian 我了个转生苹果
by Just_int_mian @ 2024-09-24 21:46:44
@Warma_Lovely 哦哦楼上说的对,我傻了。
建议你开一下Dev-C++的-Wall,这样可以有效避免函数没写返回值。
by Just_int_mian @ 2024-09-24 21:47:20
@imzfx_Square 这是什么同好聚集地吗(
by Warma_Lovely @ 2024-09-24 21:49:22
@Just_int_mian 可我用的是Visual Studio
by imzfx_Square @ 2024-09-24 21:49:22
@Just_int_mian 极好的,然而我不看 warma(
by imzfx_Square @ 2024-09-24 21:51:27
@Warma_Lovely vs 应该也可以配置编译命令的