当前位置: 首页 > news >正文

1021 Deepest Root

1021 Deepest Root
分数 25

全屏浏览

切换布局
作者 CHEN, Yue
单位 浙江大学
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

1.分析

        1.要考虑结点数为1的情况,输出1。

        2.可以用dfs遍历每个点,找到高度最高的点。

        3.用并查集来找联通块的个数。

2.代码

#include<iostream>
#include<vector>
#include<cstring>
#include<set>
using namespace std;
const int MAX = 1e5 + 10;
vector<int> v[MAX];
set<int> s, t;
int fa[MAX], n,vis[MAX],ma,l;
int find(int x) {                   //并查集查找函数
    if (fa[x] != x) fa[x] = find(fa[x]);
    return fa[x];
}
void getroot(int x,int d){          //找深度
    vis[x]=1;
    int num=0;
    for (int i = 0; i < v[x].size(); i++) {
        int t=v[x][i];
        if(!vis[t]){
            getroot(t,d+1);
            num++;
        }
    }
    if(num==0){
        l=max(l,d);
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {  //初始化
        fa[i] = i;
    }
    for (int i = 1; i < n; i++) {     //输入
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
        fa[find(y)] = fa[find(x)];     //连接
    }
    for (int i = 1; i <= n; i++) {       //找联通块个数
        t.insert(find(i));
    }
    if (t.size() == 1) {
        for(int i=1;i<=n;i++){
            memset(vis,0,sizeof vis);
            l=0;
            getroot(i,1);
            if(l>ma){
                ma=l;
                s.clear();
                s.insert(i);
            }
            else if(ma==l){
                s.insert(i);
            }
        }
        for (auto it : s) {
            cout << it << endl;
        }
    }
    else cout << "Error: " << t.size() << " components" << endl;
    return 0;
}

相关文章:

  • 气象水文研究利器WRF-Hydro 耦合模式:从流域建模到洪水预报指南
  • MySQL的子查询
  • 网络安全小知识课堂(五)
  • Linux 进程 | 概念 / 特征 / 状态 / 优先级 / 空间
  • 【leetcode 100】贪心Java版本
  • leetcode6.Z字形变换
  • Python星球日记 - 第16天:爬虫基础(仅学习使用)
  • uni-app ucharts自定义换行tooltips
  • 基于Python脚本实现Flink on YARN任务批量触发Savepoint的实践指南
  • consul服务注册与发现(go)-学习笔记
  • aspx目录扫描字典
  • 优选算法系列(6. BFS 解决 FloodFill 算法)
  • Ensemble of differential evolution variants(EDEV)
  • 6.综合练习1-创建文件
  • 三轴云台之姿态测量系统篇
  • 10-python面向对象(上)
  • 汽车电子笔记之:基于Tasking编译器怎么制作库文件并将库文件集成进工程释放
  • 内容中台的数字化管理核心是什么?
  • `ConstantPositionProperty` 的使用与应用
  • 汇丰xxx