#include<bits/stdc++.h>
using namespace std;
#define P(x) push_back(x)
#define V vector<int>
int n,m,q;
V v[100050],a[100050],ans,r[100050][20];
int d[100050],f[100050][20];
void dfs(int x,int p)
{
d[x]=d[p]+1;f[x][0]=p;r[x][0]=a[p];
for(int i=0;i<v[x].size();i++)
if(v[x][i]!=p)dfs(v[x][i],x);
}
void M(V a,V b,V &c)
{
int la=a.size(),lb=b.size(),i=0,j=0;
while(i+j<10&&i+j<la+lb)
{
if(i==la)c.P(b[j++]);
else if(j==lb)c.P(a[i++]);
else if(a[i]<=b[j])c.P(a[i++]);
else c.P(b[j++]);
}
}
void M(V b)
{
for(int i=0,l=b.size();i<l;i++)ans.P(b[i]);
}
void Init()
{
for(int i=1;i<=16;i++)
for(int j=1;j<=n;j++)
{
M(r[j][i-1],r[f[j][i-1]][i-1],r[j][i]);
f[j][i]=f[f[j][i-1]][i-1];
}
}
void Sol()
{
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
ans.clear();
if(d[x]<d[y])swap(x,y);
int rx=x,ry=y;
for(int i=16,dis=d[x]-d[y];i>=0;i--)
if((1<<i)&dis)
{
M(r[x][i]);
x=f[x][i];
}
if(x!=y)
{
for(int i=16;i>=0;i--)if(f[x][i]!=f[y][i])
{
M(r[x][i]);x=f[x][i];
M(r[y][i]);y=f[y][i];
}
M(a[rx]);M(a[ry]);
M(a[f[x][0]]);
}
else M(a[rx]);
k=min((int)ans.size(),k);
sort(ans.begin(),ans.end());
printf("%d",k);
for(int i=0;i<k;i++)printf(" %d",ans[i]);
puts("");
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=1,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
v[x].P(y);
v[y].P(x);
}
for(int i=1,x;i<=m;i++)
{
scanf("%d",&x);
if(a[x].size()<10)a[x].P(i);
}
dfs(1,0);
Init();
while(q--)Sol();
}
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |
1268. Search Suggestions System | 841. Keys and Rooms |
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |