摘要

题目链接 难度等级 完成状态 完成分数 最后编辑时间 需要注意
家族 ★☆☆☆☆ 答案正确 100 2015-7-19 16:28:11


代码

1073.cpp代码已折叠
展开折叠内容
显示/移除行号
  1. #include<cstdio>
  2.  
  3.  
  4.  
  5. int f[5010]={};
  6.  
  7. //并查集
  8.  
  9. int findf(int x){
  10.  
  11. return f[x]==x?x:(f[x]=findf(f[x]));
  12.  
  13.  
  14.  
  15. }
  16.  
  17. int main(){
  18.  
  19. int n,m,p;
  20.  
  21. scanf("%d%d%d",&n,&m,&p);
  22.  
  23. for(int i=1;i<=n;++i)
  24.  
  25. {
  26.  
  27. f[i]=i;
  28.  
  29. }
  30.  
  31. for(int i=1;i<=m;++i)
  32.  
  33. {
  34.  
  35. int x,y;
  36.  
  37. scanf("%d%d",&x,&y);
  38.  
  39. x=findf(x);
  40.  
  41. y=findf(y);
  42.  
  43. f[x]=y;
  44.  
  45.  
  46.  
  47. }
  48.  
  49. for(int i=1;i<=p;++i)
  50.  
  51. {
  52.  
  53. int x,y;
  54.  
  55. scanf("%d%d",&x,&y);
  56.  
  57. x=findf(x);
  58.  
  59. y=findf(y);
  60.  
  61. puts(x==y?"Yes":"No");
  62.  
  63. }
  64.  
  65. }
  66.  

著作权声明[编辑]

关于[编辑]