QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417579#8716. 树fallleaves01#WA 0ms3644kbC++201.9kb2024-05-22 19:50:262024-05-22 19:50:26

Judging History

你现在查看的是最新测评结果

  • [2024-05-22 19:50:26]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3644kb
  • [2024-05-22 19:50:26]
  • 提交

answer

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	int n, m, q;
	cin >> n >> m >> q;
	vector<vector<int>> e(n + 1);
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		e[u].emplace_back(v);
		e[v].emplace_back(u);
	}
	vector<int> dep(n + 1);
	vector<vector<int>> fa(21, vector(n + 1, 0));
	auto dfs = [&](auto &&self, int v, int f) -> void {
		fa[0][v] = f;
		for (auto u : e[v]) {
			if (u != f) {
				dep[u] = dep[v] + 1;
				self(self, u, v);
			}
		}
	};
	for (int i = 1; i <= 20; i++) {
		for (int j = 1; j <= n; j++) {
			fa[i][j] = fa[i - 1][fa[i - 1][j]];
		}
	}
	dfs(dfs, 1, 1);
	auto jump = [&](int v, int k) {
		for (int i = 0; i <= 20; i++) {
			if ((k >> i) & 1) {
				v = fa[i][v];
			}
		}
		return v;
	};
	auto lca = [&](int u, int v) {
		if (dep[u] < dep[v]) {
			swap(u, v);
		}
		u = jump(u, dep[u] - dep[v]);
		if (u == v) {
			return u;
		}
		for (int i = 20; i >= 0; i--) {
			if (fa[i][u] != fa[i][v]) {
				u = fa[i][u], v = fa[i][v];
			}
		}
		return fa[0][u];
	};
	auto check = [&](int x, int v, int y) {
		if (lca(x, v) == v) {
			return dep[lca(x, y)] <= dep[v];
		}
		if (lca(y, v) == v) {
			return dep[lca(x, y)] <= dep[v];
		}
		return false;
	};
	vector<int> b(m + 1);
	for (int i = 1; i <= m; i++) {
		cin >> b[i];
	}
	int cnt = m;
	for (int i = 2; i < m; i++) {
		if (check(b[i - 1], b[i], b[i + 1])) {
			--cnt;
		}
	}
	auto calc = [&](int i) {
		int res = 0;
		if (i >= 3 && check(b[i - 2], b[i - 1], b[i])) {
			++res;
		}
		if (i >= 2 && i < m && check(b[i - 1], b[i], b[i + 1])) {
			++res;
		}
		if (i < m - 1 && check(b[i], b[i + 1], b[i + 2])) {
			++res;
		}
		return res;
	};
	for (int i = 1; i <= q; i++) {
		int p, w;
		cin >> p >> w;
		cnt += calc(p);
		b[p] = w;
		cnt -= calc(p);
		cout << cnt << '\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3644kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

183
184
184
184
184
184
184
184
184
184
185
185
185
185
185
185
185
185
185
185
185
185
185
185
185
185
185
186
186
186
186
186
186
186
186
186
186
186
186
186
186
186
186
186
187
187
186
186
187
186
186
186
186
186
186
186
186
186
186
186
186
187
187
186
186
186
186
187
186
187
187
187
187
187
187
...

result:

wrong answer 1st numbers differ - expected: '174', found: '183'