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

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打挂了。。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<stack>
  6 #include<vector>
  7 
  8 #define maxn 100001
  9 
 10 using namespace std;
 11 
 12 inline int in()
 13 {
 14     int x=0,f=1;char ch=getchar();
 15     while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
 16     if(ch=='-')f=-1;
 17     while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
 18     return f*x;
 19 }
 20 
 21 struct ed{
 22     int to,last;
 23 }edge[maxn*2];
 24 
 25 struct lc{
 26     int father,num[11];
 27 }f[19][maxn];
 28 
 29 int last[maxn],tot=0,dep[maxn],n;
 30 
 31 void add(int u,int v)
 32 {
 33     edge[++tot].to=v,edge[tot].last=last[u],last[u]=tot;
 34     edge[++tot].to=u,edge[tot].last=last[v],last[v]=tot;
 35 }
 36 
 37 void dfs(int poi,int lastt,int de)
 38 {
 39     dep[poi]=de;
 40     if(lastt!=-1)f[0][poi].father=lastt;
 41     for(int i=last[poi];i;i=edge[i].last)
 42         if(edge[i].to!=lastt)dfs(edge[i].to,poi,de+1);
 43 }
 44 
 45 void update(int pos,int cen)
 46 {
 47     int i=1,j=1;
 48     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)
 49     {
 50         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++];
 51         else if(j<=f[cen-1][pos].num[0])f[cen][pos].num[++f[cen][pos].num[0]]=f[cen-1][pos].num[j++];
 52     }
 53     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++];
 54     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++];
 55 }
 56 
 57 void pre()
 58 {
 59     for(int i=1;(1<<i)<=n;i++)
 60         for(int j=1;j<=n;j++)
 61             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);
 62 }
 63 
 64 int ANS[11],ANS_B[11];
 65 
 66 void Up(int pos,int cen,int kk)
 67 {
 68     ANS_B[0]=0;
 69     int i=1,j=1;
 70     while(i<=ANS[0]&&j<=f[cen][pos].num[0]&&ANS_B[0]<kk)
 71     {
 72         if(i<=ANS[0]&&ANS[i]<f[cen][pos].num[j])ANS_B[++ANS_B[0]]=ANS[i++];
 73         else if(j<=f[cen][pos].num[0])ANS_B[++ANS_B[0]]=f[cen][pos].num[j++];
 74     }
 75     while(i<=ANS[0]&&ANS_B[0]<kk)ANS_B[++ANS_B[0]]=ANS[i++];
 76     while(j<=f[cen][pos].num[0]&&ANS_B[0]<kk)ANS_B[++ANS_B[0]]=f[cen][pos].num[j++];
 77     for(int i=0;i<=ANS_B[0];i++)ANS[i]=ANS_B[i];
 78 }
 79 
 80 void print()
 81 {
 82     printf("%d",ANS[0]);
 83     for(int i=1;i<=ANS[0];i++)printf(" %d",ANS[i]);
 84     printf("
");
 85 }
 86 
 87 void lca(int u,int v,int kk)
 88 {
 89     ANS[0]=0;
 90     int nu;
 91     if(dep[u]>dep[v])swap(u,v);
 92     if(dep[v]!=dep[u])
 93     {    
 94         nu=log2(dep[v]-dep[u]);
 95         for(int i=nu;i>=0;i--)
 96             if((1<<i) & (dep[v]-dep[u]))Up(v,i,kk),v=f[i][v].father;
 97     }
 98     if(u==v)
 99     {
100         Up(u,0,kk);
101         print();
102         return;
103     }
104     nu=log2(dep[v]);
105     while(nu!=-1)
106     {
107         if(f[nu][u].father==f[nu][v].father){nu--;continue;}
108         Up(v,nu,kk);
109         Up(u,nu,kk);
110         u=f[nu][u].father;
111         v=f[nu][v].father;
112         nu--;
113     }
114     Up(v,0,kk);
115     Up(u,0,kk);
116     Up(f[0][u].father,0,kk);
117     print();
118 }
119 
120 int main()
121 {
122     freopen("t.in","r",stdin);
123     int m,q,u,v,kk;
124     n=in(),m=in(),q=in();
125     for(int i=1;i<n;i++)
126         u=in(),v=in(),add(u,v);
127     for(int i=1;i<=m;i++)
128     {
129         u=in();
130         if(f[0][u].num[0]<10)f[0][u].num[++f[0][u].num[0]]=i;
131     }
132     dfs(1,-1,0);
133     pre();
134     for(int i=1;i<=q;i++)
135     {
136         u=in(),v=in(),kk=in();
137         lca(u,v,kk);
138     }
139     return 0;
140 }
View Code
原文地址:https://www.cnblogs.com/tuigou/p/4885916.html