QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#602571 | #8716. 树 | ucup-team902# | WA | 2ms | 10988kb | C++14 | 2.0kb | 2024-10-01 10:50:35 | 2024-10-01 10:50:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
#define gc getchar
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<typename tp>inline void chemx(tp &a,tp b){(a<b)?(a=b):0;}
template<typename tp>inline void chemn(tp &a,tp b){(a>b)?(a=b):0;}
cs int N=200005;
int n,m,q;
int fa[N][20],dep[N],b[N];
vector<int> e[N];
int LCA(int u,int v){
if(dep[u]<dep[v])swap(u,v);
for(int i=19;~i;i--)if(dep[fa[u][i]]>=dep[v])u=fa[u][i];
if(u==v)return v;
for(int i=19;~i;i--)if(fa[u][i]!=fa[v][i]){
u=fa[u][i],v=fa[u][i];
}
return fa[u][0];
}
int dis(int u,int v){
return dep[u]+dep[v]-2*dep[LCA(u,v)];
}
int calc(int a,int b,int c){
//cout<<a<<" "<<b<<" "<<c<<" "<<dis(a,b)<<" "<<dis(b,c)<<" "<<dis(a,c)<<'\n';
if(dis(a,b)+dis(b,c)==dis(a,c))return 0;
else return 1;
}
void dfs(int u,int ff){
for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int v:e[u])if(v!=ff){
fa[v][0]=u;
dep[v]=dep[u]+1;
dfs(v,u);
}
}
int main(){
// freopen("1.in","r",stdin);
n=read(),m=read(),q=read();
for(int i=1;i<n;i++){
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
dep[1]=1;
dfs(1,0);
for(int i=1;i<=m;i++)b[i]=read();
int ans=2;
for(int i=2;i<m;i++)ans+=calc(b[i-1],b[i],b[i+1]);
// cout<<ans<<'\n';exit(0);
while(q--){
int p=read(),v=read();
if(p>2)ans-=calc(b[p-2],b[p-1],b[p]);
if(p+1<m)ans-=calc(b[p],b[p+1],b[p+2]);
if(p>1&&p<m)ans-=calc(b[p-1],b[p],b[p+1]);
b[p]=v;
if(p>2)ans+=calc(b[p-2],b[p-1],b[p]);
if(p+1<m)ans+=calc(b[p],b[p+1],b[p+2]);
if(p>1&&p<m)ans+=calc(b[p-1],b[p],b[p+1]);
cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10988kb
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: 2ms
memory: 10556kb
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 175 175 175 175 175 176 176 176 176 176 176 175 175 175 175 174 173 173 173 173 173 173 174 174 174 174 173 173 174 174 174 174 174 174 174 174 174 173 173 173 174 173 173 174 173 174 174 174 174 174 174 174 174 174 174 174 175 174 174 174 174 174 175 175 177 177 177 177 177 176 ...
result:
wrong answer 138th numbers differ - expected: '173', found: '174'