結果

問題 No.399 動的な領主
ユーザー pekempeypekempey
提出日時 2016-07-19 11:30:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 2,694 bytes
コンパイル時間 1,731 ms
コンパイル使用メモリ 169,984 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-04-25 09:06:43
合計ジャッジ時間 4,153 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,888 KB
testcase_01 AC 3 ms
5,888 KB
testcase_02 AC 4 ms
5,888 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 5 ms
6,016 KB
testcase_05 AC 12 ms
6,812 KB
testcase_06 AC 153 ms
17,024 KB
testcase_07 AC 158 ms
17,152 KB
testcase_08 AC 145 ms
17,280 KB
testcase_09 AC 146 ms
17,280 KB
testcase_10 AC 4 ms
6,016 KB
testcase_11 AC 11 ms
7,060 KB
testcase_12 AC 91 ms
18,176 KB
testcase_13 AC 92 ms
18,176 KB
testcase_14 AC 72 ms
21,760 KB
testcase_15 AC 74 ms
21,760 KB
testcase_16 AC 75 ms
19,200 KB
testcase_17 AC 147 ms
17,280 KB
testcase_18 AC 142 ms
17,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:131:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  131 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:144:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  144 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

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

class HLDecomposition {
public:
	HLDecomposition(int n) :
		g(n),
		vid(n),
		sub(n, 1),
		head(n),
		heavy(n, -1),
		parent(n) {
	}

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

	void build() {
		dfs(0, -1);
		bfs();
	}

	struct Iterator {
		int u, v;
		HLDecomposition *hl;

		Iterator(HLDecomposition *hl, int u, int v) : hl(hl), u(u), v(v) {}

		pair<int, int> operator*() {
			if (hl->vid[u] > hl->vid[v]) swap(u, v);
			int l = max(hl->vid[hl->head[v]], hl->vid[u]);
			int r = hl->vid[v];
			v = hl->head[u] != hl->head[v] ? hl->parent[hl->head[v]] : -1;
			return make_pair(l, r + 1);
		}

		void operator++() {}

		bool operator!=(Iterator &) {
			return v != -1;
		}
	};

	struct Enumerator {
		int u, v;
		HLDecomposition *hl;

		Enumerator(HLDecomposition *hl, int u, int v) : hl(hl), u(u), v(v) {}

		Iterator begin() const {
			return Iterator(hl, u, v);
		}

		Iterator end() const {
			return Iterator(hl, u, v);
		}
	};

	Enumerator enumerate(int u, int v) {
		return Enumerator(this, u, v);
	}

	int lca(int u, int v) {
		if (vid[u] > vid[v]) swap(u, v);
		if (head[u] == head[v]) return u;
		return lca(u, parent[head[v]]);
	}

	vector<int> parent;

private:
	vector<vector<int>> g;
	vector<int> vid;
	vector<int> sub;
	vector<int> head;
	vector<int> heavy;

	void dfs(int curr, int prev) {
		parent[curr] = prev;
		for (int next : g[curr]) if (next != prev) {
			dfs(next, curr);
			sub[curr] += sub[next];
			if (heavy[curr] == -1 || sub[heavy[curr]] < sub[next]) {
				heavy[curr] = next;
			}
		}
	}

	void bfs() {
		int k = 0;
		queue<int> q;
		q.push(0);
		while (!q.empty()) {
			int first = q.front(); q.pop();
			for (int curr = first; curr != -1; curr = heavy[curr]) {
				vid[curr] = k++;
				head[curr] = first;
				for (int next : g[curr]) {
					if (next == parent[curr]) continue;
					if (next == heavy[curr]) continue;
					q.push(next);
				}
			}
		}
	}
};

vector<int> g[101010];
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;

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

	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