QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417045 | #8716. 树 | FlyingFeather | WA | 2ms | 11688kb | C++17 | 1.9kb | 2024-05-22 13:42:00 | 2024-05-22 13:42:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 200010, M = 19;
vector<int> e[N];
int n, m, q, fa[N][M], dep[N], b[N], ans;
void dfs (int u, int par) {
dep[u] = dep[par] + 1;
for (int v: e[u]) {
if (v == par) {
continue;
}
fa[v][0] = u;
for (int j = 1; j < M; j ++) {
fa[v][j] = fa[fa[v][j - 1]][j - 1];
}
dfs(v, u);
}
}
int lca (int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int j = M - 1; j >= 0; j --) {
if (dep[fa[u][j]] >= dep[v]) {
u = fa[u][j];
}
}
if (u == v) {
return u;
}
for (int j = M - 1; j >= 0; j --) {
if (fa[u][j] != fa[v][j]) {
u = fa[u][j];
v = fa[v][j];
}
}
return fa[u][0];
}
int dis (int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
void update (int i, int delta) {
if (i <= 1 || i >= m) {
return;
}
int x = b[i - 1], y = b[i], z = b[i + 1];
if (dis(x, y) + dis(y, z) == dis(x, z)) {
return;
}
ans += delta;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (int i = 1; i < n; i ++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i <= m; i ++) {
cin >> b[i];
}
ans = 2;
for (int i = 2; i < m; i ++) {
update(i, 1);
}
if (m == 1) {
while (q --) {
cout << "1\n";
}
return 0;
}
cout<<ans<<endl;
while (q --) {
int i, v;
cin >> i >> v;
update(i - 1, -1);
update(i, -1);
update(i + 1, -1);
b[i] = v;
update(i - 1, 1);
update(i, 1);
update(i + 1, 1);
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 11688kb
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 4 5
result:
wrong answer 3rd numbers differ - expected: '5', found: '4'