dfs求最大层数
并查集求连通个数

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
/*
dfs求最大层数
并查集求连通个数
*/
const int maxn=+;
int n;
int anslayer=; //最大层数
vector<int>deeproot;
int maxlayer; //保存当前dfs的最大层数
int vis[maxn];
//并查集
struct UF{
int fa[maxn];
void init(){
for(int i=;i<maxn;i++)
fa[i]=i;
}
int find_root(int x){
if(fa[x]!=x)
fa[x]=find_root(fa[x]);
return fa[x];
}
void Union(int x,int y){
int fx=find_root(x);
int fy=find_root(y);
if(fx!=fy){
fa[fy]=fx;
}
} }uf; //链式前向星建立边
int head[maxn];
int tot;
struct Edge{
int to;
int next;
}edge[maxn*]; int init(){
memset(head,-,sizeof(head));
tot=;
} void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int u,int layer){
bool flag=true;
vis[u]=;
for(int k=head[u];k!=-;k=edge[k].next){
int v=edge[k].to;
if(!vis[v]){
flag=false;
//vis[v]=1;
dfs(v,layer+);
} }
//到叶子节点了
if(flag){
if(layer>maxlayer)
maxlayer=layer;
}
}
int main()
{
int u,v;
scanf("%d",&n);
init();
uf.init();
for(int i=;i<n-;i++){
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
uf.Union(u,v);
} memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
int f=uf.find_root(i);
vis[f]=;
}
int cnt=;
for(int i=;i<=n;i++){
if(vis[i]==)
cnt++;
}
if(cnt>){
printf("Error: %d components\n",cnt);
}
else{
for(int i=;i<=n;i++){
maxlayer=;
memset(vis,,sizeof(vis));
//printf("dfs %d\n",i);
dfs(i,);
if(maxlayer>anslayer){
anslayer=maxlayer;
deeproot.clear();
deeproot.push_back(i);
}
else if(maxlayer==anslayer){
deeproot.push_back(i);
}
}
sort(deeproot.begin(),deeproot.end());
for(int i=;i<deeproot.size();i++){
printf("%d\n",deeproot[i]);
}
}
return ;
}

PAT甲题题解-1021. Deepest Root (25)-dfs+并查集的更多相关文章

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

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

  2. PAT甲题题解-1114. Family Property (25)-(并查集模板题)

    题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...

  3. PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性

    题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...

  4. 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 ...

  5. PAT甲题题解-1130. Infix Expression (25)-中序遍历

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789828.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲题题解-1051. Pop Sequence (25)-堆栈

    将1~n压入最多为m元素的栈 给出k个出栈序列,问你是否能够实现. 能输出YES 否则NO 模拟一遍即可,水题. #include <iostream> #include <cstd ...

  7. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  8. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

  9. PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)

    如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...

随机推荐

  1. python 使用csv 文件写入 出现多余空行数据解决方案

    因为csv.writerow() 方法会造成读取时每条数据后多一条空数据 解决方案如下: 分为两种情况 python2 和 python3 先说python2版本 with open('xxx.csv ...

  2. [Python] 制作启动uiautomator2 的web版 uiautomatorviewer2 批处理启动

    打开一个txt文件,复制如下命令进行并另存为为 .bat文件 @echo on @echo 正在启动 uiautomatorviewer2 python -m weditor @echo off 注意 ...

  3. cookie的详解

    cookie是如何出生的 由于HTTP协议是无状态的,而服务器端的业务必须是要有状态的.Cookie诞生的最初目的是为了存储web中的状态信息,以方便服务器端使用.比如判断用户是否是第一次访问网站.目 ...

  4. python textwrap.md

    textwrap textwrap模块可以用来格式化文本, 使其在某些场合输出更美观. 他提供了一些类似于在很多文本编辑器中都有的段落包装或填充特性的程序功能. Example Data 本节中的示例 ...

  5. python第三十四课——2.匿名函数配合容器函数的使用

    匿名函数配合容器函数的使用(了解) 1.匿名函数配合列表对象使用 lt=[lambda x:x**2,lambda x:x**3,lambda x:x**4] for i in lt: print(i ...

  6. Volley源码分析(三)NetWorkDispatcher分析

    NetWorkDispatcher分析 NetWorkDispatcher和CacheDispatcher一样,继承于Thread,在run方法中实现一个无限循环,代码如下 @Override pub ...

  7. jQuery 动态加载下拉框选项(Django)

    function change_style() { $.ajax({ url: "{% url 'change_style' %}", type: "GET", ...

  8. bip39

    BIP: 39 (助记词) Layer: Applications Title: Mnemonic code for generating deterministic keys Author: Mar ...

  9. [转]Custom Controls in Visual C# .NET-如何实现自定义控件

    A very simple introduction to writing your first .NET control Download source files - 1 Kb Introduct ...

  10. Python基础(11)——反射、异常处理

    1.反射 以下均是对对象的操作,而不是对类 class Foo(object): def __init__(self): self.name = 'wupeiqi' def func(self): r ...