1021 Deepest Root

0、题目

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 NNN (≤104≤10^4≤104) which is the number of nodes, and hence the nodes are numbered from 111 to NNN. Then N−1N−1N−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、大致题意

给出无向图的顶点个数 NNN ,和 N−1N-1N−1 条边,判断这个无向图可不可以转化成树。

  • 如果可以,给出所有具有最大深度树的树根
  • 如果不行,输出连通子集个数

2、基本思路

2.1 无向图转化为树的条件

首先一个问题,怎样可以判断一个无向图可以转化为树?

  • 必须是无回路的连通图
  • 当该图为有n-1条边的连通图,也为树

这边可以思考一下为什么满足以上两个条件只一就行。应该画个图就行。

2.1 判断图连通分量的个数

有两种方法 ——dfs 和 并查集。

在前面的 1013 Battle Over Cities 使用过使用并查集查找连通子集的个数,这边就使用 dfs

  • vis[] 记录访问历史, 访问过该结点就跳过,没有访问过就 dfs(i) ,同时 连通分量数+1,即:如果图是连通的,dfs一次就可以访问到所有结点。
  • 如果该图为树,让每个结点都做一次根,deep[] 记录每个结点为根时的树的深度,对每个结点 dfs_deep(),用 tmp 记录当前的深度,并与 max_deep 比较,如果有更大的就更新 vector<int>ans

3、解题过程

3.1 在线画图工具

首先,安利一个在线画图工具

例如:

Sample Output 1:
5
1 2
1 3
1 4
2 5

Sample Input 2:
5
1 3
1 4
2 5
3 4

3.2 AC代码

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=10040;
const int maxm=100050;struct Edge {int to,next;
} edge[maxm];int n,head[maxn],vis[maxn],num_edge;
int num_components,max_deep,tmp;
vector<int>ans;void addedge(int from,int to) { //头插法插入结点edge[++num_edge].next=head[from];edge[num_edge].to=to;head[from]=num_edge;
}void dfs(int x) {int k=head[x];while(k!=0) {if(vis[edge[k].to]==1) {k=edge[k].next;continue;}vis[edge[k].to]=1;dfs(edge[k].to);k=edge[k].next;}
}void dfs_deep(int x,int deep) { //dfs查找最大深度if(!vis[x]) {vis[x]=1;int k=head[x];while(k!=0) {if(vis[edge[k].to]==1) {k=edge[k].next;continue;}dfs_deep(edge[k].to,deep+1);tmp=max(deep+1,tmp);k=edge[k].next;}}
}int main() {cin>>n;int a,b;memset(edge,0,sizeof(edge));memset(vis,0,sizeof(vis));memset(head,0,sizeof(head));for(int i=0; i<n-1; i++) {cin>>a>>b;addedge(a,b);addedge(b,a);}num_components=0;for(int i=1; i<=n; i++) {if(vis[i]==0) {dfs(i);num_components++;}}if(num_components!=1) { //图不能转化为树cout<<"Error: "<<num_components<<" components";} else {max_deep=-1;for(int i=1; i<=n; i++) {memset(vis,0,sizeof(vis));tmp=0;dfs_deep(i,0);if(tmp>max_deep) { //当前深度大于最大深度ans.clear();ans.push_back(i);max_deep=tmp;} else if(tmp==max_deep) { //当前深度等于最大深度ans.push_back(i);max_deep=tmp;}}cout<<ans[0];for(int i=1; i<ans.size(); i++) {cout<<"\n"<<ans[i];}}return 0;
}

1021 Deepest Root(dfs,图的联通子集个数,树的深度)相关推荐

  1. PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度

    文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...

  2. 浙大PAT 1021. Deepest Root (25)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

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

    题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...

  4. 1021. Deepest Root (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1021 题目: 1021. Deepest Root (25) 时间限制 1500 ms 内存限制 ...

  5. 【分析】1021 Deepest Root (25 分)【DFS解法】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A graph which is connected and acyclic can be considered a tree. ...

  6. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  7. 1021 Deepest Root

    要解决两大问题: 1. 数包含几个连通分量 2. 如何找到最深结点 注意:connected components的意思是连通分量 问题1我用并查集解决 问题2转化为如何得到每个结点的深度 值得注意之 ...

  8. 1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 方法一: 数组模拟邻接表 第一步: 爆搜df ...

  9. DFS:图的联通块 AOJ-0118 Property Distribution

    这道题类似于联通图问题,将联通的归成一个,数一下总共有几个即可. 因为题目告诉不会有空格,所以排除标记用空格表示即可. Property Distribution Aizu - 0118 タナカ氏が  ...

  10. 【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)

    题干: A graph which is connected and acyclic can be considered a tree. The height of the tree depends ...

最新文章

  1. Fiddler监控面板显示Server栏(Fiddler v5.0)
  2. Prometheus 如何做到“活学活用”,大牛总结的避坑指南
  3. ci中如何得到配置的url
  4. intersect函数_PHP array_intersect()函数与示例
  5. (转)SpringMVC学习(九)——SpringMVC中实现文件上传
  6. MTK平台camera驱动架构分析
  7. 【Session】多服务器节点间session共享
  8. python写脚本看xde文件_python读文件的问题
  9. 使用kubeadm快速部署一套K8S集群
  10. 可变参C API va_list,va_start,va_arg_va_end以及c++可变参模板
  11. 高德地图生成静态图片,显示指定经纬度的地图
  12. 1.MySQL数据库 2.SQL语句
  13. ContentProvider详解
  14. 微信小程中文特殊字符编码与解码
  15. MOS管驱动电路隔离技术
  16. [译]Unity3D Shader教程(二)HLSL
  17. Android固件简介
  18. 统计学②——概率分布(几何,二项,泊松,正态分布)
  19. 小区物业管理系统(数据库课程设计)
  20. STM32单片机启动流程分析

热门文章

  1. 各互联网技术领域pdf图书合集(百度网盘)
  2. VS Code下载安装
  3. RT-Preempt笔记
  4. LeetCode-717. 1比特与2比特字符
  5. 【HDU 5956】The Elder(树上斜率DP)
  6. hdfs高可用与高拓展机制分析
  7. 使用PMOS管构建电源延时供电电路
  8. 龙蜥社区第十次运营委员会议顺利召开!
  9. 嵌入式linux locale,总结!嵌入式linux基础学习笔记
  10. Samy 蠕虫代码