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. SDN期末验收

    队名:取个队名真难 一.网络拓扑 二.负载均衡程序 1.建立拓扑的代码 拓扑 2.下发组表流表的代码 下发流表 三.演示视频 1.目的 服务器h2,h3,h4上各自有不同的服务,h1是客户端.实现一个 ...

  2. PHPer是草根吗

    以下文字并没有非常多的技术词汇,所以只要对PHP感兴趣的人都可以看看. PHPer是草根吗? 从PHP诞生之日起,PHP就开始在Web应用方面为广大的程序员服务.同时,作为针对Web开发量身定制的脚本 ...

  3. 1 TCP/IP通信

    重点参考长链接http://blog.csdn.net/fengyuzhengfan/article/details/38830115 http://blog.csdn.net/Jsagacity/a ...

  4. Kubernetes1.91(K8s)安装部署过程(三)--创建高可用etcd集群

    这里的etcd集群复用我们测试的3个节点,3个node都要安装并启动,注意修改配置文件 1.TLS认证文件分发:etcd集群认证用,除了本机有,分发到其他node节点 scp ca.pem kuber ...

  5. 爬虫代理squid

    yum -y install squid 配置文件一般是在/etc/squid3/下的squid.conf文件 pyspider使用的时候只需要设置代理服务器为你配置好的服务器的IP就可以了 clas ...

  6. Apple 相关官方地址

    https://developer.apple.com/download/more/ 证书制作地址: https://developer.apple.com/account/ios/profile/ ...

  7. poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15349   Accepted: 7379 Descripti ...

  8. SNAT和DNAT

    1.SNAT iptables防火墙 Centos6.6 64位 iptables 内网:eth0 172.16.4.1 外网:eth 112.112.112.112/24 当有用户访问公网时,修改用 ...

  9. Mac下Qt的环境搭建

    1.分别下载并安装XCode和Command Line Tools(必须安装),安装完毕后,Clang就有了. https://developer.apple.com/downloads/ 2.下载Q ...

  10. VisualSVN server 搭建SVN服务器

    最好用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用.