【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 ...
随机推荐
- Servlt入门
Servlt入门 java的两种体系结构 C/S (客户端/服务器)体系结构 通讯效率高且安全,但系统占用多 B/S (浏览器/服务器)体系结构 节约开发成本 C/S (客户端/服务器)体系结 ...
- 安卓基础(Navigation)
今天学习了简单的Navigation:页面导航. 页面导航的简单例子: MainAcitivity: package com.example.navigation; import android.su ...
- dockerfile的编写参数
注意细节 “#”号开头是注释 ,指令不区分大小写,顺序执行 FROM 指定基础镜像:注意必须是文件里第一个非注释行 ENV name 值 设置变量,注意没有=号 变量引用 ${name:-chenxi ...
- MySQLroot密码的恢复方法
MySQLroot密码的恢复方法 有可能你的系统没有 safe_MySQLd 程序(比如我现在用的 ubuntu操作系统, apt-get安装的MySQL) , 下面方法可以恢复 1.停止MySQLd ...
- sqlite3 install 和使用
windows: 在 Windows 上安装 SQLite 请访问 http://www.sqlite.org/download.html,从 Windows 区下载预编译的二进制文件. 您需要下载 ...
- 操作系统OS - 进程的状态切换
- Servlet的基本使用
1.pom.xml导入包 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax ...
- 走过的K8S坑
基本的docker命令: docker 镜像 打包成文件 sudo docker save -o 打包后的文件名 {镜像ID}或者{镜像标签} docker 改名: docker tag ff2816 ...
- ubuntu循环登录
ubuntu12.04管理员账户登录不了桌面,只能客人会话登录 登录管理员账户时,输入密码后,一直在登录界面循环 费了好大劲啊,一上午的时间,终于搞定了,哈哈哈 ctrl+alt+f1 ,切换到tty ...
- express session 实现登录
https://www.cnblogs.com/mingjiatang/p/7495321.html Express + Session 实现登录验证 1. 写在前面 当我们登录了一个网站,在没有 ...