题目链接:http://www.patest.cn/contests/pat-a-practise/1021
题目:
1021. Deepest Root (25)
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 (<=10000) 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 5Sample Output 1:
3 4 5Sample Input 2:
5 1 3 1 4 2 5 3 4Sample Output 2:
Error: 2 components
分析:
用并查集来考察能否构成一棵树,
找出最长树的深度基于这种一个事实:
从随意一个节点S0出发,找到最大的深度H0和其相应的叶子节点D0。然后再从D0出发(当作S1)找到最深的长度H1,假设和H0相等则其就是最大的H,否则继续找,直到两次找到的H相等。
当中,找最大深度函数find_height()。我是用类似层序遍历去做的,先把根节点和-1(一个标志位)放入队列中。然后每次取对头,假设是元素的话。则把其子节点都放入队列。假设是-1的话。则把深度++,而且再把-1放入,相当于-1变成了每层的结尾的标志。
当然,求树最大深度能够用递归非常easy做出来,这里仅仅是用别的方法做个拓展(事实上这代码曾经写的,当时瞎想就想到这个)。
AC代码:
#include<stdio.h> #include<queue> #include<vector> #include<algorithm> using namespace std; vector<int>V[10001]; queue<int>Q; int Tree[10001]; int ans[10001]; int findRoot(int x){ if (Tree[x] == -1)return x; else { int tmp = findRoot(Tree[x]); Tree[x] = tmp; return tmp; } } bool mark[10001]; int tail; int n; int find_height(int x){//找到最大深度的函数 mark[x] = true; int height_tmp = 0; Q.push(x); Q.push(-1); while (!Q.empty()){ if (Q.front() == -1){ height_tmp++; Q.pop(); if (Q.empty())break; else Q.push(-1); } int front = Q.front(); tail = front; for (int i = 0; i < V[front].size(); i++){ if (!mark[V[front][i]])Q.push(V[front][i]); mark[V[front][i]] = true; } Q.pop(); } for (int i = 1; i <= n; i++){ mark[i] = false; } return height_tmp; } int main(void){ //freopen("F://Temp/input.txt", "r", stdin); while (scanf("%d", &n) != EOF){ for (int i = 1; i <= n; i++){ //init Tree[i] = -1; ans[i] = 0; mark[i] = false; } if (!Q.empty())Q.pop(); int a, b; for (int i = 0; i < n - 1; i++){//并查集部分 scanf("%d%d", &a, &b); V[a].push_back(b); V[b].push_back(a); a = findRoot(a); b = findRoot(b); if (a != b) Tree[a] = b; } int com = 0; for (int i = 1; i <= n; i++){ if (Tree[i] == -1)com++; } if (com > 1){//假设并查集找出超过两部分,则输出error... printf("Error: %d components", com); continue; } else{ int h_max; int head; for (int i = 1; i <= n; i++){ if (V[i].size() == 1){ head = i; break; } } h_max = find_height(head); while (h_max != find_height(tail)){//假设找到的高度不同样。则继续找 head = tail; h_max = find_height(head); } int j = 0; for (int i = 1; i <= n; i++){ if (find_height(i) == h_max){ ans[j++] = i; } } sort(ans, ans + j); for (int i = 0; i < j; i++){ printf("%d ", ans[i]); } } } return 0; }
截图:
——Apie陈小旭