【LCA】CodeForce #326 Div.2 E:Duff in the Army
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 ;
}
【LCA】CodeForce #326 Div.2 E:Duff in the Army的更多相关文章
- 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个 ...
- Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增
题意概述: 给出一棵N个结点的树,然后有M个居民分散在这棵树的结点上(允许某个结点没有居民).现在给出一些询问形如u,v,a,定义k=min(x,a),其中x表示的是u->v路径上的居民数量.将 ...
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【LCA】bzoj 2144:跳跳棋
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 248 Solved: 121[Submit][Status][Discuss] ...
- CDOJ 92 – Journey 【LCA】
[题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...
- 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序列区间里,深度最 ...
- 【洛谷】1600:天天爱跑步【LCA】【开桶】【容斥】【推式子】
P1600 天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个 ...
- 【LCA】BZOJ1776-[Usaco2010 Hol]cowpol 奶牛政坛
[题目大意] 一棵n个点的树,树上每个点属于一个党派,要求每个党派的最远距离点.两点间距离为两点间边的个数. [思路] yy一下可知,最远距离点中必有一个是该党派深度最深的一个,那么我们就记下最深的点 ...
随机推荐
- DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):
一.DataGridView属性设置 1.我们单击选中行的时候,默认是选择一个单元格,不能选择一整行,我们只需设置DataGridView的属性SelectionMode为FullRowSelect ...
- scan的filter使用
本次操作的hbase的t1表的数据是: hbase(main)::> scan 't1' ROW COLUMN+CELL column=f1:age, timestamp=, value= co ...
- HBase分布式安装
安装HBase之前需要先安装Hadoop,因为HBase是运行在Hadoop集群上的.安装Hadoop可以参照http://www.cnblogs.com/stGeekpower/p/3307289. ...
- PHP MYSQLI中事务处理
MYSQL事务处理让所有sql语句执行成功后才去处理,如果有一条没有成功或者报错就会回滚事务,防止敏感操作处理失败.MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!其它类型是不支持的 ...
- linux服务器之LVS、Nginx和HAProxy负载均衡器对比
linux服务器之LVS.Nginx和HAProxy负载均衡器对比. LVS特点: 1.抗负载能力强,使用IP负载均衡技术,只做分发,所以LVS本身并没有多少流量产生: 2.稳定性.可靠性好,自身 ...
- 【积硅计划】html标签
一.基础 在html中,标签通常是由开始标签和结束标签组成,开始标签用<标签名>,结束标签用</标签名> 元素是指包括开始标签和结束标签在内的整体,内容是指出 ...
- 主键、外键、超键、候选键的区别【Written By KillerLegend】
先说一下属性的定义: 表的每一行对应一个元组,表的每一列对应一个域.由于域可以相同,为了加以区分,必须对每列起一个唯一的名字,称为属性(Attribute). 再来看看几个键的定义: 超键:在关系模式 ...
- DataTable使用总结
1.DataTable数据去重 static void Main(string[] args) { DataTable dt = new DataTable();//创建表 DataColumn dc ...
- 使用WIF实现单点登录Part IV —— 常见问题
InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException (有关详细 ...
- POJ——3984
走迷宫问题,POJ上面的题 #include <stdio.h> #include <stdlib.h> #define SIZE 5 bool findpath = fals ...