QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#602694 | #8716. 树 | UESTC_xxx# | WA | 3ms | 15984kb | C++14 | 2.9kb | 2024-10-01 12:07:44 | 2024-10-01 12:07:53 |
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],ans;
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;
}
void Check(int p1,int p2,int p3,int ty){
if(p1<1)return;
if(p3>m)return;
p1=a[p1];p2=a[p2];p3=a[p3];
int t1=LCA(p1,p2),t2=LCA(p2,p3),t3=LCA(p1,p3);
if(t1==p1){
if(t2==p2);
else if(t2==p3)ans+=ty;
else ans+=ty;
}
else if(t1==p2){
if(t2==p2){
if(t3!=t2)ans+=ty;
}
else if(t2==p3);
else ;
}
else {
if(t2==p2);
else if(t2==p1)ans+=ty;
else ans+=ty;
}
}
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();
}
ans=0;
for(int i=1;i<=m;++i){
Check(i-2,i-1,i,1);
}
// 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();
Check(x-2,x-1,x,-1);
Check(x-1,x,x+1,-1);
Check(x,x+1,x+2,-1);
a[x]=y;
Check(x-2,x-1,x,1);
Check(x-1,x,x+1,1);
Check(x,x+1,x+2,1);
// 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(ans==0){
if(m==1)cout<<1<<'\n';
else cout<<2<<'\n';
}
else {
cout<<ans+2<<'\n';
}
}
return 0;
}
/*
5 5 5
1 2
2 3
3 4
4 5
1 2 3 4 5
1 1
3 1
5 1
3 3
5 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 15984kb
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: 15912kb
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:
179 180 180 180 180 180 180 180 180 180 181 181 180 180 180 180 179 179 179 179 178 177 177 177 177 177 177 178 178 178 178 177 177 178 178 178 178 178 178 178 178 178 177 177 177 178 177 177 177 176 177 177 177 177 177 177 177 177 177 177 177 178 177 177 177 177 177 178 178 179 179 179 179 179 178 ...
result:
wrong answer 1st numbers differ - expected: '174', found: '179'