|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class Main { |
| 51 | + |
| 52 | + static IOController io; |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + static int N; |
| 57 | + static int[] p; |
| 58 | + static long[] s; |
| 59 | + static int[] r; |
| 60 | + static int[][] par; |
| 61 | + static int[] dep; |
| 62 | + static List<Integer>[] graph; |
| 63 | + static List<int[]> edges; |
| 64 | + |
| 65 | + static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));} |
| 66 | + |
| 67 | + public static void main(String[] args) throws Exception { |
| 68 | + |
| 69 | + io = new IOController(); |
| 70 | + |
| 71 | + init(); |
| 72 | + solve(); |
| 73 | + |
| 74 | + io.close(); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + static void init() throws Exception { |
| 79 | + |
| 80 | + N = io.nextInt(); |
| 81 | + p = new int[N+1]; |
| 82 | + s = new long[N+1]; |
| 83 | + r = new int[N+1]; |
| 84 | + par = new int[N+1][18]; |
| 85 | + graph = new List[N+1]; |
| 86 | + dep = new int[N+1]; |
| 87 | + for(int i=1;i<=N;i++) { |
| 88 | + p[i] = io.nextInt(); |
| 89 | + r[i] = i; |
| 90 | + graph[i] = new ArrayList<>(); |
| 91 | + } |
| 92 | + edges = new ArrayList<>(); |
| 93 | + for(int i=1;i<N;i++) { |
| 94 | + int a = p[io.nextInt()]; |
| 95 | + int b = p[io.nextInt()]; |
| 96 | + edges.add(new int[]{Math.min(a,b), Math.max(a,b)}); |
| 97 | + graph[a].add(b); |
| 98 | + graph[b].add(a); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + static void solve() throws Exception { |
| 104 | + |
| 105 | + dfs(N,0,0); |
| 106 | + for(int k=1;k<18;k++) for(int i=1;i<=N;i++) par[i][k] = par[par[i][k-1]][k-1]; |
| 107 | + |
| 108 | + Collections.sort(edges, (a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]); |
| 109 | + for(int[] edge : edges) { |
| 110 | + int a = edge[0], b = edge[1]; |
| 111 | + int x = f(a), y = f(b); |
| 112 | + s[y] = Math.max(s[y], s[x] + dist(x, y)); |
| 113 | + r[x] = y; |
| 114 | + } |
| 115 | + io.write(s[N] + "\n"); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + static void dfs(int n, int p, int d) { |
| 120 | + par[n][0] = p; |
| 121 | + dep[n] = d; |
| 122 | + for(int i:graph[n]) if(i != p) dfs(i,n,d+1); |
| 123 | + } |
| 124 | + |
| 125 | + static int dist(int a, int b) { |
| 126 | + int diff = Math.abs(dep[a]-dep[b]); |
| 127 | + int res = 0; |
| 128 | + for(int k=0;k<18;k++) if((diff & (1<<k)) != 0) { |
| 129 | + if(dep[a] > dep[b]) a = par[a][k]; |
| 130 | + else b = par[b][k]; |
| 131 | + res += (1<<k); |
| 132 | + } |
| 133 | + |
| 134 | + for(int k=17;k>=0;k--) if(par[a][k] != par[b][k]) { |
| 135 | + res += (1<<(k+1)); |
| 136 | + a = par[a][k]; |
| 137 | + b = par[b][k]; |
| 138 | + } |
| 139 | + if(a != b) res += 2; |
| 140 | + return res; |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | +``` |
0 commit comments