PAT A 1013. Battle Over Cities (25)

来源:互联网 发布:mfc编程实例 pdf 编辑:程序博客网 时间:2024/06/05 12:44

题目

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 31 21 31 2 3

Sample Output

100

 

即求图中去掉某个点及相连边后的连通域数量,

标记点的探测情况,多次dfs\bfs,直到全部探测完毕。

坑点:按文字理解,原本图应当是连通的,只需要考虑和去除的点相连的点即可,但是实际情况有一个测试点的原本的图是不连通的……

 

代码:

#include <iostream>#include <stdio.h>#include <queue>using namespace std;#define MAX 0x3fffffff//距离无限大int test_city(int city,int **map,int n,int *dis);//测试城市city失去时要修的路,map地图,n城市数量,dis距离缓存int main(){int n,m,k;int i,j,city1,city2;int **map;int *dis;cin>>n>>m>>k;map=new int*[n];//地图连通性图,0表示不连通,1表示连通dis=new int[n];//距离,0表示未达,1表示已达for(i=0;i<n;i++)map[i]=new int[n];for(i=0;i<n;i++)//初始化地图for(j=0;j<n;j++)map[i][j]=0;for(i=0;i<m;i++)//对1~n标号的城市之间的路转换为0~n-1存入map{scanf("%d %d",&city1,&city2);map[city1-1][city2-1]=1;map[city2-1][city1-1]=1;}for(i=0;i<k;i++)//测试k个城市{scanf("%d",&city1);printf("%d\n",test_city(city1-1,map,n,dis));}for(i=0;i<n;i++)delete [] map[i];delete [] map;delete [] dis;return 0;}int test_city(int city,int **map,int n,int *dis){int area=0; //连通域数量int i,j=0;queue<int> to_test,way_test;//考虑连通域数,路线测试for(i=0;i<n;i++)//初始化dis[i]=MAX;for(i=0;i<n;i++)//压入所有非测试城市if(city!=i)to_test.push(i);if(to_test.empty())return 0;while(!to_test.empty())//存在未连通的城市{while(dis[to_test.front()]<MAX)//跳过可以连通的城市{to_test.pop();if(to_test.empty())return area-1;}j=to_test.front();//每次弹出意味着一个新的连通域to_test.pop();area++;way_test.push(j);//将连通域的初始点压入区域判断堆栈while(!way_test.empty()){while(dis[way_test.front()]<MAX)//跳过已经检测过的连通域内的点{way_test.pop();if(way_test.empty())break;}if(way_test.empty())break;j=way_test.front();//每次有弹出的点都是已经检测过可以到达,但是没有检测过该点可以到的点way_test.pop();dis[j]=1;//将这个点距离设为1,表示已经检测过for(i=0;i<n;i++)//扫描该点的所有路(除了和city的){if(map[i][j]==1&&dis[i]==MAX&&i!=city)//将其中还没检测到的点压入堆栈,距离设为1way_test.push(i);}}}return area-1;}


 

 

 

 

0 0
原创粉丝点击