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 (≤104) 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

交了一发暴力,居然过了,关键就是这个求树的深度,这题的主要坑点在于,如果不是一颗树要输出那个信息,如果成环了但是只有一个连通块,要输出

Error: 1 components

所以注意判断逻辑

int dfs(int x)
{
//de(x);
vis[x]=1;
int ma=0;
for(int i=0;i<(int)G[x].size();i++){
int v=G[x][i];
if(vis[v])continue;
ma=max(ma,dfs(G[x][i]));
}
return ma+1;
}
#include <iostream>
#include<bits/stdc++.h>
#define each(a,b,c) for(int a=b;a<=c;a++)
#define de(x) cout<<#x<<" "<<(x)<<endl
using namespace std; const int maxn=1e4+5;
int father[maxn]; // 储存i的father父节点 void makeSet(int n) {
for (int i = 1; i <=n; i++)
father[i] = i;
} int findRoot(int x) { // 迭代找根节点
int root = x; // 根节点
while (root != father[root]) { // 寻找根节点
root = father[root];
}
while (x != root) {
int tmp = father[x];
father[x] = root; // 根节点赋值
x = tmp;
}
return root;
} void Union(int x, int y) { // 将x所在的集合和y所在的集合整合起来形成一个集合。
int a, b;
a = findRoot(x);
b = findRoot(y);
father[a] = b; // y连在x的根节点上 或father[b] = a为x连在y的根节点上;
}
vector<int>G[maxn];
/*
5
1 2
1 3
1 4
2 5
*/
int maxx;
int depth[maxn];
int vis[maxn];
int dfs(int x)
{
//de(x);
vis[x]=1;
int ma=0;
for(int i=0;i<(int)G[x].size();i++){
int v=G[x][i];
if(vis[v])continue;
ma=max(ma,dfs(G[x][i]));
}
return ma+1;
}
int main()
{
int n;
cin>>n;
makeSet(n);
int m=n-1;
int a,b;
int tree_flag=true;
while(m--)
{
scanf("%d%d",&a,&b);
if(findRoot(a)!=findRoot(b)){
Union(a,b);
G[a].push_back(b);
G[b].push_back(a);
}
else tree_flag=false; }
//int components=0;
set<int>s;
for(int i=1;i<=n;i++)
{
//de(i);
//de(findRoot(i));
s.insert(findRoot(i));
}
//de(s.size());
if(tree_flag==false||s.size()!=1)
{
printf("Error: %d components\n",(int)s.size());
return 0;
}
maxx=0;
each(i,1,n)
{
memset(vis,0,sizeof(vis));
//de(i);
depth[i]=dfs(i);
}
each(i,1,n)
{
maxx=max(maxx,depth[i]);
}
each(i,1,n)
{
if(depth[i]==maxx)
{
cout<<i<<endl;
}
} return 0;
}

PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度的更多相关文章

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

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

  2. PAT甲题题解-1021. Deepest Root (25)-dfs+并查集

    dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

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

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

  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 分)(暴力,DFS)

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

  7. PAT 1021 Deepest Root[并查集、dfs][难]

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

  8. PAT (Advanced Level) 1021. Deepest Root (25)

    先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #i ...

  9. 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. button按钮的状态为disabled禁用状态,click事件无法触发,但是为什么touchstart下却依然可以触发

    切换到移动模拟模式,并点击按钮,查看控制台. 发现click没有事件没有触发,而touch事件依然触发. 解决办法: 对于移动端我们使用css来禁止按钮,达到disable的效果: 对,就是这个神奇的 ...

  2. [转]【Windows小技巧】批量重命名文件

    注:如果文件名包含空格,命令应写成ren "s0 (1).gif" s001.gif,简而言之,就是加上双引号!!!原因:系统将s0和(1).gif认为是两个参数,再加上后面的s0 ...

  3. libfacedetection

    libfacedetection测试 #include <stdio.h> #include <opencv2/opencv.hpp> #include <facedet ...

  4. 使用HSQLDB 客户端(jvm自带数据库使用技巧)

    数据库连接jar包 http://how2j.cn/frontdownload?bean.id=1169 hsqldb.jarservlet-2_3-fcs-classfiles.zipsqltool ...

  5. Egret中的对象池Pool

    为了可以让对象复用,防止大量重复创建对象,导致资源浪费,使用对象池来管理. 一 对象池A 二 对象池B 一 对象池A 1. 支持传入构造函数 2. 支持预先创建对象 3. 支持统一执行函数 /** * ...

  6. ios开发注意事项小总结

    一.LaunchScreen LaunchScreen产生原因:代替之前的启动图片 好处: 1.可以展示更多的东西 2.可以只需要出一个尺寸的图片. 启动图片的优先级 启动图片 < Launch ...

  7. 【相机篇】从到FlyCapture2到Spinnaker

    从FlyCapture2 到 Spinnaker SDK的变换,可参见FLIR公司机器视觉的相机产品:https://www.flir.com/iis/machine-vision/ Spinnake ...

  8. 【helm & Tiller】报错Error: incompatible versions client[v2.14.1] server[v2.13.0] │

    helm是helm的客户端部分 tiller是helm的服务器端部分 报错 报错Error: incompatible versions client[v2.14.1] server[v2.13.0] ...

  9. VUE中事件修饰符:stop prevent self capture

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  10. 09点睛Spring4.1-AOP

    9.1 AOP AOP可以了让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,这样使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上; 下面演示一个日志系统的实现,简单但不 ...