Submission #70268852
Source Code Expand
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class Main implements Runnable {
int n;
List<List<Integer>> g = new ArrayList<>();
int[][] end;
int[] res;
int cmp(int[] a, int[] b) {
int c = Integer.compare(a[0], b[0]);
if (c != 0) {
return c;
} else {
return Integer.compare(a[1], b[1]);
}
}
void dfs1(int i, int p) {
end[0][i] = -1;
end[1][i] = i;
for (Integer ch : g.get(i)) {
if (ch == p) continue;
dfs1(ch, i);
if (end[0][ch] > end[0][i] || (end[0][ch] == end[0][i] && end[1][ch] > end[1][i])) {
end[0][i] = end[0][ch];
end[1][i] = end[1][ch];
}
}
end[0][i]++;
}
void dfs2(int i, int p, int pend0, int pend1) {
// int[][] mx = new int[2][2];
// mx[0][0] = pend0;
// mx[0][1] = pend1;
int mx00 = pend0;
int mx01 = pend1;
int mx10 = 0;
int mx11 = 0;
for (Integer ch : g.get(i)) {
if (ch == p) continue;
// if (cmp(end[ch], mx[0]) > 0) {
// mx[1] = mx[0];
// mx[0] = end[ch];
// } else if (cmp(end[ch], mx[1]) > 0) {
// mx[1] = end[ch];
// }
if (end[0][ch] > mx00 || (end[0][ch] == mx00 && end[1][ch] > mx01)) {
mx10 = mx00;
mx11 = mx01;
mx00 = end[0][ch];
mx01 = end[1][ch];
} else if (end[0][ch] > mx10 || (end[0][ch] == mx10 && end[1][ch] > mx11)) {
mx10 = end[0][ch];
mx11 = end[1][ch];
}
}
res[i] = mx01 + 1;
// int[] np1 = new int[] {mx10 + 1, mx11};
// int[] np2 = new int[] {mx00 + 1, mx01};
for (Integer ch : g.get(i)) {
if (ch == p) continue;
if (mx01 == end[1][ch]) {
dfs2(ch, i, mx10 + 1, mx11);
} else {
dfs2(ch, i, mx00 + 1, mx01);
}
}
}
public void solve() {
n = nextInt();
end = new int[2][n];
res = new int[n];
for (int i = 0; i < n; i++) {
g.add(new ArrayList<>());
}
for (int i = 0; i < n - 1; i++) {
int a = nextInt() - 1, b = nextInt() - 1;
g.get(a).add(b);
g.get(b).add(a);
}
dfs1(0, -1);
dfs2(0, -1, -1, 0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(res[i]);
if (i < n - 1) sb.append("\n");
}
out.println(sb);
}
public static void main(String[] args) throws Exception {
new Thread(null, new Main(), "CustomThread", 1024 * 1024 * 100).start();
}
@Override
public void run() {
int t = 1;
for (int i = 0; i < t; i++) {
new Main().solve();
}
out.flush();
}
static PrintWriter out = new PrintWriter(System.out, false);
static InputReader in = new InputReader(System.in);
static String next() { return in.next(); }
static int nextInt() { return Integer.parseInt(in.next()); }
static long nextLong() { return Long.parseLong(in.next()); }
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
}
}
Submission Info
| Submission Time |
|
| Task |
E - Farthest Vertex |
| User |
dwx2 |
| Language |
Java (OpenJDK 17) |
| Score |
0 |
| Code Size |
4179 Byte |
| Status |
TLE |
| Exec Time |
2219 ms |
| Memory |
194620 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 475 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_00.txt, 00_sample_01.txt |
| All |
00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 02_path_00.txt, 02_path_01.txt, 02_path_02.txt, 02_path_03.txt, 03_star_1_00.txt, 03_star_1_01.txt, 03_star_1_02.txt, 04_star_2_00.txt, 04_star_2_01.txt, 04_star_2_02.txt, 04_star_2_03.txt, 04_star_2_04.txt, 05_star_3_00.txt, 05_star_3_01.txt, 05_star_3_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 06_corner_03.txt, 06_corner_04.txt, 06_corner_05.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_00.txt |
AC |
43 ms |
34744 KiB |
| 00_sample_01.txt |
AC |
43 ms |
34804 KiB |
| 01_random_00.txt |
AC |
711 ms |
96804 KiB |
| 01_random_01.txt |
AC |
1108 ms |
135832 KiB |
| 01_random_02.txt |
AC |
881 ms |
115912 KiB |
| 01_random_03.txt |
AC |
1118 ms |
133960 KiB |
| 01_random_04.txt |
AC |
893 ms |
118940 KiB |
| 01_random_05.txt |
AC |
1099 ms |
136544 KiB |
| 01_random_06.txt |
AC |
1094 ms |
134480 KiB |
| 01_random_07.txt |
AC |
1110 ms |
135992 KiB |
| 02_path_00.txt |
TLE |
2218 ms |
192320 KiB |
| 02_path_01.txt |
TLE |
2216 ms |
162876 KiB |
| 02_path_02.txt |
TLE |
2216 ms |
156644 KiB |
| 02_path_03.txt |
TLE |
2216 ms |
167116 KiB |
| 03_star_1_00.txt |
AC |
595 ms |
125588 KiB |
| 03_star_1_01.txt |
AC |
695 ms |
144524 KiB |
| 03_star_1_02.txt |
AC |
1080 ms |
140680 KiB |
| 04_star_2_00.txt |
AC |
612 ms |
136464 KiB |
| 04_star_2_01.txt |
AC |
1123 ms |
145176 KiB |
| 04_star_2_02.txt |
AC |
1220 ms |
134120 KiB |
| 04_star_2_03.txt |
AC |
1714 ms |
152288 KiB |
| 04_star_2_04.txt |
TLE |
2219 ms |
165904 KiB |
| 05_star_3_00.txt |
AC |
1196 ms |
137380 KiB |
| 05_star_3_01.txt |
AC |
1234 ms |
137296 KiB |
| 05_star_3_02.txt |
AC |
1170 ms |
138148 KiB |
| 06_corner_00.txt |
AC |
1576 ms |
177748 KiB |
| 06_corner_01.txt |
AC |
1615 ms |
194620 KiB |
| 06_corner_02.txt |
TLE |
2218 ms |
161708 KiB |
| 06_corner_03.txt |
TLE |
2216 ms |
161824 KiB |
| 06_corner_04.txt |
AC |
1512 ms |
172236 KiB |
| 06_corner_05.txt |
AC |
1432 ms |
172676 KiB |