QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#602811 | #8716. 树 | UESTC_DebugSimulator# | WA | 1ms | 7884kb | C++17 | 3.1kb | 2024-10-01 12:59:18 | 2024-10-01 12:59:19 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lowbit(i) (i&-i)
#define rand() (myrand())
using namespace std;
mt19937 myrand(time(0));
const int maxn = 2e5 + 5;
int n, _, m, q, fa[maxn][20], head[maxn], cnt, b[maxn], dep[maxn], c[maxn];
struct EDGE{
int next, to;
}edge[maxn<<1];
void addedge(int from, int to) {
edge[++cnt].next = head[from];
edge[cnt].to = to;
head[from] = cnt;
}
void dfs(int u, int p) {
fa[u][0] = p;
dep[u] = dep[p] + 1;
for (int i = head[u] ; i ; i = edge[i].next) {
int v = edge[i].to;
if (v == p) continue;
dfs(v, u);
}
}
int check(int u, int v) {
if (dep[v] > dep[u]) {
int k = dep[v] - dep[u];
for (int i = 19 ; i >= 0 ; --i) {
if (k&(1<<i)) v = fa[v][i];
}
if (v != u) return 2;
return 0;
}
else {
int k = dep[u] - dep[v];
for (int i = 19 ; i >= 0 ; --i) {
if (k&(1<<i)) u = fa[u][i];
}
if (v != u) return 2;
return 1;
}
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int k = dep[x] - dep[y];
for (int i = 19 ; i >= 0 ; --i) {
if (k&(1<<i)) x = fa[x][i];
}
if (x == y) return x;
for (int i = 19 ; i >= 0 ; --i) {
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
int get(int x, int y, int a, int b, int c) {
if (x == y) return x == 2;
if (x == 1 && y == 0) {
if (LCA(a, c) == b) return 0;
return 1;
}
if (x == 0 && y == 1) return 1;
if (x == 2 && y == 0) return 0;
if (x == 2 && y == 1) return 1;
if (x == 0 && y == 2) return 1;
if (x == 1 && y == 2) return 0;
}
void solve() {
cin >> n >> m >> q;
for (int i = 1 ; i < n ; ++i) {
int u, v;
cin >> u >> v;
addedge(u, v);
addedge(v, u);
}
dfs(1, 0);
for (int j = 1 ; j < 20 ; ++j) {
for (int i = 1 ; i <= n ; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
for (int i = 1 ; i <= m ; ++i) cin >> b[i];
if (m == 1) {
for (int i = 1 ; i <= q ; ++i) {
int p, w;
cin >> p >> w;
cout << 1 << '\n';
}
return;
}
int ans = 2;
c[1] = check(b[1], b[2]);
for (int i = 3 ; i <= m ; ++i) {
c[i - 1] = check(b[i - 1], b[i]);
ans += get(c[i - 2], c[i - 1], b[i - 2], b[i - 1], b[i]);
}
for (int i = 1 ; i <= q ; ++i) {
int p, w;
cin >> p >> w;
b[p] = w;
if (p == 1) {
ans -= get(c[p], c[p + 1], b[p], b[p + 1], b[p + 2]);
c[p] = check(b[p], b[p + 1]);
ans += get(c[p], c[p + 1], b[p], b[p + 1], b[p + 2]);
}
else if (p == n) {
ans -= get(c[p - 2], c[p - 1], b[p - 2], b[p - 1], b[p]);
c[p - 1] = check(b[p - 1], b[p]);
ans += get(c[p - 2], c[p - 1], b[p - 2], b[p - 1], b[p]);
}
else {
if (p >= 3) ans -= get(c[p - 2], c[p - 1], b[p - 2], b[p - 1], b[p]);
c[p - 1] = check(b[p - 1], b[p]);
if (p >= 3) ans += get(c[p - 2], c[p - 1], b[p - 2], b[p - 1], b[p]);
if (p <= n - 2) ans -= get(c[p], c[p + 1], b[p], b[p + 1], b[p + 2]);
c[p] = check(b[p], b[p + 1]);
if (p <= n - 2) ans += get(c[p], c[p + 1], b[p], b[p + 1], b[p + 2]);
}
cout << ans << '\n';
}
}
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3788kb
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: 1ms
memory: 7884kb
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:
174 175 175 175 175 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 177 177 177 177 177 177 177 177 177 176 176 176 177 177 177 177 177 178 178 178 178 178 178 178 178 178 178 178 178 178 179 179 179 179 179 179 179 179 179 179 179 179 ...
result:
wrong answer 6th numbers differ - expected: '175', found: '176'