QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#602623 | #8716. 树 | liguo# | WA | 1ms | 7992kb | C++20 | 2.2kb | 2024-10-01 11:18:31 | 2024-10-01 11:18:32 |
Judging History
answer
#include<cstdio>
#include<iostream>
using namespace std;
int n,m,q;
const int maxn=2e6+10;
struct node{
int to,next;
}e[maxn*2];
int head[maxn],k=0;
void add(int a,int b){
e[++k].next=head[a];
e[k].to=b;
head[a]=k;
}
struct nod{
int pl,to;
}h[maxn];
int fa[maxn][22],deep[maxn];
void dfs(int id){
deep[id]=deep[fa[id][0]]+1;
for(int i=1;i<21;i++){
if(fa[id][i-1]==0) continue;
fa[id][i]=fa[fa[id][i-1]][i-1];
}
for(int i=head[id];i;i=e[i].next){
int tp=e[i].to;
if(tp==fa[id][0]) continue;
fa[tp][0]=id;
dfs(tp);
}
}
int lca(int a,int b){
if(deep[a]>deep[b]) return lca(b,a);
for(int i=20;i>=0;i--){
if(deep[a]>deep[fa[b][i]]) continue;
b=fa[b][i];
}
if(a==b) return a;
for(int i=20;i>=0;i--){
if(fa[a][i]==fa[b][i]) continue;
b=fa[b][i];
a=fa[a][i];
}
return fa[a][0];
}
int main(){
scanf("%d %d %d",&n,&m,&q);
int a,b;
for(int i=1;i<n;i++){
scanf("%d %d",&a,&b);
add(a,b);
add(b,a);
}
for(int i=1;i<=m;i++) scanf("%d",&h[i].pl);
dfs(1);
int ans=1;
if(n>1) ans++;
for(int i=2;i<=n;i++){
int c=lca(h[i].pl,h[i-1].pl);
if(c==h[i-1].pl){
h[i].to=-1;
if(h[i-1].to>0){
c=lca(h[i].pl,h[i-2].pl);
if(c!=h[i-1].pl) ans++;
}
}
else if(c==h[i].pl){
h[i].to=1;
if(h[i-1].to<0) ans++;
}
else{
h[i].to=-1;
if(h[i-1].to<0) ans++;
}
}
while(q--){
scanf("%d%d",&a,&b);
for(int i=max(2,a);i<=n&&i<=a+2;i++){
int c=lca(h[i].pl,h[i-1].pl);
if(c==h[i-1].pl){
h[i].to=-1;
if(h[i-1].to>0){
c=lca(h[i].pl,h[i-2].pl);
if(c!=h[i-1].pl) ans--;
}
}
else if(c==h[i].pl){
h[i].to=1;
if(h[i-1].to<0) ans--;
}
else{
h[i].to=-1;
if(h[i-1].to<0) ans--;
}
}
h[a].pl=b;
for(int i=max(2,a);i<=n&&i<=a+2;i++){
int c=lca(h[i].pl,h[i-1].pl);
if(c==h[i-1].pl){
h[i].to=-1;
if(h[i-1].to>0){
c=lca(h[i].pl,h[i-2].pl);
if(c!=h[i-1].pl) ans++;
}
}
else if(c==h[i].pl){
h[i].to=1;
if(h[i-1].to<0) ans++;
}
else{
h[i].to=-1;
if(h[i-1].to<0) ans++;
}
//printf("***%d\n",ans);
}
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7992kb
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: 1ms
memory: 7916kb
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:
27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 26 26 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '27'