【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 ...
随机推荐
- JS中数组实现(倒序遍历数组,数组连接字符串)
// =================== 求最大值===================================== <script> var arr = [10,35,765 ...
- SQL常用语句和函数
从一个表中选取数据插入到另一个表中: select column_name(s) into new_table_name from old_table_name --new_table_name表不必 ...
- ACM-ICPC实验室20.2.19测试-图论
B.Harborfan的新年拜访Ⅱ 就是一道tarjan缩点的裸题. 建图比较麻烦 以后遇到这种建图,先用循环把样例实现出来,再对着循环写建图公式 #include<bits/stdc++.h& ...
- oracle 12cR2 RAC deconfig CRS过程记录
已经停了该节点的crs所有服务: [root@jhjzksrv02 install]# ./rootcrs.sh -force -deconfig -verboseUsing configuratio ...
- 深入浅出Oracle:DBA入门、进阶与诊断案例 PDF 下载
网盘地址: 链接:https://pan.baidu.com/s/1tMFoNSUW7ICKOtmSQ5ZazA 提取码:dbnc
- React项目中遇到的那些坑
1.react中路由跳转后页面不置顶问题 问题: 从页面A跳转到页面B,页面A滚动到中间位置,跳转后页面B也会在中间位置 解决方法:在顶部组件的生命周期中进行判断,例如 componentWillRe ...
- DreamWeaver CC 中的回车
在Dreamweaver CC中换行有两种: 第一种是在设计视图中直接回车,对应的代码是<p>标签,即新生成一个段落. (注:在DreamWeaver CC编辑的代码中,按下回车相当于 ) ...
- 「NOIP2016」蚯蚓
传送门 Luogu 解题思路 很容易想到用一个堆去维护,但是复杂度是 \(O((n+m)\log(n+m))\) 的,显然过不了 \(7e6\). 其实这题有一个性质: 先被切开的蚯蚓,得到的两条新蚯 ...
- Java Web 笔记(杂)
Java Web 概述 什么是Java Web 在Sun的Java Servlet 规范中,对Java Web 应用做了这样的定义: "Java Web" 应用由一组Servlet ...
- cf854B Maxim Buys an Apartment
Maxim Buys an Apartment #include <iostream> #define int long long using namespace std; int n,k ...