PAT 1021. Deepest Root (25)

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

本题最容易想到是将任何两个点之间的距离进行计算最后得到最大的距离即可。我采用了计算了所有的leaf到其他所有节点的距离,然后得到最大的距离。
后面看了想网上的算法可以采用两次的DFS得到结果
  1 #include <iostream>
  2 #include <list>
  3 #include <set>
  4 #include <queue>
  5 #include <bitset>
  6 #include <algorithm>
  7 
  8 using namespace std;
  9 
 10 list<int> graph[10001];
 11 int main()
 12 {
 13     int n;
 14     cin >> n;
 15     for (int i = 0; i < n - 1; i++)
 16     {
 17         int n1, n2;
 18         cin >> n1 >> n2;
 19         graph[n1].insert(graph[n1].begin(), n2);
 20         graph[n2].insert(graph[n2].begin(), n1);
 21     }
 22 
 23     set<int> leaf;
 24     for (int i = 1; i <= n; i++)
 25         if (graph[i].size() == 1)
 26             leaf.insert(i);
 27     if (leaf.size() == 0)
 28         leaf.insert(1);
 29     int maxLength = -1;
 30     set<int> root;
 31     int length[10001];
 32     bitset<10001> visited;
 33     for (set<int>::iterator it = leaf.begin(); it != leaf.end(); it++)
 34     {
 35         visited.reset();
 36         queue<int> que;
 37         int start = *it;
 38         length[start] = 0;
 39         que.push(start);
 40         visited.set(start);
 41         bool flag = false;
 42         int cnt = 1;
 43         while (!que.empty())
 44         {
 45             int node = que.front();
 46             que.pop();
 47             for (list<int>::iterator lit = graph[node].begin(); lit != graph[node].end(); lit++)
 48             {
 49                 if (!visited[*lit])
 50                 {
 51                     length[*lit] = length[node] + 1;
 52                     visited.set(*lit);
 53                     que.push(*lit);
 54                 }
 55             }
 56             if (que.empty())
 57             {
 58                 for (int i = 1; i <= n; i++)
 59                 {
 60                     if (!visited[i])
 61                     {
 62                         que.push(i);
 63                         visited.set(i);
 64                         flag = true;
 65                         cnt++;
 66                         break;
 67                     }
 68                 }
 69             }
 70         }
 71 
 72         if (flag)
 73         {
 74             cout << "Error: " << cnt << " components" << endl;
 75             return 0;
 76         }
 77         int max = -1, maxIndex;
 78         for (int j = 1; j <= n; j++)
 79         {
 80             if (length[j] > max)
 81             {
 82                 max = length[j];
 83                 maxIndex = j;
 84             }
 85         }
 86         if (maxLength < max)
 87         {
 88             maxLength = max;
 89             root.clear();
 90             root.insert(start);
 91             root.insert(maxIndex);
 92         }
 93         else if (maxLength == max)
 94         {
 95             root.insert(start);
 96             root.insert(maxIndex);
 97         }
 98     }
 99 
100     for (set<int>::iterator it = root.begin(); it != root.end(); it++)
101         cout << *it << endl;
102 }

 

 

posted @ 2015-08-24 23:16  JackWang822  阅读(202)  评论(0编辑  收藏  举报