結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-15 23:56:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,676 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 177,508 KB
実行使用メモリ 49,572 KB
最終ジャッジ日時 2024-04-25 08:47:20
合計ジャッジ時間 3,706 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 3 ms
6,016 KB
testcase_02 AC 3 ms
6,016 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 4 ms
6,272 KB
testcase_05 AC 12 ms
9,216 KB
testcase_06 AC 133 ms
44,808 KB
testcase_07 AC 139 ms
44,672 KB
testcase_08 AC 127 ms
44,796 KB
testcase_09 AC 131 ms
44,652 KB
testcase_10 AC 4 ms
6,272 KB
testcase_11 AC 11 ms
9,344 KB
testcase_12 AC 102 ms
45,672 KB
testcase_13 AC 102 ms
45,504 KB
testcase_14 AC 91 ms
49,572 KB
testcase_15 AC 89 ms
49,316 KB
testcase_16 AC 86 ms
47,016 KB
testcase_17 AC 131 ms
44,908 KB
testcase_18 AC 122 ms
44,652 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:64:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:77:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   77 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

struct LCA {
	vector<int> vid;
	vector<vector<int>> g;
	vector<vector<pair<int, int>>> mn;

	LCA(int n) : g(n), vid(n), mn(1) {}

	void add(int u, int v) {
		g[u].push_back(v);
		g[v].push_back(u);
	}

	void build() {
		dfs(0, -1, 0);
		int m = log2(mn[0].size());
		mn.resize(m + 1, vector<pair<int, int>>(mn[0].size()));
		for (int i = 0; i < m; i++) {
			for (int j = 0; j + (1 << i) < mn[i].size(); j++) {
				mn[i + 1][j] = min(mn[i][j], mn[i][j + (1 << i)]);
			}
		}
	}

	void dfs(int curr, int prev, int d) {
		vid[curr] = mn[0].size();
		mn[0].emplace_back(d, curr);
		for (int next : g[curr]) if (next != prev) {
			dfs(next, curr, d + 1);
			mn[0].emplace_back(d, curr);
		}
	}

	int query(int u, int v) {
		int l, r;
		tie(l, r) = minmax(vid[u], vid[v]);
		int k = 31 - __builtin_clz(r + 1 - l);
		return min(mn[k][l], mn[k][r + 1 - (1 << k)]).second;
	}
};

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

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

int main() {
	int n;
	cin >> n;

	LCA lca(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);
		lca.add(u, v);
	}
	lca.build();

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

		imos[u]++;
		imos[v]++;
		imos[l] -= 2;
		part[l]++;
	}

	dfs(0, -1);
	long long ans = 0;
	for (int i = 0; i < n; i++) {
		long long v = imos[i] + part[i];
		ans += v * (v + 1) / 2;
	}
	cout << ans << endl;
}
0