pat 1013 Battle Over Cities(25 分) (并查集)
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 by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing Knumbers, which represent the cities we concern.
Output Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input:
3 2 3
1 2
1 3
1 2 3
Sample Output:
1
0
0
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e6 + ; int n, m, k, pre[MAXN], t; struct node
{
int a, b;
}edge[MAXN]; void init()
{
for (int i = ; i <= n; ++ i)
pre[i] = i;
} int my_find(int x)
{
int n = x;
while (n != pre[n])
n = pre[n];
int i = x, j;
while (n != pre[i])
{
j = pre[i];
pre[i] = n;
i = j;
}
return n;
} int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i < m; ++ i)
scanf("%d%d", &edge[i].a, &edge[i].b);
for (int i = ; i < k; ++ i)
{
init();
scanf("%d", &t);
for (int i = ; i < m; ++ i)
{
if (edge[i].a == t || edge[i].b == t)
continue;
int n1 = my_find(edge[i].a), n2 = my_find(edge[i].b);
if (n1 != n2) pre[n1] = n2;
}
int ans = , temp = == t ? my_find() : my_find();
for (int i = ; i <= n; ++ i)
{
if (i == pre[i] && i != t)
++ ans;
} printf("%d\n", -- ans);
}
return ;
}
pat 1013 Battle Over Cities(25 分) (并查集)的更多相关文章
- 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 分)(图的遍历,统计强连通分量个数,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 ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
随机推荐
- 【Java必修课】ArrayList与HashSet的contains方法性能比较(JMH性能测试)
1 简介 在日常开发中,ArrayList和HashSet都是Java中很常用的集合类. ArrayList是List接口最常用的实现类: HashSet则是保存唯一元素Set的实现. 本文主要对两者 ...
- jmeter-使用代理服务器录制脚本
使用代理服务器录制脚本 1.测试计划-添加线程组 2.工作台添加HTTP代理服务器(路径:工作台-右键添加-非测试元件-HTTP代理服务器) 3.端口号一般由8888改为其他的 4.打开chrome的 ...
- Docker学习3-简单shell脚本安装mysql5.7与docker小技巧
前言 玩过Windows中的 .bat 的小伙伴是不是觉得很有意思呢,github中一键推送.同步拉取等等操作,哈哈,当然shell脚本也是很类似,可以运行一个脚本就可以自动给我们部署好环境啦!但是这 ...
- 玩转OneNET物联网平台之MQTT服务⑤ —— OneNet智能灯+MVP框架
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- redis之管道
Redis 的消息交互当我们使用客户端对 Redis 进行一次操作时,如下图所示,客户端将请求传送给服务器,服务器处理完毕后,再将响应回复给客户端.这要花费一个网络数据包来回的时间. 如果连续执行多条 ...
- Airflow速用
Airflow是Apache用python编写的,用到了 flask框架及相关插件,rabbitmq,celery等(windows不兼容):. 主要实现的功能 编写 定时任务,及任务间的编排: 提供 ...
- 关于到美国学习cs的亲身感受,希望对你们有所帮助
1.能否向各位寄托天下的朋友们简单介绍一下你自己?比如你国内的学校(或者什么档次),哪年申请出国的,什么专业,硕士还是博士,在美国的学校(或者什么档次),以及留学经历(毕业时间),现在状态(学生?博后 ...
- Hadoop 在 windows 7 64位的配置(二)|非cygwin
第一次使用需要 hdfs namenode -format 一键启动和关闭hadoop 新建文本文档 然后改名 start-hadoop.cmd 里面的内容 @echo off cd /d %HADO ...
- The command ("dfs.browser.action.delete") is undefined 解决Hadoop Eclipse插件报错
Hadoop Eclipse插件 报错. 使用 hadoop-eclipse-kepler-plugin-2.2.0.jar 如下所示 Error Log 强迫症看了 受不了 The command ...
- django-表单之创建表单(一)
1.在book app目录下新建一个forms.py,并加入 from django import forms class RegisterForms(forms.Form): # test=form ...