1013. Battle Over Cities (25)
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 Input3 2 3 1 2 1 3 1 2 3Sample Output
1 0 0
主要思路:
1、计算图中去除某结点及该结点的所有边之后,该图的连通集团的数量(该数量-2即为需要rapair highway的数量)
2、原图只有一个城市单独处理。
3、City的编号(即图中结点的编号从1开始),在对图进行深度优先遍历时需要注意。
#include<iostream> using namespace std; #define MaxNode 1001 typedef struct graph //邻接矩阵存储图 { int value[MaxNode][MaxNode]; int n;//图中结点数 int m;//图中边的个数 }Graph,*pGraph; void InitGraph(pGraph G,int N,int M)//将图的结点数和边数传入并初始化图 { for (int i=0; i<=N; ++i) { for (int k=0; k<N; k++) { G->value[i][k]=-1; } } G->m=M; G->n=N; } void GetGraph(pGraph G,int M)//存储题目输入的图 { int v1,v2; for (int i=0; i<M; ++i) { cin>>v1>>v2; G->value[v1][v2]=1; G->value[v2][v1]=1; } } void DFS(pGraph G,int now,int* Visited)//从now结点开始对包含now结点的连通分量进行深度优先遍历 { Visited[now]=1; for (int i=1; i<=G->n; ++i) { if ((G->value[now][i]==1) && (!Visited[i])) { DFS(G,i,Visited); } } return; } int DFS_ALG(pGraph G)//对图G进行深度优先遍历 { int count=0;//连通分量个数 int Visited[MaxNode]; for (int i=1; i<=G->n; ++i) { Visited[i]=0; } for (int i=1; i<=G->n; ++i) { if (!Visited[i]) { DFS(G,i,Visited);//此处每次DFS遍历一个连通集团 count++;//图G中连通分量的个数 } } return count; } int One_City_Occupied(int city,Graph G)//注意用value方式传递,在栈空间中创建一个图G,对此图G的更改不会改动函数外的图G的值 { for (int i=1; i<=G.n; ++i) { if (G.value[city][i]==1) { G.value[city][i]=-1; G.value[i][city]=-1; } } return DFS_ALG(&G); } int main() { int N,M,K; cin>>N>>M>>K; if(N==1)//一个城市则没有highway { cout<<"0"; }else//正常情况,城市大于等于1 { pGraph G=new Graph; InitGraph(G, N,M); GetGraph(G,M); int city=0; int link=0;//连通分量个数 for (int i=0; i<K; ++i) { cin>>city; link=One_City_Occupied(city, *G); if (link==1) { cout<<"0"; }else { cout<<link-2;//被占领的城市为一个连通分量,剩下城市需要的道路最少只要此时的连通分量再减去一,故总的来说减去二 } if (i<K-1) { cout<<endl; } } } return 0; }