PAT Advanced Level 1021

1021 Deepest Root (25)(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
最后一题 A得很开心

去吃饭啦

/**********************
author: yomi
date: 18.8.20
ps:
**********************/
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 10010;

vector<int>g[maxn];
bool vis[maxn];
int ans[maxn];
int depth = 1, n, now;
void dfs(int index)
{
    if(vis[index] == true)
        return;
    vis[index] = true;
    for(int i=0; i<g[index].size(); i++){
        if(!vis[g[index][i]]){
            depth++;
            ans[now] = max(ans[now], depth);
            dfs(g[index][i]);
            depth--;
        }
    }
}

int main()
{
    int u, v, cnt=1;
    memset(ans, 0, sizeof(ans));
    memset(vis, 0, sizeof(vis));
    cin >> n;
    for(int i=0; i<n-1; i++){
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1; i<=n; i++){

        if(!vis[i]){
            dfs(i);
            cnt++;
        }
    }
    if(cnt-1>1){
        cout << "Error: " << cnt-1 << " components";
    }
    else{
        for(int i=1; i<=n; i++){
            memset(vis, 0, sizeof(vis));
            now = i;
            dfs(i);
        }
        int Max = 0;
        for(int i=1; i<=n; i++){
            Max = max(Max, ans[i]);
        }
        for(int i=1; i<=n; i++){
            if(Max == ans[i])
                cout << i << endl;
        }
    }

    return 0;
}
/**
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
**/

 

posted @ 2018-08-20 16:28  深圳地铁Princess  阅读(125)  评论(0编辑  收藏  举报