forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.java
85 lines (72 loc) · 2.61 KB
/
A.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.PrintStream;
import java.util.*;
/**
* KickStart 2018 Round C
* Problem A. Planet Distance
*/
public class Main {
private String solve(Scanner scanner) {
int n=scanner.nextInt();
ArrayList<Integer>[] lists = new ArrayList[n];
for (int i=0;i<n;i++) lists[i]=new ArrayList<>();
for (int i=0;i<n;i++) {
int x=scanner.nextInt()-1, y=scanner.nextInt()-1;
lists[x].add(y);
lists[y].add(x);
}
int[] visited=new int[n]; visited[0]=1;
boolean[] circle = findCircle(n, lists, 0, -1, new int[n], visited);
int[] distance = new int[n];
Arrays.fill(distance, Integer.MAX_VALUE/10);
for (int i=0;i<n;i++) {
if (!circle[i]) continue;
distance[i]=0;
Queue<Integer> queue=new LinkedList<>();
queue.offer(i);
while (!queue.isEmpty()) {
int curr = queue.poll();
for (int v: lists[curr]) {
if (distance[v]>distance[curr]+1) {
distance[v]=distance[curr]+1;
queue.offer(v);
}
}
}
}
return String.join(" ", Arrays.stream(distance).boxed().map(String::valueOf).collect(Collectors.toList()));
}
private boolean[] findCircle(int n, ArrayList<Integer>[] lists, int curr, int last, int[] father, int[] visited) {
for (int v: lists[curr]) {
if (v==last) continue;
if (visited[v]==1) {
// found!
boolean[] circle = new boolean[n];
circle[v]=true;
while (curr!=v) {
circle[curr]=true;
curr=father[curr];
}
return circle;
}
father[v]=curr;
if (visited[v]==0) {
visited[v]=1;
boolean[] find = findCircle(n, lists, v, curr, father, visited);
if (find!=null) return find;
visited[v]=2;
}
}
return null;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}