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.
Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.
To answer a query:
Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order.
If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).
Duff is very busy at the moment, so she asked you to help her and answer the queries.
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).
The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u).
Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m).
Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
5 4 5
1 3
1 2
1 4
4 5
2 1 4 3
4 5 6
1 5 2
5 5 10
2 3 3
5 3 1
1 3
2 2 3
0
3 1 2 4
1 2
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
大约题目是给一棵树给m个人在哪个点上的信息
然后给q个询问,每次问u到v上的路径有的点上编号最小的k个人,k<=10(很关键)
u到v上路径的询问很容易想到lca
但是前k个答案很不好搞?
直接在lca数组里面开个Num[11]记录前10个在该点上的编号
码了1个半小时结果wa成狗- -
最后发现lca打挂了。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector> #define maxn 100001 using namespace std; inline int in()
{
int x=,f=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')f=-;
while(ch<=''&&ch>='')x=x*+ch-'',ch=getchar();
return f*x;
} struct ed{
int to,last;
}edge[maxn*]; struct lc{
int father,num[];
}f[][maxn]; int last[maxn],tot=,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!=-)f[][poi].father=lastt;
for(int i=last[poi];i;i=edge[i].last)
if(edge[i].to!=lastt)dfs(edge[i].to,poi,de+);
} void update(int pos,int cen)
{
int i=,j=;
while(i<=f[cen-][f[cen-][pos].father].num[]&&j<=f[cen-][pos].num[]&&f[cen][pos].num[]<)
{
if(i<=f[cen-][f[cen-][pos].father].num[]&&f[cen-][f[cen-][pos].father].num[i]<f[cen-][pos].num[j])f[cen][pos].num[++f[cen][pos].num[]]=f[cen-][f[cen-][pos].father].num[i++];
else if(j<=f[cen-][pos].num[])f[cen][pos].num[++f[cen][pos].num[]]=f[cen-][pos].num[j++];
}
while(i<=f[cen-][f[cen-][pos].father].num[]&&f[cen][pos].num[]<)f[cen][pos].num[++f[cen][pos].num[]]=f[cen-][f[cen-][pos].father].num[i++];
while(j<=f[cen-][pos].num[]&&f[cen][pos].num[]<)f[cen][pos].num[++f[cen][pos].num[]]=f[cen-][pos].num[j++];
} void pre()
{
for(int i=;(<<i)<=n;i++)
for(int j=;j<=n;j++)
if(f[i-][f[i-][j].father].father)f[i][j].father=f[i-][f[i-][j].father].father,update(j,i);
} int ANS[],ANS_B[]; void Up(int pos,int cen,int kk)
{
ANS_B[]=;
int i=,j=;
while(i<=ANS[]&&j<=f[cen][pos].num[]&&ANS_B[]<kk)
{
if(i<=ANS[]&&ANS[i]<f[cen][pos].num[j])ANS_B[++ANS_B[]]=ANS[i++];
else if(j<=f[cen][pos].num[])ANS_B[++ANS_B[]]=f[cen][pos].num[j++];
}
while(i<=ANS[]&&ANS_B[]<kk)ANS_B[++ANS_B[]]=ANS[i++];
while(j<=f[cen][pos].num[]&&ANS_B[]<kk)ANS_B[++ANS_B[]]=f[cen][pos].num[j++];
for(int i=;i<=ANS_B[];i++)ANS[i]=ANS_B[i];
} void print()
{
printf("%d",ANS[]);
for(int i=;i<=ANS[];i++)printf(" %d",ANS[i]);
printf("\n");
} void lca(int u,int v,int kk)
{
ANS[]=;
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>=;i--)
if((<<i) & (dep[v]-dep[u]))Up(v,i,kk),v=f[i][v].father;
}
if(u==v)
{
Up(u,,kk);
print();
return;
}
nu=log2(dep[v]);
while(nu!=-)
{
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,,kk);
Up(u,,kk);
Up(f[][u].father,,kk);
print();
} int main()
{
freopen("t.in","r",stdin);
int m,q,u,v,kk;
n=in(),m=in(),q=in();
for(int i=;i<n;i++)
u=in(),v=in(),add(u,v);
for(int i=;i<=m;i++)
{
u=in();
if(f[][u].num[]<)f[][u].num[++f[][u].num[]]=i;
}
dfs(,-,);
pre();
for(int i=;i<=q;i++)
{
u=in(),v=in(),kk=in();
lca(u,v,kk);
}
return ;
}