PAT A1021.Deepest Root

时间限制: 2000 ms    内存限制: 64 MB    代码长度限制: 16 KB

A graph which is connected and acyclic can be considered a tree. The hight 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 (≤10​4​​) 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

 题目大意:

acyclic 无环的;非循环的。

非循环的图可以看做树,树的高度取决于所选择的根节点。现给你一棵树,找出有最高高度的根节点。

输入样例第一行给出节点个数N,随后N-1行给出边(因为是非循环)。(N<=10000)

输出样例,输出具有最深高度的根节点,如果不唯一按数字从小到大输出;如果给出图不是单一连通集,输出“Error: K components”,其中K是连通集个数。

题目分析:

求深度,想到深度优先搜索。把把每一个点当做起点遍历一遍,找到最大的那个点和他的高度。

最大点的表示:用vector<Node>的形式表示出来,一个一个添加,最后排序并输出。

如果遍历结束所涉及节点个数小于总个数,则找出有几个连通集。

 

重要部分:

 

注意事项:

 

完整代码:

提交时间状态分数题目编译器耗时用户
2018/11/28 00:10:28

部分正确

201021C++ (g++)1109 msDirichlet
测试点结果耗时内存
0答案正确3 ms512KB
1答案正确3 ms640KB
2答案错误4 ms512KB
3答案正确1109 ms1152KB
4答案正确3 ms640KB
5答案正确3 ms768KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

struct Node{
    int data;
    int height;
};

const int maxn = 10010;
vector<int> Adj[maxn];   //图的邻接表
vector<Node> H;         //表示树高的数组
bool vis[maxn]={false};
int N;//节点个数
int depth, numConnect, deepest=0;//每次计算的高度和连通集内节点个数

void DFS(int u, int d){//节点,深度
    numConnect++;//记录连接点个数增加
    vis[u]=true;
    for(int i=0; i<Adj[u].size(); i++){//第一维从1开始,第二维从0开始
        int v = Adj[u][i];
        if(vis[v]==false){
            DFS(v,d+1);
        }
        deepest = deepest>d?deepest:d;
    }
}
//传统遍历求连通集个数,返回负值
int DFSReal(){
    memset(vis,false,sizeof(vis));
    int numCircle=0;
    for(int i=1; i<N; i++){
        if(vis[i]==false){
            DFS(i,1);
            numCircle++;
        }
    }
    return -numCircle;
}
//以u为起点,输出以u为根的高度,如果不连通输出负值表示连通集个数
int DFSTraversal(int u){
    //初始化
    numConnect=0;
    deepest=0;
    memset(vis, false, sizeof(vis));
    //求树高
    DFS(u,1);
    if(numConnect<N){
        return DFSReal();
    }
    return deepest;
}

bool comp(Node a, Node b){
    if(a.height!=b.height) return a.height>b.height;
    else return a.data<b.data;
}

int main()
{
    int e1,e2;
    cin>>N;
    for(int i=1; i<N; i++){
        Node tmp1,tmp2;
        cin>>e1>>e2;
        Adj[e1].push_back(e2);
        Adj[e2].push_back(e1);
    }
    //processing
    Node c;
    for(int i=1; i<=N; i++){
        c.data=i;
        c.height=DFSTraversal(i);
        if(c.height<0){//不连通,结束,输出
            cout<<"Error: "<<-c.height<<" components"<<endl;
            return 0;
        }
        H.push_back(c);
    }
    //sorting
    sort(H.begin(), H.end(), comp);
    //outputing
    for(int i=0; i<N && H[i].height==H[0].height; i++){
        cout<<H[i].data<<endl;
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值