并查集判断连通性。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #include<map>
  7. using namespace std;
  8.  
  9. const int maxn=;
  10. struct Edge
  11. {
  12. int u,v;
  13. }e[maxn*maxn];
  14. int n,m,k;
  15. int f[maxn];
  16.  
  17. int Find(int x)
  18. {
  19. if(x!=f[x]) return f[x]=Find(f[x]);
  20. return f[x];
  21. }
  22.  
  23. int main()
  24. {
  25. scanf("%d%d%d",&n,&m,&k);
  26. for(int i=;i<=m;i++)
  27. scanf("%d%d",&e[i].u,&e[i].v);
  28.  
  29. for(int i=;i<=k;i++)
  30. {
  31. int id; scanf("%d",&id);
  32. int sz=n-;
  33. for(int j=;j<=n;j++) f[j]=j;
  34. for(int j=;j<=m;j++)
  35. {
  36. if(e[j].u==id) continue;
  37. if(e[j].v==id) continue;
  38. int fx=Find(e[j].u);
  39. int fy=Find(e[j].v);
  40. if(fx!=fy)
  41. {
  42. f[fx]=fy;
  43. sz--;
  44. }
  45. }
  46. printf("%d\n",sz-);
  47. }
  48. return ;
  49. }

PAT (Advanced Level) 1013. Battle Over Cities (25)的更多相关文章

  1. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  2. PTA (Advanced Level) 1013 Battle Over Cities

    Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...

  3. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  4. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  5. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  6. 【PAT Advanced Level】1013. Battle Over Cities (25)

    这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...

  7. PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  8. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  9. PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

    题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...

随机推荐

  1. nginx配置错误

    重启nginx:sudo /usr/local/nginx/sbin/nginx -s reload 出现错误提示:nginx: [emerg] unknown directive "if& ...

  2. 读苹果开发文档时遇到瓶颈,转而花2天看了Objc基本语法

    根据这篇博客中列出的文章开始看Objc基本语法: http://blog.hellolucky.info/articles/ios-beginner-ios-development/ 看完以后,明白多 ...

  3. Ansible4:Ad-hoc与命令执行模块【转】

    Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook.Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块. 如:command ...

  4. Shell脚本,自动化发布tomcat项目【转】

    Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...

  5. bmp文件格式详细解析

    先区分几个概念:16色和16位色一样吗? 不一样! 颜色位数,即是用多少位字节表示的值,每一位可以表示0和1两值.通常图片的颜色深度,简称色深,就是用位数来表示的,所以,我通常会看到8位色,16位色, ...

  6. php mysql 实现消息队列

    最近遇到一个批量发送短信的需求,短信接口是第三方提供的.刚开始想到,获取到手机号之后,循环调用接口发送不就可以了吗? 但很快发现问题:当短信数量很大时,不仅耗时,而且成功率很低. 于是想到,用PHP和 ...

  7. DISUBSTR - Distinct Substrings

    DISUBSTR - Distinct Substrings no tags  Given a string, we need to find the total number of its dist ...

  8. UITabBarItem's appearance

    1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...

  9. Servlet与jsp间的传值问题

    Servlet与JSP 之间的传值有两种情况:JSP -> Servlet, Servlet -> JSP.通过对象 request和 session (不考虑 application)完 ...

  10. Android开发 R cannot be resolved to a variable问题的分析

    R文件是系统自动生成的,如果没出现的话,你的XML文件是不是有错误?是否之前修改过res文件夹下面.xml文件 R文件没有生成的情况有几种: 1.项目没有自动编译:这种时候只需要简单的编译一下工程就会 ...