結果

問題 No.399 動的な領主
ユーザー 37zigen37zigen
提出日時 2016-07-16 12:52:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,322 bytes
コンパイル時間 2,281 ms
コンパイル使用メモリ 80,084 KB
実行使用メモリ 106,348 KB
最終ジャッジ日時 2024-04-23 13:41:43
合計ジャッジ時間 25,373 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
54,316 KB
testcase_01 AC 117 ms
53,064 KB
testcase_02 AC 173 ms
54,492 KB
testcase_03 AC 179 ms
54,404 KB
testcase_04 AC 230 ms
57,816 KB
testcase_05 AC 482 ms
61,320 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 230 ms
57,508 KB
testcase_11 AC 459 ms
61,664 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package No300台;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		solver();
	}

	static void solver() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();// the num of vertices;
		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 int[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 int[] 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;
	}

	// Verified ABC014 D,yukicoder No363
	static int MAX_LOG_V;// =(int)log2(MAX_V)+1
	static int MAX_V;
	static int root;// 根ノードの番号
	static int parent[][];// [MAX_LOG_V + 1][MAX_V]
	// parent[k][v] 2^k回親を辿ったときに到達する頂点(根を通り過ぎたときは-1)
	static int[] depth;// [MAX_V] 根からの深さ
	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];

		// parent[0]とdepthを初期化する
		bfs(root, -1, 0);
		// parentを初期化する
		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;
		}
	}

	// dfsだとstack over flow が怖いのでbfs
	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) {
		// uとvの深さが同じになるまで親を辿る
		if (depth[u] > depth[v]) {
			int d = u;
			u = v;
			v = d;
		}
		// depth[v]-depth[u]>=2^kとなる最小のkを求める。
		// つまりuをvと深さが同じか小さいぎりぎりのところまで親を辿る。
		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;
		// uとvが衝突しないように辿る。
		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