QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417123 | #8716. 树 | yzhang | WA | 0ms | 9888kb | C++14 | 1.9kb | 2024-05-22 14:47:20 | 2024-05-22 14:47:20 |
Judging History
answer
#include<bits/stdc++.h>
#define N 200005
using namespace std;
int n,m,q,b[N];
struct edge{
int to,nxt;
}e[N<<1];
int head[N],cnt;
void add(int u,int v){
e[++cnt]=(edge){v,head[u]};
head[u]=cnt;
}
int f[N][20],dep[N];
void dfs(int x,int fa){
f[x][0]=fa;
dep[x]=dep[fa]+1;
for(int i=1;i<=19;++i) f[x][i]=f[f[x][i-1]][i-1];
for(int i=head[x];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa) continue;
dfs(v,x);
}
}
int glca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=19;i>=0;--i) if(dep[x]-(1<<i)>=dep[y]) x=f[x][i];
for(int i=19;i>=0;--i) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return x==y?x:f[x][0];
}
int dis(int x,int y){
int lca=glca(x,y);
return dep[x]+dep[y]-2*dep[lca];
}
void sol(){
cin>>n>>m>>q;
for(int i=1;i<n;++i){
int u,v;
cin>>u>>v;
add(u,v),add(v,u);
}
dfs(1,0);
for(int i=1;i<=m;++i)
cin>>b[i];
int ans=m;
for(int i=2;i<m;++i)
if(dis(b[i-1],b[i+1])==dis(b[i-1],b[i])+dis(b[i],b[i+1])){
--ans;
// cerr<<"pos "<<i<<endl;
// cerr<<dis(b[i-1],b[i+1])<<" "<<dis(b[i-1],b[i])<<" "<<dis(b[i],b[i+1])<<endl;
}
for(int t=1;t<=q;++t){
int pos,v;
cin>>pos>>v;
for(int i=pos-1;i<=pos+1;++i){
if(i<2||i>n-1) continue;
if(dis(b[i-1],b[i+1])==dis(b[i-1],b[i])+dis(b[i],b[i+1]))
++ans;
}
b[pos]=v;
for(int i=pos-1;i<=pos+1;++i){
if(i<2||i>n-1) continue;
if(dis(b[i-1],b[i+1])==dis(b[i-1],b[i])+dis(b[i],b[i+1]))
--ans;
}
cout<<ans<<'\n';
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
sol();
return 0;
}
/*
5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9888kb
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: 5680kb
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 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 173 173 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 ...
result:
wrong answer 2nd numbers differ - expected: '175', found: '174'