【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意:
输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6)。有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公路全部不能使用,求增加多少条高速公路可以使剩下N-1个城市联通。(原本城市之间可能不联通,假设原本联通只能通过第0,4个数据)。
trick:
同一份代码交很多次,有几次会第4个点超时。(存疑)建议采用g++而不是clang++
AAAAAccepted code:
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int fa[];
int find_(int x){
if(fa[x]==x)
return x;
else
return fa[x]=find_(fa[x]);
}
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=;i<=m;++i)
cin>>a[i]>>b[i];
for(int i=;i<=k;++i){
int x;
cin>>x;
for(int j=;j<=n;++j)
fa[j]=j;
for(int j=;j<=m;++j){
if(a[j]==x||b[j]==x)
continue;
int t=find_(a[j]);
int tt=find_(b[j]);
if(t!=tt)
fa[t]=tt;
}
int cnt=-;
for(int j=;j<=n;++j){
if(j==x)
continue;
if(fa[j]==j)
cnt++;
}
cout<<cnt<<"\n";
}
return ;
}
【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)的更多相关文章
- 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 ...
- 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
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 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 ...
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013 Battle Over Cities (25 分)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- .net core 框架调用顺序
API -> AppSrv -> IRepository -> Repository ->
- 计算机二级-C语言-程序填空题-190107记录
//给定程序的功能是:调用fun函数建立班级通讯录.通讯中记录每位学生的编号,姓名和电话号码.班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块(代表要使用结构体)写到名为myfile5.d ...
- Python格式化字符串知多少
字符串格式化相当于字符串模板.也就是说,如果一个字符串有一部分是固定的,而另一部分是动态变化的,那么就可以将固定的部分做成模板,然后那些动态变化的部分使用字符串格式化操作符(%) 替换.如一句问候语: ...
- Django - 美化显示request.META
def index(request): values = request.META.items() html = [] for k, v in values: html.append('<tr& ...
- blg_统考,打印准考证 网页代码!
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>打印准考证</title ...
- Servlet映射细节
Servlet的映射细节: 1):一个Servlet程序(Web组件),可以配置多个<url-pattern>,表示一个Servlet有多个资源名称. <servlet-mappin ...
- 【IO多路复用】
" 目录 一.IO模型介绍 二.阻塞IO(blocking IO) 三.非阻塞IO(non-blocking IO) 四.多路复用IO(IO multiplexing) 五.异步IO(Asy ...
- window下jenkins+allure+邮箱发送
一.安装allure 1)下载 allure.zip 下载地址:allure-github: https://github.com/allure-framework/allure2 2)解压到本地目录 ...
- QAction菜单行为
/*File*/ QAction* close_action_; /*File*/ QAction* close_action_;
- java程序设计课期中考试——数据库的增删改查和简单的js界面
首先是设计思路,对于数据库的增删改查,我们借助Ecilipse来进行前端和后端的编写.Ecilipse是可以进行java web项目的操作的. 前端,我们选择用使用jsp,所谓的jsp就是可以嵌入其他 ...