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.
每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正整数N(≤104),它是节点的数量,因此节点的编号从1到N。接下来是N−1行,每行通过给定两个相邻节点的编号来描述一条边。
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.
对于每个测试用例,在一行中打印每个最深的根。如果这样的根不是唯一的,请按编号的升序打印。如果给定的图形不是树,请打印错误:K组件,其中K是图形中连通分量的数量。
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
思路:本题就是深度优先搜索dfs 判断有几个连通分量,最大高度,很遗憾,有一个测试点运行超时
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Collections;
class Main{
//邻接表,记录每个结点的邻居结点编号
static ArrayList<ArrayList<Integer>> v = new ArrayList<>();
//记录每个结点是否有被访问过
static int []visited;
//存储高度最高的根结点
static ArrayList<Integer> farthest_point = new ArrayList<>();
//记录最大高度
static int maxdis = 0;
public static void main(String[] args)throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(bf);
in.nextToken();
int n = (int)in.nval;
//因为编号从1开始,所以多创建一个
visited = new int[n + 1];
//创建对象
for(int i = 0 ; i < n + 1 ;i ++)
{
ArrayList<Integer> arr = new ArrayList<>();
v.add(arr);
}
for(int i = 1 ; i < n ;i++)
{
in.nextToken();
int x = (int)in.nval;
in.nextToken();
int y = (int)in.nval;
v.get(x).add(y);
v.get(y).add(x);
}
int count = 0;
//先判断有几个连通分量
for(int i = 1 ;i < n+1 ;i ++)
{
if(visited[i] == 0)
{
dfs(i);
count ++;
}
}
//不只有一个连通分量,一定不是树,一定有环,因为只给了n个结点,n-1条边
if(count != 1)
{
System.out.println("Error: " + count + " components");
return ;
}
//是树
else
{
for(int i = 1 ; i <= n ; i ++)
{
for(int j = 1 ; j <= n ; j ++)
{
visited[j] = 0;
}
dfs2(i,0);
}
}
Collections.sort(farthest_point);
for(int k : farthest_point)
{
System.out.println(k);
}
}
//两个dfs实现的功能不同
private static void dfs(int cur) {
if(visited[cur] == 1) return ;
visited[cur] = 1;
for(int i:v.get(cur))
{
dfs(i);
}
}
public static void dfs2(int cur,int depth)
{
if(visited[cur] == 1) return ;
visited[cur] = 1;
if(depth > maxdis)
{
maxdis = depth;
farthest_point.clear();
if(!farthest_point.contains(cur))
{
farthest_point.add(cur);
}
}
else if(depth == maxdis)
{
if(!farthest_point.contains(cur))
{
farthest_point.add(cur);
}
}
for(int i : v.get(cur))
{
dfs2(i , depth + 1);
}
}
}