浙江财经大学
信工学院ACM集训队

PAT(A) 1013. Battle Over Cities (25)

本文由 Ocrosoft 于 2017-01-05 14:50:20 发表

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

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 K numbers, which represent the cities we concern.

Output

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
题意:n个城市,m条无向边,k个城市询问;每次询问为给出的编号的城市被占领后,要保证城市连通,要新建多少条路(就是找不连通块,要修建不连通块-1条路)。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
#define  rep(a,v,b) for(int a=v;a<b;a++)
#define  repe(a,v,b) for(int a=v;a<=b;a++)
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 1000 + 10;
const int MOD = 1000000009;
LL GCD(LL a, LL b)
{
	if (!b)return a;
	return GCD(b, a%b);
}
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
/*(◕‿‿◕) 签订契约,成为马猴烧酒吧! (◕‿‿◕)*/
/*(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
using namespace std;
bool vis[MAXN];
vector<int> mp[MAXN];
void dfs(int x)
{
	if (vis[x])return;
	vis[x] = true;
	for (int i = 0, vs = mp[x].size(); i < vs; i++)
		dfs(mp[x][i]);
}
int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 0; i < m; i++)
	{
		int x, y; cin >> x >> y;
		mp[x].push_back(y);
		mp[y].push_back(x);
	}
	while (k--)
	{
		int x; cin >> x;
		ms(vis); vis[x] = -1;
		int id = 0;
		for (int i = 1; i <= n; i++)
		{
			if (vis[i])continue;
			dfs(i);
			id++;
		}
		printf("%d\n", id-1);
	}
	return 0;
}

顺便安利一下自己用的TamperMonkey脚本(虽然用处不大):https://greasyfork.org/zh-CN/scripts/26307-pat-helper

欢迎转载,请保留出处与链接。Ocrosoft » PAT(A) 1013. Battle Over Cities (25)

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址