QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#449937 | #8716. 树 | JEdward | WA | 3ms | 14916kb | C++17 | 2.3kb | 2024-06-21 19:47:45 | 2024-06-21 19:47:45 |
Judging History
answer
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define here system("pause")
using namespace std;
const int N = 2e5+5;
const int mod = 1e9+7;
const int INF = 1e15+7;
const double eps = 1e-6;
inline int pow(int a, int b, int p){ int res = 1%p; while(b){ if(b&1) res=res*a%p; a=a*a%p, b>>=1;} return res;}
inline int inv(int a, int p){ return pow(a,p-2,p)%p;}
int n, m, q;
vector<int> g[N];
int b[N], ok[N];
int f[N][20], dep[N];
inline void dfs(int u, int fa){
f[u][0] = fa;
dep[u] = dep[fa] + 1;
for(int i=1;i<20 && f[u][i-1];i++){
f[u][i] = f[f[u][i-1]][i-1];
}
for(auto v:g[u]){
if(v==fa) continue;
dfs(v, u);
}
}
inline int getlca(int u, int v){
if(dep[u]<dep[v]) swap(u, v);
for(int i=19;i>=0;i--){
if(dep[f[u][i]]>=dep[v]) u = f[u][i];
}
if(u==v) return u;
for(int i=19;i>=0;i--){
if(f[u][i]!=f[v][i]) u = f[u][i], v = f[v][i];
}
return f[v][0];
}
inline int check(int i){ //共链返回0
int x = b[i-1], y = b[i], z = b[i+1];
if(x==z) return 1;
if(getlca(x, y)==y || getlca(y, z)==y) return 0;
else return 1;
}
inline void sol(){
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);
}
dfs(1, 0);
for(int i=1;i<=m;i++) cin >> b[i];
for(int i=2;i<m;i++){
ok[i] = check(i);
}
int ans = accumulate(ok+1,ok+1+m,0LL);
while(q--){
int idx, now;
cin >> idx >> now;
b[idx] = now;
if(idx>2){
int delta = ok[idx-1];
ok[idx-1] = check(idx-1);
delta -= ok[idx-1];
ans -= delta;
}
if(idx<m-1){
int delta = ok[idx+1];
ok[idx+1] = check(idx+1);
delta -= ok[idx+1];
ans -= delta;
}
if(idx!=1 && idx!=m){
int delta = ok[idx];
ok[idx] = check(idx);
delta -= ok[idx];
ans -= delta;
}
// for(int i=1;i<=m;i++){
// cout << " " << b[i];
// }cout << endl;
// for(int i=1;i<=m;i++){
// cout << " " << ok[i];
// }cout << endl;
cout << ans+2 << endl;
}
}
signed main(){
IOS;
int tc = 1;
while(tc--){
sol();
}
return 0;
}
/*
10 8 1
1 2
2 3
2 6
6 7
3 5
3 4
1 8
8 9
9 10
4 3 6 2 3 8 10 1
1 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14124kb
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: 14916kb
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:
151 152 152 152 151 151 151 152 152 152 153 153 153 153 152 151 150 150 150 150 149 148 148 148 148 147 147 147 147 147 147 146 145 146 146 146 147 147 147 147 147 147 146 146 147 148 147 148 149 148 149 149 148 147 148 148 148 149 149 149 150 151 150 150 150 150 150 150 150 150 150 149 150 150 149 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '151'