Elaina_7 @ 2019-11-16 09:44:29
RT
我按给的范围开数组,不管是10000,10005还是10009都MLE
by Elaina_7 @ 2019-11-16 09:45:54
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
int n, m, ans;
int father[10005];
int find(int x)
{
if(x == father[x]) return x;
else return father[x] = find(father[x]);
}
void add(int x, int y)
{
father[find(x)] = y;
}
bool check(int x, int y)
{
return find(x) == find(y);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
father[i] = i;
for(int i = 1; i <= m; i ++)
{
int z, x, y;
scanf("%d%d%d", &z, &x, &y);
if(z == 1)
{
add(x, y);
}
else if(z == 2)
{
if(check(x, y))
{
printf("Y\n");
}
else printf("N\n");
}
}
return 0;
}
by 樱花飞舞 @ 2019-11-16 09:58:04
@saber_lover 没问题
by 紫陰花 @ 2019-11-16 09:58:32
@saber_lover 您的写法好像错了……
by 樱花飞舞 @ 2019-11-16 09:59:13
@saber_lover add()写错了,错误跟试机时一模一样
by 紫陰花 @ 2019-11-16 10:01:17
我刚刚还A了这道题,数据怎么会错
by _jimmywang_ @ 2019-11-16 10:03:59
father[find(x)] = y
是
father[find(x)] = find(y)
吧
by _jimmywang_ @ 2019-11-16 10:04:45
帮你测过了,改完就A了
by _jimmywang_ @ 2019-11-16 10:05:12
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
int n, m, ans;
int father[10005];
int find(int x)
{
if(x == father[x]) return x;
else return father[x] = find(father[x]);
}
void add(int x, int y)
{
father[find(x)] =find(y);//改这里
}
bool check(int x, int y)
{
return find(x) == find(y);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
father[i] = i;
for(int i = 1; i <= m; i ++)
{
int z, x, y;
scanf("%d%d%d", &z, &x, &y);
if(z == 1)
{
add(x, y);
}
else if(z == 2)
{
if(check(x, y))
{
printf("Y\n");
}
else printf("N\n");
}
}
return 0;
}
by Elaina_7 @ 2019-11-16 10:23:21
谢谢各位大佬,A了