結果

問題 No.399 動的な領主
ユーザー htensaihtensai
提出日時 2020-06-12 16:02:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,089 ms / 2,000 ms
コード長 2,593 bytes
コンパイル時間 3,163 ms
コンパイル使用メモリ 74,760 KB
実行使用メモリ 133,328 KB
最終ジャッジ日時 2023-09-06 09:19:04
合計ジャッジ時間 15,978 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,236 KB
testcase_01 AC 41 ms
48,984 KB
testcase_02 AC 46 ms
49,320 KB
testcase_03 AC 46 ms
49,248 KB
testcase_04 AC 105 ms
52,812 KB
testcase_05 AC 209 ms
58,796 KB
testcase_06 AC 1,083 ms
110,104 KB
testcase_07 AC 1,070 ms
113,444 KB
testcase_08 AC 1,089 ms
113,956 KB
testcase_09 AC 1,057 ms
109,936 KB
testcase_10 AC 119 ms
50,996 KB
testcase_11 AC 199 ms
58,164 KB
testcase_12 AC 988 ms
116,392 KB
testcase_13 AC 1,016 ms
116,932 KB
testcase_14 AC 733 ms
132,292 KB
testcase_15 AC 757 ms
133,328 KB
testcase_16 AC 778 ms
121,696 KB
testcase_17 AC 1,054 ms
112,956 KB
testcase_18 AC 1,050 ms
109,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	static ArrayList<HashSet<Integer>> graph = new ArrayList<>();
	static int[] costs;
	static int[] depth;
	static int[][] parents;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
		    graph.add(new HashSet<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    graph.get(a).add(b);
		    graph.get(b).add(a);
		}
		costs = new int[n];
		parents = new int[18][n];
		depth = new int[n];
		setParent(0, 0, 0);
		for (int i = 1; i < 18; i++) {
		    for (int j = 0; j < n; j++) {
		        parents[i][j] = parents[i - 1][parents[i - 1][j]];
		    }
		}
		int m = Integer.parseInt(br.readLine());
		for (int i = 0; i < m; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    int lca = getLCA(a, b);
		    costs[a]++;
		    costs[b]++;
		    costs[lca]--;
		    if (lca != 0) {
		        costs[parents[0][lca]]--;
		    }
		}
		ArrayDeque<Integer> deq = new ArrayDeque<>();
		for (int i = 1; i < n; i++) {
		    if (graph.get(i).size() == 1) {
		        deq.add(i);
		    }
		}
		while (deq.size() > 0) {
		    int x = deq.poll();
		    if (x == 0) {
		        continue;
		    }
		    costs[parents[0][x]] += costs[x];
		    graph.get(parents[0][x]).remove(x);
		    if (graph.get(parents[0][x]).size() == 1) {
		        deq.add(parents[0][x]);
		    }
		}
		long total = 0;
		for (int x : costs) {
		    total += (long)x * (x + 1) / 2;
		}
		System.out.println(total);
	}
	
	static void setParent(int idx, int parent, int d) {
	    parents[0][idx] = parent;
	    depth[idx] = d;
	    for (int x : graph.get(idx)) {
	        if (x == parent) {
	            continue;
	        }
	        setParent(x, idx, d + 1);
	    }
	}
	
	static int getLCA(int a, int b) {
	    if (depth[a] > depth[b]) {
	        return getLCA(b, a);
	    }
	    for (int i = 17; i >= 0; i--) {
	        if (depth[b] - depth[a] >= (1 << i)) {
	            b = parents[i][b];
	        }
	    }
	    for (int i = 17; i >= 0; i--) {
	        if (parents[i][a] != parents[i][b]) {
	            a = parents[i][a];
	            b = parents[i][b];
	        }
	    }
	    if (a != b) {
            a = parents[0][a];
            b = parents[0][b];
	    }
	    return a;
	}
}
0