PAT甲级 1021 没有AC,留着重做

51 篇文章 0 订阅
3 篇文章 0 订阅

Deepest Root

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


  1. 考点

    • 树的遍历
    • 连通分量的个数
  2. 题解

    • 假设连通分量为1(不为1比较简单),那么选取1号节点为根,用dfs计算其它所有节点(叶节点)到root的距离。
    • 对dist排序。
    • 找到最大的那个。当最大距离的叶节点个数为1时,则找到所有次大的的节点。当不为1时,则当前最大的叶节点即为所有的节点。
    • 对找到的节点排序。
  3. 补充

    • 题解中第三条:比如[3,3,2],则组合为(3,3);再如[4,3,3],则组合为(4,3),(4,3)。
  4. 错误

    • 补充3的组合有问题
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=10000+10;
vector<int> v[maxn],rst;

struct node{
    int dist,index;
}dist[maxn];
bool visit[maxn],record[maxn];

bool comp(node a,node b){
    return a.dist>b.dist;
}

void dfs(int s,int length){
    if(visit[s])return;
    visit[s]=true;
    if(v[s].size()<=1){//叶节点度为1,考虑n=1
        dist[s].dist=length;
        dist[s].index=s;
    }
    for(auto it=v[s].begin();it!=v[s].end();it++){
        dfs(*it,length+1);
    }
}

void output(int componentsNum,int n){
    if(componentsNum!=1){
        printf("Error: %d components",componentsNum);
    }else{
        sort(dist+1,dist+n+1,comp);
        int maxNumberCnt=1;
        int i;
        rst.push_back(dist[1].index);
        for(i=2;i<=n;i++){
            if(dist[i].dist!=dist[i-1].dist){
                break;
            }
            rst.push_back(dist[i].index);
            ++maxNumberCnt;
        }
        if(maxNumberCnt==1&&i<=n){
            rst.push_back(dist[i].index);
            for(i+=1;i<=n;i++){
                if(dist[i-1].dist!=dist[i].dist){
                    break;
                }
                rst.push_back(dist[i].index);
            }
        }
        sort(rst.begin(),rst.end());
        for(auto it=rst.begin();it!=rst.end();it++){
            printf("%d\n",*it);
        }
    }
}

int setDistance(int n){
    int i;
    int cnt=0;
    for(i=1;i<=n;i++){
        if(!visit[i]){
            dfs(i,1);
            cnt++;
        }
    }
    return cnt;
}

int main(){
    freopen("./in","r",stdin);
    int N;
    scanf("%d",&N);
    int up,vp;
    int i;
    for(i=1;i<N;i++){
        scanf("%d %d",&up,&vp);
        v[up].push_back(vp);
        v[vp].push_back(up);
    }
    output(setDistance(N),N);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值