PAT1013 Battle Over Cities

Sample Input

3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
0

 

#include <iostream>
#include <string.h>
using namespace std;

int map[1000][1000]={0};
int copy_map[1000][1000]={0};
bool visit[1000];
bool need_to_check[1000];

void dfs(int n,int N)
{
	for(int i = 1; i <= N; i++)
	{
		if(i != n && need_to_check[i] && copy_map[i][n] && !visit[i])
		{
			visit[i] = true;
			dfs(i,N);
		}
	}
}

int main()
{
	int N,M,K;
	cin>>N>>M>>K;
	for(int i = 1; i <= M; i++)
	{
		int s,e;
		cin>>s>>e;
		copy_map[s][e] = copy_map[e][s] = map[s][e] = map[e][s] = 1;
	}

	int city;
	for(int i = 1; i <= K; i++)
	{
		memset(need_to_check, true, sizeof(need_to_check));
		int count = 0;
		cin>>city;
		need_to_check[city] = false;
		for(int j = 1; j <= N; j++)
		{
			copy_map[city][j] = copy_map[j][city] = 0;
		}
		for(int k = 1; k <= N; k++)
		{
			if(need_to_check[k] && !visit[k])
			{
				count++;
				dfs(k,N);
			}
		}
		if(count > 1)
		{
			cout<<count - 1<<endl;;
		}
		else 
		{
			cout<<"0"<<endl;
		}

		for(int j = 1; j <= N; j++)
		{
			copy_map[city][j] = map[city][j];
			copy_map[j][city] = map[j][city];
		}
		memset(visit,0,sizeof(visit));
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值