1021 Deepest Root (25 分)
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
题意:
给定一个图,N个节点,N-1条边,判断他是不是树,如果是,找到深度最深的那个根节点,如果不是,找到这个图有几个连通分量。
思路:
判断一个图是不是树:1、N个节点是不是有N-1条边,2.如果有N-1条边,则bfs或者dfs这个图,看有几个连通分量,如果只有一个,则就是树。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int N;
static LinkedList<Integer>[] G;
static int find[];
static boolean collected[];
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
N = (int) in.nval;
G = new LinkedList[N];
collected = new boolean[N];
for (int i = 0; i < N; i++) {
G[i] = new LinkedList<Integer>();
}
for (int i = 0; i < N - 1; i++) {
in.nextToken();
int x = (int) in.nval;
in.nextToken();
int y = (int) in.nval;
G[x - 1].add(y - 1);
G[y - 1].add(x - 1);
}
int count = 0;
for (int i = 0; i < N; i++) {
if (collected[i] == false) {
bfs(i + 1);
count++;
}
}
if (count != 1) {
System.out.printf("Error: %d components", 2);
} else {
// floyd();
bfs2();
}
}
private static void bfs2() {
// TODO Auto-generated method stub
int level[] = new int[N];
int max=-1;
for (int i = 0; i < N; i++) {
level[i] = countlevel(i);
if(level[i]>max)
max=level[i];
}
for(int i=0;i<N;i++) {
if(level[i]==max)
System.out.println(i+1);
}
}
private static int countlevel(int root) {
// TODO Auto-generated method stub
Queue<Integer> qu = new LinkedList<>();
boolean []bool = new boolean[N];
qu.add(root);
bool[root] = true;
qu.add(-1);
int level = 0;
while (!qu.isEmpty()) {
int num = qu.poll();
if (num == -1) {
level++;
if (!qu.isEmpty())
qu.add(-1);
continue;
}
for (int i = 0; i < G[num].size(); i++) {
if (bool[G[num].get(i)]==false) {
qu.add(G[num].get(i));
bool[G[num].get(i)]=true;
}
}
}
return level;
}
private static void bfs(int root) {
// TODO Auto-generated method stub
Queue<Integer> qu = new LinkedList<>();
qu.add(root - 1);
collected[root - 1] = true;
while (!qu.isEmpty()) {
int num = qu.poll();
for (int i = 0; i < N; i++) {
if (G[num].contains(i) && num != i && collected[i] == false) {
collected[i] = true;
qu.add(i);
}
}
}
}
}