結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-22 07:10:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 2,525 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 172,800 KB
実行使用メモリ 18,176 KB
最終ジャッジ日時 2024-04-25 09:10:16
合計ジャッジ時間 3,243 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 86 ms
13,464 KB
testcase_07 AC 90 ms
13,584 KB
testcase_08 AC 92 ms
13,648 KB
testcase_09 AC 86 ms
13,648 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 68 ms
14,244 KB
testcase_13 AC 65 ms
14,096 KB
testcase_14 AC 56 ms
18,048 KB
testcase_15 AC 58 ms
18,176 KB
testcase_16 AC 56 ms
15,616 KB
testcase_17 AC 85 ms
13,524 KB
testcase_18 AC 84 ms
13,652 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:105:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  105 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:117:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  117 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct HLDecomposition {
	const vector<vector<int>> &g;
	vector<int> vid, head, heavy, parent;
	vector<int> light_depth;
	vector<vector<int>> light_parent;

	HLDecomposition(vector<vector<int>> &g) :
		g(g),
		vid(g.size()),
		head(g.size()),
		heavy(g.size(), -1),
		parent(g.size()),
		light_depth(g.size()),
		light_parent(5, vector<int>(g.size())) {
		dfs(0, -1);
		bfs();

		for (int i = 0; i < g.size(); i++) {
			light_parent[0][i] = parent[head[i]];
		}
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < g.size(); j++) {
				if (light_parent[i][j] != -1) {
					light_parent[i + 1][j] = light_parent[i][light_parent[i][j]];
				} else {
					light_parent[i + 1][j] = -1;
				}
			}
		}
	}

	int lca(int u, int v) {
		if (light_depth[u] < light_depth[v]) swap(u, v);
		for (int i = 4; i >= 0; i--) {
			if (light_depth[u] - light_depth[v] >= 1 << i) {
				u = light_parent[i][u];
			}
		}
		for (int i = 4; i >= 0; i--) {
			if (light_parent[i][u] != light_parent[i][v]) {
				u = light_parent[i][u];
				v = light_parent[i][v];
			}
		}
		if (head[u] == head[v]) {
			return vid[u] < vid[v] ? u : v;
		} else {
			return light_parent[0][u];
		}
	}

	int dfs(int curr, int prev) {
		parent[curr] = prev;
		int sub = 1, max_sub = 0;
		for (int next : g[curr]) if (next != prev) {
			int s = dfs(next, curr);
			sub += s;
			if (max_sub < s) max_sub = s, heavy[curr] = next;
		}
		return sub;
	}

	void bfs() {
		int k = 0;
		queue<int> q({ 0 });
		while (!q.empty()) {
			int h = q.front(); q.pop();
			for (int i = h; i != -1; i = heavy[i]) {
				light_depth[i] = light_depth[h];
				vid[i] = k++;
				head[i] = h;
				for (int j : g[i]) if (j != parent[i] && j != heavy[i]) {
					light_depth[j] = light_depth[i] + 1;
					q.push(j);
				}
			}
		}
	}
};

vector<vector<int>> g;
long long imos[101010];
long long ans;

void dfs(int curr, int prev) {
	for (int next : g[curr]) if (next != prev) {
		dfs(next, curr);
	}
	if (prev != -1) {
		imos[prev] += imos[curr];
	}
	ans += imos[curr] * (imos[curr] + 1) / 2;
}

int main() {
	int n;
	cin >> n;
	g.resize(n);

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	HLDecomposition hl(g);

	int Q;
	cin >> Q;
	while (Q--) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--;
		v--;
		int l = hl.lca(u, v);

		imos[u]++;
		imos[v]++;
		imos[l]--;
		if (hl.parent[l] != -1) {
			imos[hl.parent[l]]--;
		}
	}

	dfs(0, -1);
	cout << ans << endl;
}
0