QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#419348 | #8716. 树 | yangyuchen17 | WA | 1ms | 9908kb | C++14 | 4.0kb | 2024-05-23 20:37:31 | 2024-05-23 20:37:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+5,M=2*N;
int n,m,q,b[N],deep[N],f[N],h1[N],son[N];
int lian[N],top_lian[N];
int a[N],c[N];
int h[N],e[N],ne[N],idx;
void add(int u,int v){
e[idx]=v,ne[idx]=h[u],h[u]=idx++;
}
bool vis[N];
void dfs1(int u){
vis[u]=true;
for(int i=h[u];~i;i=ne[i]){
int v=e[i];
if(vis[v]) continue;
deep[v]=deep[u]+1;
f[v]=u;
dfs1(v);
if(h1[v]+1>h1[u]){
h1[u]=h1[v]+1;
son[u]=v;
}
}
}
void dfs2(int u){
for(int i=h[u];~i;i=ne[i]){
int v=e[i];
if(v==f[u]) continue;
if(v==son[u]){
lian[v]=lian[u];
}else{
lian[v]=++idx;
top_lian[idx]=v;
}
dfs2(v);
}
}
int main(){
cin>>n>>m>>q;
memset(h,-1,sizeof h);
a[1]=c[n]=-1;
int u,v,ans=2;
for(int i=1;i<n;i++){
cin>>u>>v;
add(u,v),add(v,u);
}
idx=0;
deep[1]=1;
dfs1(1);
lian[1]=++idx;
top_lian[idx]=1;
dfs2(1);
cin>>b[1];
for(int i=2;i<=m;i++){
cin>>b[i];
int x=b[i],y=b[i-1],last;
bool flag=false;
if(deep[x]>deep[y]) swap(x,y),flag=true;
while(lian[x]!=lian[y]){
if(deep[top_lian[lian[x]]]>deep[top_lian[lian[y]]]){
break;
}
last=y;
y=f[top_lian[lian[y]]];
}
if(lian[x]!=lian[y]) continue;
if(flag){
if(deep[x]<deep[y]) c[i-1]=lian[x];
else if(deep[x]==deep[y]) c[i-1]=lian[last];
}else{
if(deep[x]<deep[y]) a[i]=lian[x];
else if(deep[x]==deep[y]) a[i]=lian[last];
}
}
for(int i=2;i<=m;i++){
if(a[i]==c[i]){
ans++;
//cout<<b[i]<<":"<<a[i]<<" "<<c[i]<<endl;
}
}
//cout<<ans<<endl;
int p;
while(q--){
cin>>p>>u;
b[p]=u;
if(a[p]==c[p]) ans--;
if(p!=1){
if(a[p-1]==c[p-1]) ans--;
int x=b[p],y=b[p-1],last;
bool flag=false;
if(deep[x]>deep[y]) swap(x,y),flag=true;
while(lian[x]!=lian[y]){
if(deep[top_lian[lian[x]]]>deep[top_lian[lian[y]]]){
break;
}
last=y;
y=f[top_lian[lian[y]]];
}
if(lian[x]!=lian[y]) a[p]=0,c[p-1]=0;
else if(flag){
a[p]=0;
if(deep[x]<deep[y]) c[p-1]=lian[x];
else if(deep[x]==deep[y]) c[p-1]=lian[last];
}else{
if(deep[x]<deep[y]) a[p]=lian[x];
else if(deep[x]==deep[y]) a[p]=lian[last];
c[p-1]=0;
}
if(a[p-1]==c[p-1]) ans++;
}
if(p!=n){
if(a[p+1]==c[p+1]) ans--;
int x=b[p+1],y=b[p],last;
bool flag=false;
if(deep[x]>deep[y]) swap(x,y),flag=true;
while(lian[x]!=lian[y]){
if(deep[top_lian[lian[x]]]>deep[top_lian[lian[y]]]){
break;
}
last=y;
y=f[top_lian[lian[y]]];
}
if(lian[x]!=lian[y]) a[p+1]=0,c[p]=0;
else if(flag){
if(deep[x]<deep[y]) c[p]=lian[x];
else if(deep[x]==deep[y]) c[p]=lian[last];
a[p+1]=0;
}else{
if(deep[x]<deep[y]) a[p+1]=lian[x];
else if(deep[x]==deep[y]) a[p+1]=lian[last];
c[p]=0;
}
if(a[p+1]==c[p+1]) ans++;
}
if(a[p]==c[p]) ans++;
cout<<ans<<endl;
/*
for(int i=1;i<=m;i++){
cout<<b[i]<<":"<<a[i]<<" "<<c[i]<<endl;
}
*/
}
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: 1ms
memory: 9756kb
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: 9908kb
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 174 174 173 172 172 172 172 172 172 173 173 173 173 172 172 173 173 173 173 173 173 173 173 173 172 172 172 173 172 172 173 172 173 173 173 173 173 173 173 173 173 173 173 174 173 173 173 173 173 174 174 176 176 176 176 176 175 ...
result:
wrong answer 19th numbers differ - expected: '175', found: '174'