【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一下可知,最远距离点中必有一个是该党派深度最深的一个,那么我们就记下最深的点 ...
随机推荐
- 微信Api
//定义变量 HttpRequest Request; HttpResponse Response; HttpServerUtility Server; HttpContext Context; pr ...
- 在Entity Framework 中执行T-sql语句
从Entity Framework 4开始在ObjectContext对象上提供了2个方法可以直接执行SQL语句:ExecuteStoreQuery<T> 和 ExecuteStoreC ...
- C#关于值类型和引用类型的备忘
值类型 引用类型 内存分配地点 分配在栈中 分配在堆中 效率 效率高,不需要地址转换 效率低,需要进行地址转换 内存回收 使用完后,立即回收 使用完后,不是立即回收,等待GC回收 赋值操作 进行复 ...
- ElasticSearch 模板文件配置
首先是推荐一下参考资料 中文资料:http://kibana.logstash.es/content/elasticsearch/index.html 官方文档:https://www.elastic ...
- setting菜单界面的形成--未优化
代码: first_preference.xml: <?xml version="1.0" encoding="utf-8"?> <Prefe ...
- Thinkpad 小紅點設定
因为我只需要这么多设置,所以就只写这么多了sudo gedit /etc/rc.local echo -n 240 > /sys/devices/platform/i8042/serio1/se ...
- OJ推荐【转】
来自:http://blog.csdn.net/zdp072/article/details/16207111 一. Online Judge简介: Online Judge系统(简称OJ)是一个在线 ...
- psql: 致命错误: 用户 "postgres" Ident 认证失败
RedHat: 问题: psql -U postgres 时出现:psql: 致命错误: 用户 "postgres" Ident 认证失败 解决: 修改 /var/lib/pgs ...
- linux kernel同步机制的思考
在学习内核同步机制的时候,书中介绍了同步方法:原子操作(atomic).自旋锁(spinlock).信号量(semaphore).互斥锁(mutex).完成变量(completion).大内核(BLK ...
- Linux下如何将数据库脚本文件从sh格式变为sql格式
在从事软件开发的过程中,经常会涉及到在Linux下将数据库脚本文件从sh格式变为sql格式的问题.本文以一个实际的脚本文件为例,说明格式转换的过程. 1. sh文件内容 本文中的文件名为 ...