PAT (Advanced Level) 1013. Battle Over Cities (25)
并查集判断连通性。
- #include<iostream>
- #include<cstring>
- #include<cmath>
- #include<algorithm>
- #include<cstdio>
- #include<map>
- using namespace std;
- const int maxn=;
- struct Edge
- {
- int u,v;
- }e[maxn*maxn];
- int n,m,k;
- int f[maxn];
- int Find(int x)
- {
- if(x!=f[x]) return f[x]=Find(f[x]);
- return f[x];
- }
- int main()
- {
- scanf("%d%d%d",&n,&m,&k);
- for(int i=;i<=m;i++)
- scanf("%d%d",&e[i].u,&e[i].v);
- for(int i=;i<=k;i++)
- {
- int id; scanf("%d",&id);
- int sz=n-;
- for(int j=;j<=n;j++) f[j]=j;
- for(int j=;j<=m;j++)
- {
- if(e[j].u==id) continue;
- if(e[j].v==id) continue;
- int fx=Find(e[j].u);
- int fy=Find(e[j].v);
- if(fx!=fy)
- {
- f[fx]=fy;
- sz--;
- }
- }
- printf("%d\n",sz-);
- }
- return ;
- }
PAT (Advanced Level) 1013. Battle Over Cities (25)的更多相关文章
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 【PAT Advanced Level】1013. Battle Over Cities (25)
这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...
- 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 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数
题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...
随机推荐
- nginx配置错误
重启nginx:sudo /usr/local/nginx/sbin/nginx -s reload 出现错误提示:nginx: [emerg] unknown directive "if& ...
- 读苹果开发文档时遇到瓶颈,转而花2天看了Objc基本语法
根据这篇博客中列出的文章开始看Objc基本语法: http://blog.hellolucky.info/articles/ios-beginner-ios-development/ 看完以后,明白多 ...
- Ansible4:Ad-hoc与命令执行模块【转】
Ad-Hoc 是指ansible下临时执行的一条命令,并且不需要保存的命令,对于复杂的命令会使用playbook.Ad-hoc的执行依赖于模块,ansible官方提供了大量的模块. 如:command ...
- Shell脚本,自动化发布tomcat项目【转】
Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...
- bmp文件格式详细解析
先区分几个概念:16色和16位色一样吗? 不一样! 颜色位数,即是用多少位字节表示的值,每一位可以表示0和1两值.通常图片的颜色深度,简称色深,就是用位数来表示的,所以,我通常会看到8位色,16位色, ...
- php mysql 实现消息队列
最近遇到一个批量发送短信的需求,短信接口是第三方提供的.刚开始想到,获取到手机号之后,循环调用接口发送不就可以了吗? 但很快发现问题:当短信数量很大时,不仅耗时,而且成功率很低. 于是想到,用PHP和 ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...
- UITabBarItem's appearance
1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...
- Servlet与jsp间的传值问题
Servlet与JSP 之间的传值有两种情况:JSP -> Servlet, Servlet -> JSP.通过对象 request和 session (不考虑 application)完 ...
- Android开发 R cannot be resolved to a variable问题的分析
R文件是系统自动生成的,如果没出现的话,你的XML文件是不是有错误?是否之前修改过res文件夹下面.xml文件 R文件没有生成的情况有几种: 1.项目没有自动编译:这种时候只需要简单的编译一下工程就会 ...