QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602653 | #8716. 树 | xing4c# | WA | 2ms | 10388kb | C++14 | 2.9kb | 2024-10-01 11:36:34 | 2024-10-01 11:36:36 |
Judging History
answer
#include <bits/stdc++.h>
#define NOSYNC ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(), x.end()
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 200005;
// -------------- Templates Above -------------------------
int n, m, q;
vector<int> g[N];
int dfn[N], siz[N], b[N], dfncnt;
int tp[N];
void dfs(int x, int fa) {
siz[x] = 1;
dfn[x] = ++ dfncnt;
for(int y : g[x]) {
if(y == fa) continue;
dfs(y, x);
siz[x] += siz[y];
}
}
int in(int x, int y) { // x 是否在 y 的子树内
if(dfn[y] <= dfn[x] && dfn[x] <= dfn[y] + siz[y] - 1) return 1;
return 0;
}
int get(int x) {
// x ~ x + 1 的答案
// a, b, c 为 x, x+1, x+2
int y = x + 1, z = x + 2;
int A = b[x], B = b[y], C = b[z];
// cout << "A: " << A << " B: " << B << " C: " << C << endl;
if(tp[x] == 0 && tp[y] == 0) {
return 0;
}
else if(tp[x] == 0 && tp[y] == 1) {
if(!in(C, B)) return 0;
if(in(C, A) || in(A, C)) return 1;
return 0;
}
else if(tp[x] == 1 && tp[y] == 0) {
return 1;
}
else if(tp[x] == 1 && tp[y] == 1) {
if(in(C, B)) return 0;
return 1;
}
}
signed main() {
NOSYNC;
cin >> n >> m >> q;
for(int i = 1; i < n; i ++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= m; i ++) {
cin >> b[i];
}
dfs(1, 1);
for(int i = 1; i < m; i ++) {
int j = i + 1; // a = bi, b = bj
if(in(b[i], b[j])) {
tp[i] = 0;
}
else tp[i] = 1;
}
int ans = 2;
for(int i = 1; i < m - 1; i ++) {
ans += get(i);
}
// for(int i = 1; i < m; i ++) {
// cout << tp[i] << " ";
// }cout << endl;
// for(int i = 1; i < m - 1; i ++) {
// cout << get(i) << " ";
// }cout << endl;
// cout << "ans: " << ans << endl;
for(int i = 1; i <= q; i ++) {
int p, w; cin >> p >> w;
// 改变 tp[p], tp[p-1]
// 重新统计答案:p-2, p-1, p
if(m == 1) {
cout << 1 << endl;
continue;
}
if(m == 2) {
cout << 2 << endl;
continue;
}
if(p == m) {
// 改变 p-1
// 重新统计答案:p-2
ans -= get(p-2);
b[p] = w;
if(in(b[p-1], b[p])) tp[p-1] = 0;
else tp[p-1] = 1;
ans += get(p-2);
}
else if(p == 1) {
// 改变 p
// 重新统计答案:p
ans -= get(p);
b[p] = w;
if(in(b[p], b[p+1])) tp[p] = 0;
else tp[p] = 1;
ans += get(p);
}
else {
// 判一下 p-2 是否要统计
// 判一下 p 是否要统计
if(p-2 >= 1) {
ans -= get(p-2);
}
if(p <= m-2) {
ans -= get(p);
}
ans -= get(p-1);
b[p] = w;
if(in(b[p-1], b[p])) tp[p-1] = 0;
else tp[p-1] = 1;
if(in(b[p], b[p+1])) tp[p] = 0;
else tp[p] = 1;
if(p-2 >= 1) {
ans += get(p-2);
}
if(p <= m-2) {
ans += get(p);
}
ans += get(p-1);
}
cout << ans << endl;
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10296kb
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: 10388kb
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:
156 157 157 157 156 156 156 157 157 157 158 158 158 158 157 157 156 156 156 156 155 155 155 155 155 154 154 154 154 154 154 153 153 154 154 154 155 155 155 155 155 155 154 154 154 154 154 155 156 155 156 156 156 155 156 156 156 157 157 157 158 159 159 159 159 159 159 160 160 161 161 160 161 159 158 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '156'