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));
	}
}
 

你可能感兴趣的:(over)