讨论 / 25行短小代码
lijie201602 2017-09-05 22:01:24
点我顶贴 收藏 删除
#include<cstdio>

int n,m,p,f[5010];

int find(int x)

{

if(f[x]!=x) f[x]=find(f[x]);

return f[x];

}

int main()

{

scanf("%d %d %d",&n,&m,&p);

for(int i=1;i<=n;i++) f[i]=i;

for(int i=1;i<=m;i++)

{

int x,y,fx,fy; scanf("%d %d",&x,&y);

fx=find(x); fy=find(y);

if(fx!=fy) f[fx]=fy;

}

for(int i=1;i<=p;i++)

{

int x,y; scanf("%d %d",&x,&y);

if(find(x)==find(y)) printf("Yes\n");

else printf("No\n");

}

return 0;

}

#1 mike_unk@2020-01-08 22:58:47
回复 删除
#include <cstdio>

int n, m, a, b, q, f[5005];

int find(int u) {

if(f[u] == u) return u;

else return f[u] = find(f[u]);

}

void join(int x, int y) {

f[find(y)] = find(x);

return;

}

int main() {

scanf("%d%d%d", &n, &m, &q);

for(int i = 1; i <= n; i++)

f[i] = i;

for(int i = 1; i <= m; i++) {

scanf("%d%d", &a, &b);

join(a, b);

}

for(int i = 1; i <= q; i++) {

scanf("%d%d", &a, &b);

if(find(a) == find(b)) puts("Yes");

else puts("No");

}

return 0;

}

#2 bojuelina@2020-01-09 09:08:27
回复 删除
http://bible.com/events/5510832

http://bible.com/events/5510833

http://bible.com/events/5510834

http://bible.com/events/5510838

http://bible.com/events/5510840

http://bible.com/events/5510841

http://bible.com/events/5510842

http://bible.com/events/5510844

http://bible.com/events/5510845

http://bible.com/events/5510850

http://bible.com/events/5510852

http://bible.com/events/5510855

http://bible.com/events/5510856

http://bible.com/events/5510857

http://bible.com/events/5510859

http://bible.com/events/5510860

http://bible.com/events/5510861

http://bible.com/events/5510862

查看更多回复
提交回复