题意:

输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出。如果不是一棵树,输出这张图有几个部分。

trick:

时间比较充裕数据可能也不是很极限,所以用了一个暴力的手段。直接枚举度数为1的点进行深度优先搜索,如果递归了一万次还没有return就当它有环。(这道题的数据不存在只有一个带有自环的部分,即如有自环图必定不止一个)正解应当是dfs一次以后找到一些可能是最深根的叶子结点们,然后对这些叶子节点们依次dfs,再找到对应的一些叶子节点们。成对的叶子结点并且长度最大的就是答案。(最深根一定是成对成对的)(正解待补)

AAAAAccepted code:

 #include<bits/stdc++.h>
using namespace std;
vector<int>edg[];
int vis[];
int deg[];
int ans[];
int tmp=;
int mx=;
int num=;
int dfs(int x,int fa){
num++;
if(num>)//断言出现自环,一共只有N个点
return ;
vis[x]=;
++tmp;
mx=max(mx,tmp);
for(auto it:edg[x])
if(it!=fa)
dfs(it,x);
--tmp;
return ;
}
int main(){
int n;
cin>>n;
for(int i=;i<n;++i){
int u,v;
cin>>u>>v;
edg[u].push_back(v);
edg[v].push_back(u);
++deg[u];
++deg[v];
}
int cnt=;
int mxx=;
int flag2=;
for(int i=;i<=n;++i){
tmp=;
mx=;
if(deg[i]==){//叶子结点才可能作为最深根出现
if(!vis[i])
++cnt;
num=;
dfs(i,);
ans[i]=mx;
mxx=max(mxx,mx);
}
}
for(int i=;i<=n;++i)
if(!vis[i]){
num=;
dfs(i,);
++cnt;
}
int flag=;
if(cnt==){
for(int i=;i<=n;++i)
if(ans[i]==mxx){
if(flag)
cout<<"\n";
cout<<i;
flag=;
}
}
else
cout<<"Error: "<<cnt<<" components";
return ;
}

【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)的更多相关文章

  1. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  2. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  3. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

  4. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

  5. 1021 Deepest Root (25 分)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  6. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  7. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  8. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  9. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

随机推荐

  1. ci/cd部署时遇到的一个问题

    今天在部署项目的时候报了一个错Error  response  from  daemon:  endpoint  with  name  xxx already  exists  in  networ ...

  2. 05hive函数

    一. 系统内置函数 1)查看系统自带的函数 hive> show functions; 2)显示自带的函数的用法 hive> desc function upper; 3)详细显示自带的函 ...

  3. Sql性能优化梳理

    前言 先简单梳理下Mysql的基本概念,然后分创建时和查询时这两个阶段的优化展开. 1.0 基本概念简述 1.1 逻辑架构 第一层:客户端通过连接服务,将要执行的sql指令传输过来 第二层:服务器解析 ...

  4. python web django 2nd level -- 待更新

    练习代码位置 实例代码位置 --> app: myblog Form 利用Form表单验证,自己写的html 思路: 新建一个类 LoginForm(forms.Form) 新建对象 obj = ...

  5. linux配置放火墙开放端口

    vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...

  6. 2016 Google code jam 大赛

    二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  7. Linux编程日常错误

    编译的时候出现如下错误提示: undefined reference to `sem_init'undefined reference to `sem_post'undefined reference ...

  8. LVS DR实验!

    =========================================================================== 操作图 设备:两台节点模拟机,一台调度机 调度机 ...

  9. SpringCloud全家桶学习之客户端负载均衡及自定义负载均衡算法----Ribbon(三)

    一.Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端  负载均衡的工具(这里区别于nginx的负载均衡).简单来说,Ribbon是Netf ...

  10. linux chrome rpm chrome浏览器下载(ver 63-70)

    我的github chrome下载地址:https://github.com/chen1932390299/python 国内开源的资源 chrome下载centos 的:https://www.ch ...