您的位置:首页 > 其它

【LCA】CodeForce #326 Div.2 E:Duff in the Army

2015-10-16 17:41 351 查看
C. Duff in the Army
Recently Duff has been a soldier in the army. Malek is her commander.

Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.

There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is iand he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>

#define maxn 100001

using namespace std;

inline int in()
{
int x=0,f=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')f=-1;
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return f*x;
}

struct ed{
int to,last;
}edge[maxn*2];

struct lc{
int father,num[11];
}f[19][maxn];

int last[maxn],tot=0,dep[maxn],n;

void add(int u,int v)
{
edge[++tot].to=v,edge[tot].last=last[u],last[u]=tot;
edge[++tot].to=u,edge[tot].last=last[v],last[v]=tot;
}

void dfs(int poi,int lastt,int de)
{
dep[poi]=de;
if(lastt!=-1)f[0][poi].father=lastt;
for(int i=last[poi];i;i=edge[i].last)
if(edge[i].to!=lastt)dfs(edge[i].to,poi,de+1);
}

void update(int pos,int cen)
{
int i=1,j=1;
while(i<=f[cen-1][f[cen-1][pos].father].num[0]&&j<=f[cen-1][pos].num[0]&&f[cen][pos].num[0]<10)
{
if(i<=f[cen-1][f[cen-1][pos].father].num[0]&&f[cen-1][f[cen-1][pos].father].num[i]<f[cen-1][pos].num[j])f[cen][pos].num[++f[cen][pos].num[0]]=f[cen-1][f[cen-1][pos].father].num[i++];
else if(j<=f[cen-1][pos].num[0])f[cen][pos].num[++f[cen][pos].num[0]]=f[cen-1][pos].num[j++];
}
while(i<=f[cen-1][f[cen-1][pos].father].num[0]&&f[cen][pos].num[0]<10)f[cen][pos].num[++f[cen][pos].num[0]]=f[cen-1][f[cen-1][pos].father].num[i++];
while(j<=f[cen-1][pos].num[0]&&f[cen][pos].num[0]<10)f[cen][pos].num[++f[cen][pos].num[0]]=f[cen-1][pos].num[j++];
}

void pre()
{
for(int i=1;(1<<i)<=n;i++)
for(int j=1;j<=n;j++)
if(f[i-1][f[i-1][j].father].father)f[i][j].father=f[i-1][f[i-1][j].father].father,update(j,i);
}

int ANS[11],ANS_B[11];

void Up(int pos,int cen,int kk)
{
ANS_B[0]=0;
int i=1,j=1;
while(i<=ANS[0]&&j<=f[cen][pos].num[0]&&ANS_B[0]<kk)
{
if(i<=ANS[0]&&ANS[i]<f[cen][pos].num[j])ANS_B[++ANS_B[0]]=ANS[i++];
else if(j<=f[cen][pos].num[0])ANS_B[++ANS_B[0]]=f[cen][pos].num[j++];
}
while(i<=ANS[0]&&ANS_B[0]<kk)ANS_B[++ANS_B[0]]=ANS[i++];
while(j<=f[cen][pos].num[0]&&ANS_B[0]<kk)ANS_B[++ANS_B[0]]=f[cen][pos].num[j++];
for(int i=0;i<=ANS_B[0];i++)ANS[i]=ANS_B[i];
}

void print()
{
printf("%d",ANS[0]);
for(int i=1;i<=ANS[0];i++)printf(" %d",ANS[i]);
printf("\n");
}

void lca(int u,int v,int kk)
{
ANS[0]=0;
int nu;
if(dep[u]>dep[v])swap(u,v);
if(dep[v]!=dep[u])
{
nu=log2(dep[v]-dep[u]);
for(int i=nu;i>=0;i--)
if((1<<i) & (dep[v]-dep[u]))Up(v,i,kk),v=f[i][v].father;
}
if(u==v)
{
Up(u,0,kk);
print();
return;
}
nu=log2(dep[v]);
while(nu!=-1)
{
if(f[nu][u].father==f[nu][v].father){nu--;continue;}
Up(v,nu,kk);
Up(u,nu,kk);
u=f[nu][u].father;
v=f[nu][v].father;
nu--;
}
Up(v,0,kk);
Up(u,0,kk);
Up(f[0][u].father,0,kk);
print();
}

int main()
{
freopen("t.in","r",stdin);
int m,q,u,v,kk;
n=in(),m=in(),q=in();
for(int i=1;i<n;i++)
u=in(),v=in(),add(u,v);
for(int i=1;i<=m;i++)
{
u=in();
if(f[0][u].num[0]<10)f[0][u].num[++f[0][u].num[0]]=i;
}
dfs(1,-1,0);
pre();
for(int i=1;i<=q;i++)
{
u=in(),v=in(),kk=in();
lca(u,v,kk);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: