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 ...
随机推荐
- [LUOGU2964] [USACO09NOV]硬币的游戏A Coin Game
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- [LUOGU1868] 饥饿的奶牛 - dp二分
题目描述 有一条奶牛冲出了围栏,来到了一处圣地(对于奶牛来说),上面用牛语写着一段文字. 现用汉语翻译为: 有N个区间,每个区间x,y表示提供的x~y共y-x+1堆优质牧草.你可以选择任意区间但不能有 ...
- Python开发【第十一篇】函数
函数 什么是函数? 函数是可以重复执行的语句块,可以重复调用并执行函数的面向过程编程的最小单位. 函数的作用: 函数用于封装语句块,提高代码的重用性,定义用户级别的函数.提高代码的可读性和易维护性. ...
- LWIP移植文件介绍
在介绍文件之前首先介绍一下DMA描述符 stm32以太网模块接收/发送FIFO和内存之间的以太网传输是通过以太网DMA使用DMA描述符完成的,一共有两个描述符列表:一个用于接收,一个用于发送, 两个列 ...
- 安装并使用SourceTree进行代码管理(Mac环境)
应用场景 对于我们开发人员来说,熟练使用Git是最基本的技能之一.SourceTree又是一款比较好的Git UI工具,是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,主要 ...
- Swoole 的微信扫码登录
微信应用的便捷,扫码登录方式越来越被现在的应用所使用.它因为不用去记住密码,只要有微信号即可方便快捷登录.微信的开放平台原生就有支持扫码登录的功能,不过大部分人还是在用公众平台,所以扫码登录只能自行实 ...
- Arduino学习笔记① 初识Arduino
1.前言 近段时间,博主陆续更新了ESP8266学习笔记,主要开发平台是Arduino.但是,对于很多无基础的初学者来说,甚至不了解Arduino是什么.因此,博主决定加入一个Arduino学 ...
- 基于jquery,php实现AJAX长轮询(LongPoll),类似推送机制
HTTP是无状态.单向的协议,用户只能够通过客服端向服务器发送请求并由服务器处理发回一个响应.若要实现聊天室.WEBQQ.在线客服.邮箱等这些即时通讯的应用,就要用到“ 服务器推送技术(Comet)” ...
- RESTful基本概念
文章目录 01 前言 02 RESTful的来源 03 RESTful6大原则 1. C-S架构 2. 无状态 3.统一的接口 4.一致的数据格式 4.系统分层 5.可缓存 6.按需编码.可定制代码( ...
- Go defer使用
defer使用语法 //defer后面必须是函数调用语句或方法调用语句,不能是其他语句,否则编译器会出错. package main import ( "fmt" ) func f ...