10.3 图的遍历
PAT A1013 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 Specification:
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Specification:
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
#include<cstdio> #include<cstring> #include<vector> using namespace std; int n,m,k; const int N = 1111; vector<int> G[N]; bool vis[N]; int currentPoint; void dfs(int v){ if(v == currentPoint) return; vis[v] = true; for(int i=0;i<G[v].size();i++){ if(vis[G[v][i]] == false) dfs(G[v][i]); } } int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=0;i<m;i++){ int a,b; scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } for(int query = 0;query<k;query++){ scanf("%d",¤tPoint); memset(vis,false,sizeof(vis)); int block = 0; for(int i=1;i<=n;i++){ if(i != currentPoint && vis[i]==false){ dfs(i); block++; } } printf("%d\n",block-1); } return 0; }
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes' numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print Error: K components
where K
is the number of connected components in the graph.
Sample Input 1:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int N = 10010; vector<int> G[N]; bool isRoot[N]; int father[N]; int findFather(int x){ int a = x; while(x != father[x]){ x = father[x]; } while(a != father[a]){ int z = a; a = father[a]; father[z] = x; } return x; } void Union(int a,int b){ int faA = findFather(a); int faB = findFather(b); if(faA != faB){ father[faA] = faB; } } void init(int n){ for(int i=1;i <= n;i++){ father[i] = i; } } int calBlock(int n){ int Block = 0; for(int i = 1; i <= n;i++){ isRoot[findFather(i)] = true; } for(int i = 1; i <= n ;i++){ Block += isRoot[i]; } return Block; } int maxH = 0; vector<int> temp,Ans; void DFS(int u,int Height,int pre){ if(Height > maxH){ temp.clear(); temp.push_back(u); maxH = Height; }else if(Height == maxH){ temp.push_back(u); } for(int i=0;i<G[u].size();i++){ if(G[u][i] == pre) continue; DFS(G[u][i],Height+1,u); } } int main(){ int a,b,n; scanf("%d",&n); init(n); for(int i=1;i<n;i++){ scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); Union(a,b); } int Block = calBlock(n); if(Block != 1){ printf("Error: %d components\n",Block); }else{ DFS(1,1,-1); Ans = temp; DFS(Ans[0],1,-1); for(int i = 0;i < temp.size();i++){ Ans.push_back(temp[i]); } sort(Ans.begin(),Ans.end()); printf("%d\n",Ans[0]); for(int i = 1; i < Ans.size(); i++){ if(Ans[i] != Ans[i-1]){ printf("%d\n",Ans[i]); } } } }