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. 文本处理命令--cut、sort、join

    声明:下面介绍的只是命令的常用选项,如果需要详细了解命令全部细节,需要参考其他的资料. 一.cut cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进 ...

  2. phpcms v9 源码解析-1 index.php

    这个是phpcms V9 的入口文件index.php. V9程序的执行绝大多数是从这个文件开始的,但不绝对,在项目下面的api.php和plugin.php是另外的入口文件,这里我们先不做深究. 在 ...

  3. 用Python的smtp模块发邮件的示例

    # -*- coding: UTF-8 -*- import smtplib from email.mime.multipart import MIMEMultipart from email.mim ...

  4. DevExpress 中 在做全选的全消功能的时候 加快效率

    在做 DevExpress 中对增加的选择 Check列 控制全选的全消时通过以下代码红色字代码效率会有明显的提升: private void CheckedRow() { try { splashS ...

  5. How to move the user document folder to D disk[Windows 7]

    when you install windows 7 OS, the system ask for you enter username and password, then you have not ...

  6. Python之Flask Web开发

    下载python包管理工具Pip: 访问网址:https://pip.pypa.io/en/stable/installing/    下载文件get-pip.py到本地计算机 定位到get-pip. ...

  7. 学习simple.data之基础篇

    simple.data是一个轻量级的.动态的数据访问组件,支持.net4.0. 1.必须条件和依赖性: v4.0 or greater of the .NET framework, or v2.10 ...

  8. MHA 日常管理

    一.MHA的主要脚本 1.manager端 masterha_check_ssh 检查MHA的SSH配置状况 masterha_check_repl 检查MySQL复制状况 masterha_mang ...

  9. STM32F4_引领入门

    Ⅰ.概述 该文写给那些想学ST芯片开发(或初级学习)的朋友,文章着重细节,或许有点简单. 笔者想告诉那些刚开始学习ST的朋友,不管你使用哪一个系列(F0.F1.F2),哪一种型号芯片,其实学习的方法和 ...

  10. 浅谈Objective—C中的面向对象特性

    Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...