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.

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.

Input

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 ≤ nv ≠ 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).

Output

For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.

Sample test(s)
input
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
output
1 3
2 2 3
0
3 1 2 4
1 2
Note

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 ;
}

【LCA】CodeForce #326 Div.2 E:Duff in the Army的更多相关文章

  1. Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法

    题意:一个n个点的数, m个人住在其中的某些点上, 每个人的标号1-m, 询问u-v 路径上标号前a个人,并输出标号,a < 10. 作法, 利用倍增, ID[j][i] 表示i到i的第2^j个 ...

  2. Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增

    题意概述: 给出一棵N个结点的树,然后有M个居民分散在这棵树的结点上(允许某个结点没有居民).现在给出一些询问形如u,v,a,定义k=min(x,a),其中x表示的是u->v路径上的居民数量.将 ...

  3. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  4. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  5. 【LCA】bzoj 2144:跳跳棋

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 248  Solved: 121[Submit][Status][Discuss] ...

  6. CDOJ 92 – Journey 【LCA】

    [题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...

  7. How far away ? HDU - 2586 【LCA】【RMQ】【java】

    题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...

  8. 【洛谷】1600:天天爱跑步【LCA】【开桶】【容斥】【推式子】

    P1600 天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个 ...

  9. 【LCA】BZOJ1776-[Usaco2010 Hol]cowpol 奶牛政坛

    [题目大意] 一棵n个点的树,树上每个点属于一个党派,要求每个党派的最远距离点.两点间距离为两点间边的个数. [思路] yy一下可知,最远距离点中必有一个是该党派深度最深的一个,那么我们就记下最深的点 ...

随机推荐

  1. 一些CSS"bug"

    1.img三像像素问题 解决办法:img{display:block;} or img{vertical-align:middle;} 问题原因:img是行内元素,默认的垂直对齐方式 baseline ...

  2. sqoop的job工具

    sqoop job: Work with saved jobs 就是将sqoop的某条语句保存为一个job 1.把person表导入到HDFS上,可以使用下面的语句 sqoop import --co ...

  3. NodeManager起不来

    NodeManager无法启动,解除授权,重新授权! CDH需要注意的表: SELECT * FROM `ROLES` SELECT * FROM CONFIGS WHERE attr LIKE '% ...

  4. 使用Polly让程序有Retry的机制

    有时候我们需要调用其他API的时候出现暂时连接不通超时的情况,那这时候可以通过Polly进行Retry. 1.从nuget引用polly, 2.定义需要处理的异常有哪些,比如 Policy.Handl ...

  5. 谈谈final、finally、finalize的区别

    1.final:如果一个类被final修饰,意味着该类不能派生出新的子类,不能作为父类被继承.因此一个类不能被声明为abstract,又被声明为final.将变量或方法声明为final.可以保证他们在 ...

  6. 关于生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法

    System.Drawing.Image OldImage = null; oldImage = System.Drawing.Image.FromFile(ImageUrl); 使用该方法读取图片时 ...

  7. hdu 1316 How Many Fibs?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1316 How Many Fibs? Description Recall the definition ...

  8. JVM学习总结五——性能监控及故障处理工具

    之前扯了四篇理论,这一篇终于可以动动手了.本篇我们将介绍JVM常用的一些工具,这些工具将是我们监控JVM状态.处理故障和调优分析的利器. 不过在开始之前,我还是要先车扯两句:工具终归只是帮助我们我们处 ...

  9. Eclipse基金会

    昨天Eclipse基金会庆祝其成立十周年.2004年2月的新闻稿宣布该非盈利组织的正式成立,由包括开发者.消费者和插件提供商在内的各独立团体组成的董事会,为Eclipse的长期发展负责. 基金会成立时 ...

  10. 面向对象原生js幻灯片代淡出效果

    面向对象原生js幻灯片代淡出效果 下面是代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...