Submission #1051754
Source Code Expand
import java.io.*; import java.util.*; public class Main { static final int INF = Integer.MAX_VALUE / 10; BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; List<Integer>[] g; int brute(String s) { int[][] d = new int[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(d[i], INF); d[i][i] = 0; } for (int i = 0; i < n; i++) { for (int j : g[i]) { d[i][j] = 1; } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]); } } // System.err.println(Arrays.deepToString(d)); HashSet<Integer> ans = new HashSet<>(); for (int i = 0; i < n; i++) { if (s.charAt(i) == '0') { continue; } for (int dist = 0; dist < n; dist++) { int mask = 0; for (int j = 0; j < n; j++) { if (d[i][j] <= dist) { mask |= 1 << j; } } ans.add(mask); } } return ans.size(); } int n; int[][] bfs(int v) { int[] d = new int[n]; int[] par = new int[n]; Arrays.fill(d, INF); par[v] = v; int[] que = new int[n]; int head = 0, tail = 0; que[tail++] = v; d[v] = 0; while (head < tail) { v = que[head++]; for (int u : g[v]) { if (d[u] > d[v] + 1) { d[u] = d[v] + 1; que[tail++] = u; par[u] = v; } } } return new int[][] { d, par }; } int[] findCentre() { int[][] info = bfs(0); int v = 0; for (int i = 0; i < n; i++) { if (info[0][i] > info[0][v]) { v = i; } } info = bfs(v); v = 0; for (int i = 0; i < n; i++) { if (info[0][i] > info[0][v]) { v = i; } } int diam = info[0][v]; for (int i = 0; i < diam / 2; i++) { v = info[1][v]; } if (diam % 2 == 0) { return new int[] { v, diam / 2 }; } else { return new int[] { v, info[1][v], diam / 2 + 1 }; } } long solve(int[] vs, int[] us, String s) { g = new List[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int v = vs[i]; int u = us[i]; g[v].add(u); g[u].add(v); } int[] tmp = findCentre(); int[] furth = new int[n]; int[] par = new int[n]; int[] que = new int[n]; int head = 0, tail = 0; Arrays.fill(furth, -1); for (int i = 0; i < tmp.length - 1; i++) { que[tail++] = tmp[i]; furth[tmp[i]] = tmp[tmp.length - 1]; } par[tmp[0]] = tmp[0]; if (tmp.length == 3) { par[tmp[1]] = tmp[0]; } while (head < tail) { int v = que[head++]; for (int u : g[v]) { if (furth[u] == -1) { furth[u] = furth[v] + 1; par[u] = v; que[tail++] = u; } } } int[] depth = new int[n]; int[] high = new int[n]; int[] low = new int[n]; long ans = 1; for (int i = n - 1; i >= 0; i--) { int v = que[i]; low[v] = s.charAt(v) == '1' ? 0 : INF; for (int u : g[v]) { if (u == par[v]) { continue; } depth[v] = Math.max(depth[v], depth[u] + 1); low[v] = Math.min(low[v], low[u] - 1); } high[v] = Math.min(depth[v] + 1, furth[v] - 1); ans += Math.max(high[v] - low[v] + 1, 0); low[v] = Math.max(low[v], high[v] + 1); if (low[v] == furth[v]) { low[v] = INF; } } // if (ans != brute(s)) { // throw new AssertionError(Arrays.toString(g) + " " + s); // } return ans; } void submit() throws IOException { n = nextInt(); int[] vs = new int[n - 1]; int[] us = new int[n - 1]; for (int i = 0; i < n - 1; i++) { vs[i] = nextInt() - 1; us[i] = nextInt() - 1; } String s = nextToken(); out.println(solve(vs, us, s)); } static final Random rng = new Random(); void stress(int n) { this.n = n; int[] vs = new int[n - 1]; int[] us = new int[n - 1]; char[] buf = new char[n]; for (int i = 0; i < n - 1; i++) { vs[i] = i + 1; us[i] = rng.nextInt(i + 1); } for (int i = 0; i < n; i++) { buf[i] = rng.nextBoolean() ? '0' : '1'; } buf[rng.nextInt(n)] = '1'; solve(vs, us, new String(buf)); } Main() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // solve(); submit(); // for (int i = 0; i < Integer.MAX_VALUE; i++) { // System.err.println(i); // stress(25); // } out.close(); } public static void main(String[] args) throws IOException { new Main(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Submission Info
Submission Time | |
---|---|
Task | F - Black Radius |
User | mmaxio |
Language | Java8 (OpenJDK 1.8.0) |
Score | 1900 |
Code Size | 5215 Byte |
Status | AC |
Exec Time | 895 ms |
Memory | 80624 KB |
Compile Error
Note: ./Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Judge Result
Set Name | Sample | Subtask | All | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1300 / 1300 | 600 / 600 | ||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt |
Subtask | 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt, 2_44.txt, 2_45.txt, 2_46.txt, 2_47.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 95 ms | 8276 KB |
0_01.txt | AC | 255 ms | 8148 KB |
0_02.txt | AC | 95 ms | 8276 KB |
1_00.txt | AC | 97 ms | 8144 KB |
1_01.txt | AC | 831 ms | 78784 KB |
1_02.txt | AC | 626 ms | 73956 KB |
1_03.txt | AC | 895 ms | 80592 KB |
1_04.txt | AC | 863 ms | 78952 KB |
1_05.txt | AC | 636 ms | 74124 KB |
1_06.txt | AC | 873 ms | 78168 KB |
1_07.txt | AC | 850 ms | 78188 KB |
1_08.txt | AC | 825 ms | 77728 KB |
1_09.txt | AC | 837 ms | 77612 KB |
1_10.txt | AC | 624 ms | 72712 KB |
1_11.txt | AC | 825 ms | 79752 KB |
1_12.txt | AC | 626 ms | 71876 KB |
1_13.txt | AC | 882 ms | 79776 KB |
1_14.txt | AC | 835 ms | 79164 KB |
1_15.txt | AC | 614 ms | 72508 KB |
1_16.txt | AC | 823 ms | 79468 KB |
1_17.txt | AC | 842 ms | 78664 KB |
1_18.txt | AC | 841 ms | 79336 KB |
1_19.txt | AC | 848 ms | 79636 KB |
1_20.txt | AC | 648 ms | 73740 KB |
1_21.txt | AC | 864 ms | 80624 KB |
1_22.txt | AC | 615 ms | 73608 KB |
1_23.txt | AC | 880 ms | 77884 KB |
1_24.txt | AC | 834 ms | 79612 KB |
1_25.txt | AC | 830 ms | 78592 KB |
1_26.txt | AC | 807 ms | 79460 KB |
1_27.txt | AC | 838 ms | 78364 KB |
1_28.txt | AC | 845 ms | 78208 KB |
1_29.txt | AC | 817 ms | 78112 KB |
2_00.txt | AC | 98 ms | 8144 KB |
2_01.txt | AC | 846 ms | 78500 KB |
2_02.txt | AC | 832 ms | 78552 KB |
2_03.txt | AC | 831 ms | 78952 KB |
2_04.txt | AC | 833 ms | 78140 KB |
2_05.txt | AC | 846 ms | 78328 KB |
2_06.txt | AC | 825 ms | 78052 KB |
2_07.txt | AC | 826 ms | 78360 KB |
2_08.txt | AC | 886 ms | 77948 KB |
2_09.txt | AC | 855 ms | 79756 KB |
2_10.txt | AC | 861 ms | 78552 KB |
2_11.txt | AC | 863 ms | 78844 KB |
2_12.txt | AC | 885 ms | 78704 KB |
2_13.txt | AC | 854 ms | 77884 KB |
2_14.txt | AC | 858 ms | 79452 KB |
2_15.txt | AC | 863 ms | 79300 KB |
2_16.txt | AC | 689 ms | 72836 KB |
2_17.txt | AC | 843 ms | 79112 KB |
2_18.txt | AC | 885 ms | 77932 KB |
2_19.txt | AC | 856 ms | 79044 KB |
2_20.txt | AC | 847 ms | 78128 KB |
2_21.txt | AC | 855 ms | 78420 KB |
2_22.txt | AC | 856 ms | 78152 KB |
2_23.txt | AC | 840 ms | 77620 KB |
2_24.txt | AC | 634 ms | 72600 KB |
2_25.txt | AC | 843 ms | 80420 KB |
2_26.txt | AC | 855 ms | 78084 KB |
2_27.txt | AC | 833 ms | 78820 KB |
2_28.txt | AC | 870 ms | 77860 KB |
2_29.txt | AC | 859 ms | 77868 KB |
2_30.txt | AC | 867 ms | 78040 KB |
2_31.txt | AC | 830 ms | 79432 KB |
2_32.txt | AC | 854 ms | 78004 KB |
2_33.txt | AC | 843 ms | 79596 KB |
2_34.txt | AC | 618 ms | 72372 KB |
2_35.txt | AC | 619 ms | 72336 KB |
2_36.txt | AC | 851 ms | 77540 KB |
2_37.txt | AC | 838 ms | 77560 KB |
2_38.txt | AC | 870 ms | 78336 KB |
2_39.txt | AC | 891 ms | 79892 KB |
2_40.txt | AC | 861 ms | 79292 KB |
2_41.txt | AC | 844 ms | 79556 KB |
2_42.txt | AC | 845 ms | 78292 KB |
2_43.txt | AC | 859 ms | 78192 KB |
2_44.txt | AC | 836 ms | 78296 KB |
2_45.txt | AC | 623 ms | 74340 KB |
2_46.txt | AC | 864 ms | 78664 KB |
2_47.txt | AC | 870 ms | 78996 KB |