結果

問題 No.399 動的な領主
ユーザー 37zigen37zigen
提出日時 2016-07-16 13:04:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,861 ms / 2,000 ms
コード長 2,784 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 80,104 KB
実行使用メモリ 92,836 KB
最終ジャッジ日時 2024-04-25 08:55:11
合計ジャッジ時間 23,174 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
41,320 KB
testcase_01 AC 125 ms
41,340 KB
testcase_02 AC 167 ms
42,120 KB
testcase_03 AC 175 ms
41,948 KB
testcase_04 AC 215 ms
46,364 KB
testcase_05 AC 447 ms
50,160 KB
testcase_06 AC 1,447 ms
86,736 KB
testcase_07 AC 1,861 ms
86,700 KB
testcase_08 AC 1,412 ms
86,332 KB
testcase_09 AC 1,667 ms
86,084 KB
testcase_10 AC 257 ms
46,276 KB
testcase_11 AC 483 ms
50,228 KB
testcase_12 AC 1,665 ms
86,700 KB
testcase_13 AC 1,339 ms
87,296 KB
testcase_14 AC 1,647 ms
92,092 KB
testcase_15 AC 1,762 ms
92,836 KB
testcase_16 AC 1,523 ms
91,216 KB
testcase_17 AC 1,423 ms
85,600 KB
testcase_18 AC 1,509 ms
88,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No300台;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Scanner;
public class Main implements Runnable{
	public static void main(String[] args) {
		new Thread(null, new Main(), "", 1024L * 1024 * 100).start();
	}
	@Override
	public void run() {
		
		solver();
		
	}

	
	static void solver() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		edges = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			edges[i] = new ArrayList<>();
		}
		for (int i = 0; i < n - 1; i++) {
			int u = sc.nextInt() - 1;
			int v = sc.nextInt() - 1;
			edges[u].add(v);
			edges[v].add(u);
		}
		init(n);
		accum = new long[n];

		int q = sc.nextInt();

		for (int i = 0; i < q; i++) {
			int a = sc.nextInt() - 1;
			int b = sc.nextInt() - 1;
			int lca = lca(a, b);
			accum[a]++;
			accum[b]++;
			accum[lca]--;
			if (parent[0][lca] != -1) {
				accum[parent[0][lca]]--;
			}
		}
		dfs(-1,0);
		System.out.println(sum);
	}

	static long sum;
	static long[] accum;

	static void dfs(int pre, int cur) {
		for (int v : edges[cur]) {
			if (v != pre)
				dfs(cur, v);
		}
		for (int v : edges[cur]) {
			if (v != pre) {
				accum[cur] += accum[v];
			}
		}
		sum += accum[cur] * (accum[cur] + 1) / 2;
	}

	static int MAX_LOG_V;
	static int MAX_V;
	static int root;
	static int parent[][];
	static int[] depth;
	static ArrayList<Integer> edges[];

	static void init(int N) {
		MAX_V = N;
		MAX_LOG_V = (int) (Math.log(MAX_V) / Math.log(2)) + 1;
		root = 0;
		parent = new int[MAX_LOG_V + 1][MAX_V];
		depth = new int[MAX_V];

		bfs(root, -1, 0);
		for (int k = 0; k < MAX_LOG_V; k++) {
			for (int v = 0; v < MAX_V; v++) {
				if (parent[k][v] < 0) {
					parent[k + 1][v] = -1;
				} else {
					parent[k + 1][v] = parent[k][parent[k][v]];
				}
			}
		}
	}

	static class P {
		int parent;
		int me;
		int depth;

		P(int me, int parent, int depth) {
			this.me = me;
			this.parent = parent;
			this.depth = depth;
		}
	}

	static void bfs(int v, int p, int d) {
		ArrayDeque<P> q = new ArrayDeque<P>();
		q.add(new P(v, p, d));
		while (!q.isEmpty()) {
			P u = q.poll();
			parent[0][u.me] = u.parent;
			depth[u.me] = u.depth;
			for (int i = 0; i < edges[u.me].size(); i++) {
				if (edges[u.me].get(i) != u.parent)
					q.add(new P(edges[u.me].get(i), u.me, u.depth + 1));
			}
		}
	}

	static int lca(int u, int v) {
		if (depth[u] > depth[v]) {
			int d = u;
			u = v;
			v = d;
		}
		for (int k = 0; k < MAX_LOG_V; k++) {
			if ((((depth[v] - depth[u]) >> k) & 1) == 1) {
				v = parent[k][v];
			}
		}
		if (u == v)
			return u;
		for (int k = MAX_LOG_V - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v] && parent[k][u] != -1 && parent[k][v] != -1) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
}
0