社区讨论
样例都没过。。。。0pts求调
P4281[AHOI2008] 紧急集合 / 聚会参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lys3px2f
- 此快照首次捕获于
- 2024/07/19 10:49 2 年前
- 此快照最后确认于
- 2024/07/19 11:33 2 年前
CPP
#include <bits/stdc++.h>
using namespace std;
vector<long long >edge[500010];
long long n,m,a,b,c,s,cnt,deep[1000010],pos[500010],ola[1000010],mn[4000010],ans,t;
void dfs(int x,int father){
ola[++cnt]=x;
deep[x]=deep[father]+1;
pos[x]=cnt;
for(int i=0;i<edge[x].size();i++){
if(edge[x][i]==father)continue;
dfs(edge[x][i],x);
ola[++cnt]=x;
}
}
void build (int v,int l,int r){
if(l==r){
mn[v]=ola[l];
return;
}
int mid=(l+r)/2;
build(v<<1,l,mid);
build(v<<1|1,mid+1,r);
if(deep[mn[v<<1]]<deep[mn[v<<1|1]])
mn[v]=mn[v<<1];
else
mn[v]=mn[v<<1|1];
}
int query(int v,int l,int r,int x,int y){
if(x<=l&&r<=y)
return mn[v];
int mid=(l+r)/2,k1=0,k2=0;
if(x<=mid)
k1=query(v<<1,l,mid,x,y);
if(y>mid)
k2=query(v<<1|1,mid+1,r,x,y);
if(deep[k1]<deep[k2])
return k1;
else
return k2;
}
int main(){
cin>>n>>m;
for(int i=1;i<n;i++){
cin>>a>>b;
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1,0);
build(1,1,cnt);
deep[0]=n+1;
while(m--){
cin>>a>>b>>c;
int aa,bb,cc;
aa=a;
bb=b;
cc=c;
a=pos[a],b=pos[b],c=pos[c];
if(a>b)swap(a,b);
if(b>c)swap(c,b);
if(a>c)swap(a,c);
int t1,t2,t3;
t1=query(1,1,cnt,a,b);
t2=query(1,1,cnt,a,c);
t3=query(1,1,cnt,b,c);
//cout<<"t1: "<<t1<<" t2: "<<t2<<" t3: "<<t3<<" d[a] "<<deep[aa]<<" d[b] "<<deep[bb]<<" d[c] "<<deep[cc]<<endl;
ans=0;
if(t1==t2)
t=t3;
else if(t1==t3)
t=t2;
else if(t2==t3)
t=t1;
ans=deep[aa]+deep[bb]+deep[cc]-deep[t1]-deep[t2]-deep[t3];
cout<<t<<" "<<ans<<endl;
}
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...