1021 Deepest Root (25 分)--PAT甲级

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

题目大意:找到使一棵树深度最大的所有根节点,若不为树,则输出连通分量个数。
先一遍dfs得到深度最大的所有点的集合,再从集合中随便找一个节点,从该点出发再做一遍dfs,得到另一个深度最大的所有点的集合,两个集合的并集即为结果。

也可以直接暴力法:对每个节点做一遍dfs,将得到的深度最大的节点记录下来。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include <algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
const int maxn=10010;
bool visit[maxn]={false};
bool v[maxn][maxn]={false};
vector<vector<int>> e;
int n;
set<int>res;
vector<int>temp;
int maxheight=0;
void dfs(int now,int height){
    visit[now]=true;
    if(height>maxheight){
        maxheight=height;
        temp.clear();
        temp.push_back(now);
    }
    else if(height==maxheight){
        temp.push_back(now);
    }
    for(int i=0;i<e[now].size();i++){
        if(visit[e[now][i]]==false){
            dfs(e[now][i],height+1);
        }
    }
}

int main()
{
    scanf("%d",&n);
    e.resize(n+1);
    for(int i=1;i<n;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        e[a].push_back(b);
        e[b].push_back(a);
    }
    int cnt=0;
    int now;
    for(int i=1;i<=n;i++){
        if(visit[i]==false){
            dfs(i,1);
            cnt++;
        }
        if(i==1){
            if(temp.size()>0) {
                now=temp[0];
                for(int j=0;j<temp.size();j++){
                    res.insert(temp[j]);
                }
                temp.clear();
            }
        }
    }
    if(cnt>1){
        printf("Error: %d components\n",cnt);
    }
    else{
        for(int i=1;i<=n;i++) visit[i]=false;
        dfs(now,1);
        for(int j=0;j<temp.size();j++){
            res.insert(temp[j]);
        }
        for(set<int>::iterator it=res.begin();it!=res.end();it++){
            printf("%d\n",*it);
        }
    }
    return 0;
}

暴力:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
const int maxn=10010;
const int inf=0x3fffffff;
bool visit[maxn];
vector<vector<int>> edge;
int maxheight=0,temp=0;
vector<int>res;
void dfs(int now,int height){
    visit[now]=true;
    if(height>temp){
        temp=height;
    }
    for(int i=0;i<edge[now].size();i++){
        if(visit[edge[now][i]]==false) dfs(edge[now][i],height+1);
    }
}
int main()
{
    int n,a,b;
    scanf("%d",&n);
    edge.resize(n+1);
    for(int i=1;i<n;i++){
        scanf("%d %d",&a,&b);
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        fill(visit+1,visit+n+1,false);
        int sum=1;
        bool flag=false;
        dfs(i,1);
        for(int j=1;j<=n;j++){
            if(visit[j]==false){
                dfs(j,1);
                sum++;
                flag=true;
            }
        }
        if(flag){
            printf("Error: %d components",sum);
            return 0;
        }
        if(temp>maxheight){
            res.clear();
            res.push_back(i);
            maxheight=temp;
        }
        else if(temp==maxheight){
            res.push_back(i);
        }
        temp=0;
    }
    for(int i=0;i<res.size();i++){
        printf("%d\n",res[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值