【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 ...
随机推荐
- 计算机二级-C语言-程序填空题-190109记录-对二维字符串数组的处理
//给定程序,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符*补齐,使其与最长的字符串等长.ss所指字符串数组中共有M个字符串,且串长<N. //重难点 ...
- eclipse 自定义代码块设置(代码模板)
eclipse设置自定义代码模板 window -> show View -> other -> Templates 原来main模板 修改模板 再次插入
- BETWEEN
选取介于两个值之间的数据范围内的值.这些值可以是数值.文本或者日期. 选取 alexa 介于 1 和 20 之间的所有网站: SELECT * FROM table ; NOT BETWEEN 显示不 ...
- Cookie API介绍
一.Java提供的操作Cookie的API Java中的javax.servlet.http.Cookie类用于创建一个Cookie Cookie类的主要方法 No. 方法 类型 描述 Cookie( ...
- Mybatis空指针
查询数据,返回的字段要在 resultMap 中定义, <resultMap id="BaseResultMap" type="com...." > ...
- Jekyll+Github个人博客构建之路
请参考: http://robotkang.cc/2017/03/HowToCreateBlog/
- QWidget::setLayout: Attempting to set QLayout "" on xxx "", which already has a layout
QLayout是Qt应用开发中一个非常重要的组件,然而平时使用的时候不小心经常会发现控制台有类似如下的警告: QWidget::setLayout: Attempting to set QLayout ...
- 01 认识python
python介绍 python的创始⼈为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决⼼ 开发⼀个新的脚本解释程序,作为ABC语 ...
- IDEA 下的 github 创建提交与修改
本章假定你已经安装了 git 客户端,本文仅仅使用与 Mac 环境下,未在 Window下实验,但 IDEA 在 Window 和 Mac 下软件的使用方法是一致的. 1 配置账号 IDEA 需要配置 ...
- linux和windows系统的区别
在21世纪的今天,互联网可以说是当代发展最为迅速的行业,举个很简单的例子,现在的我们不论什么年龄阶层,几乎人手都有一部手机,上面的某博,某音,末手等软件,更是受到多数人的热爱,并且人们不仅仅用其来消遣 ...