1013. Battle Over Cities (25)

来源:互联网 发布:68.168.16.158现在域名 编辑:程序博客网 时间:2024/06/10 21:24

1013. Battle Over Cities (25)

原题地址:http://www.patest.cn/contests/pat-a-practise/1013

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

解题思路:对每一座城市孤立后的情况分别求图的连通分量,需要建立的公路数为连通分量数减2(因为孤立的城市被算作一个连通分量)。

代码如下:

#include <stdio.h>#include <string.h>#define MAXN 1005int G[MAXN][MAXN];int mirror[MAXN][MAXN];int vis[MAXN];int N,M,K;int dfs(int k){    for(int i = 0; i < N; i++){        if(!vis[i] && mirror[k][i]){            vis[i] = 1;            dfs(i);        }           }}int findans(int m){    int count = 0;    memcpy(mirror,G,sizeof(G));    memset(vis,0,sizeof(vis));      for(int i = 0; i < N; i++){        mirror[i][m] = 0;        mirror[m][i] = 0;    }    for(int i = 0; i < N; i++){        if(!vis[i]) {            vis[i] = 1; dfs(i);            count++;        }    }    printf("%d\n",count-2);    return 0;}int main(){//  freopen("1013.txt","r",stdin);      scanf("%d%d%d",&N,&M,&K);    memset(G,0,sizeof(G));    for(int i = 0 ; i < M; i++){        int f, t;        scanf("%d%d",&f,&t);        G[f-1][t-1] = 1;        G[t-1][f-1] = 1;        }    for(int i = 0; i < K; i++){        int m;        scanf("%d",&m);        findans(m-1);       }   }
0 0
原创粉丝点击