QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602634 | #8716. 树 | UESTC_xxx# | WA | 0ms | 18016kb | C++14 | 2.9kb | 2024-10-01 11:24:36 | 2024-10-01 11:24:37 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<map>
#define LL long long
using namespace std;
const int mod=998244353;
const int Base=23,Base2=27;
inline int lowbit(int x){return x&(-x);}
inline int Jia(int x){return x>=mod?x-mod:x;}
inline int Jian(int x){return x>=0?x:x+mod;}
int KSM(int a,int b){
int Ans=1;
while(b){
if(b&1)Ans=1LL*Ans*a%mod;
a=1LL*a*a%mod;
b>>=1;
}
return Ans;
}
inline int read(){
int k=0,ty=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')ty=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
k=k*10+ch-'0';
ch=getchar();
}
return k*ty;
}
int gcd(int a,int b){
if(!b)return a;
return gcd(b,a%b);
}
const int maxn=5e5+5;
int n,m,Q,fa[maxn*2],dept[maxn*2],top[maxn*2],DFN,cnt,h[maxn];
int Size[maxn],Son[maxn],id[maxn],a[maxn],b[maxn],c[maxn];
struct d{
int to,next;
}E[maxn*2];
void add(int x,int y){
E[++cnt]=(d){y,h[x]};
h[x]=cnt;
}
void dfs1(int x,int father,int deep){
dept[x]=deep;fa[x]=father;
Size[x]=1;
for(int i=h[x];i;i=E[i].next){
int y=E[i].to;
if(y==fa[x])continue;
dfs1(y,x,deep+1);
Size[x]+=Size[y];
if(Size[y]>Size[Son[x]])Son[x]=y;
}
}
void dfs2(int x,int Top){
id[x]=++DFN;top[x]=Top;
if(!Son[x])return;
dfs2(Son[x],Top);
for(int i=h[x];i;i=E[i].next){
int y=E[i].to;
if(y==fa[x]||y==Son[x])continue;
dfs2(y,y);
}
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(dept[x]<dept[y])swap(x,y);
x=fa[top[x]];
}
if(dept[x]<dept[y])swap(x,y);
return y;
}
int main(){
// freopen("data.txt","r",stdin);
n=read();m=read();Q=read();
for(int i=1;i<n;++i){
int x=read(),y=read();
add(x,y);add(y,x);
}
dfs1(1,0,1);
dfs2(1,1);
for(int i=1;i<=m;++i){
a[i]=read();
}
int ans=2;
for(int i=2;i<=m;++i){
int t=LCA(a[i],a[i-1]);
if(t==a[i-1])b[i]=1,c[i]=1;
else if(t==a[i])b[i]=0,c[i]=0;
else b[i]=0,c[i]=1;
if(b[i]!=c[i-1]&&i!=1&&i!=2)++ans;
}
// for(int i=1;i<=m;++i)cout<<b[i]<<' ';cout<<'\n';
// for(int i=1;i<=m;++i)cout<<c[i]<<' ';cout<<'\n';
// cout<<ans<<'\n';
for(int i=1;i<=Q;++i){
int x=read(),y=read();
if(x!=1&&b[x]!=c[x-1]&&x!=2&&x!=1)--ans;
if(x!=m&&b[x+1]!=c[x]&&x+1!=1&&x+1!=2)--ans;
if(x+2<=m&&b[x+2]!=c[x+1])--ans;
a[x]=y;
// cout<<"daaa "<<ans<<'\n';
if(x!=1){
int t=LCA(a[x],a[x-1]);
if(t==a[x-1])b[x]=1,c[x]=1;
else if(t==a[x])b[x]=0,c[x]=0;
else b[x]=0,c[x]=1;
if(b[x]!=c[x-1]&&x!=1&&x!=2)++ans;
}
if(x!=m){
int t=LCA(a[x],a[x+1]);
// cout<<"FAF "<<t<<'\n';
if(t==a[x])b[x+1]=1,c[x+1]=1;
else if(t==a[x+1])b[x+1]=0,c[x+1]=0;
else b[x+1]=0,c[x+1]=1;
if(b[x+1]!=c[x]&&x+1!=1&&x+1!=2)++ans;
if(x+2<=m&&b[x+2]!=c[x+1])++ans;
}
// for(int i=1;i<=m;++i)cout<<b[i]<<' ';cout<<'\n';
// for(int i=1;i<=m;++i)cout<<c[i]<<' ';cout<<'\n';
// cout<<ans<<'\n';
if(m!=1)cout<<ans<<'\n';
else cout<<1<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18016kb
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: 17960kb
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:
180 181 181 181 181 181 181 181 182 182 183 183 182 182 182 182 181 181 181 181 180 179 179 179 179 179 179 180 180 180 180 179 178 179 179 179 179 179 179 179 179 179 178 178 178 179 178 178 178 177 178 179 179 179 179 179 179 179 179 179 179 180 179 179 179 179 179 180 180 181 181 181 181 181 180 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '180'