QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602730 | #8716. 树 | UESTC_DebugSimulator# | WA | 2ms | 9836kb | C++17 | 2.6kb | 2024-10-01 12:29:50 | 2024-10-01 12:29:51 |
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 mod1 = 998244353;
const int mod2 = 1e9 + 7;
const int p1 = 1997;
const int p2 = 3331;
const int maxn = 1e6 + 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 get(int x, int y) {
if (x == y) return x == 2;
if ((x == 1 && y == 0) && (x == 0 && y == 1)) return 1;
if (x == 2) {
if (y == 0) return 0;
return 1;
}
if (y == 2) {
if (x == 0) return 1;
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]);
}
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]);
c[p] = check(b[p], b[p + 1]);
ans += get(c[p], c[p + 1]);
}
else if (p == n) {
ans -= get(c[p - 2], c[p - 1]);
c[p - 1] = check(b[p - 1], b[p]);
ans += get(c[p - 2], c[p - 1]);
}
else {
if (p >= 3) ans -= get(c[p - 2], c[p - 1]);
c[p - 1] = check(b[p - 1], b[p]);
if (p >= 3) ans += get(c[p - 2], c[p - 1]);
if (p <= n - 2) ans -= get(c[p], c[p + 1]);
c[p] = check(b[p], b[p + 1]);
if (p <= n - 2) ans += get(c[p], c[p + 1]);
}
cout << ans << '\n';
}
}
signed main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9836kb
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
result:
wrong answer 3rd numbers differ - expected: '5', found: '4'