1021. Deepest Root (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1021

题目:

1021. Deepest Root (25)

时间限制
1500 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 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

分析:

用并查集来考察是否能构成一棵树,
找出最长树的深度基于这样的一个事实:
从任意一个节点S0出发,找到最大的深度H0和其对应的叶子节点D0,然后再从D0出发(当作S1)找到最深的长度H1,如果和H0相等则其就是最大的H,否则继续找,直到两次找到的H相等。
其中,找最大深度函数find_height(),我是用类似层序遍历去做的,先把根节点和-1(一个标志位)放入队列中,然后每次取对头,如果是元素的话,则把其子节点都放入队列,如果是-1的话,则把深度++,并且再把-1放入,相当于-1变成了每层的结尾的标志。
当然,求树最大深度可以用递归很容易做出来,这里只是用别的方法做个拓展(其实这代码以前写的,当时瞎想就想到这个)。

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\n", ans[i]);
   }
   
   
  }
 }
 return 0;
}


截图:


——Apie陈小旭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值