题意概述:

给出一棵N个结点的树,然后有M个居民分散在这棵树的结点上(允许某个结点没有居民)。现在给出一些询问形如u,v,a,定义k=min(x,a),其中x表示的是u->v路径上的居民数量。将所有路径上的居民编号升序排列之后得到序列p1,p2,...,px,要求对于每一组询问,输出k,p1,p2,...,pk。

N,M,Q<=10^5,1<=a<=10.

分析:

实际上这个题是被丢在数据结构作业里面的只是。。。。好像没有这个必要?

分析一波可以发现每个点可能在答案中出现的居民最多只有10个,所以说按照出现的顺序依次把每个点的至多10个居民储存起来,然后倍增的时候用归并排序的思想合并就可以了。时间复杂度O(10nlogn)。

实现过程中注意到一些问题,今后用倍增统计点的信息的时候都用半开半闭就好了,加上对链顶端(x=y时对x,否则x,y一起爬之后对x,y,fa[x][0])的特判就可以做到不重不漏统计(之前都是用来求最值之类的所以没有注意到这个问题)。

所以。。。数据结构是树的意思吗。。。。(感觉听了小火车讲课之后写代码的时候都在各种压长度?)

所以这个时限的4s是输入输出的锅?(实测手写0.5s的事情)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=; int N,M,Q;
struct edge{ int to,next; }E[maxn<<];
int first[maxn],np,dep[maxn],fa[maxn][],info[maxn][][],sz[maxn][];
int ans[],tmp[]; void add_edge(int u,int v)
{
E[++np]=(edge){v,first[u]};
first[u]=np;
}
void data_in()
{
scanf("%d%d%d",&N,&M,&Q);
int x,y;
for(int i=;i<N;i++){
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
for(int i=;i<=M;i++){
scanf("%d",&x);
if(sz[x][]<) info[x][][sz[x][]++]=i;
}
}
int merge(int *a,int *b,int l1,int l2,int *c)
{
int l=,i=,j=;
while(i<l1&&j<l2&&l<) c[l++]=a[i]<b[j]?a[i++]:b[j++];
while(i<l1&&l<) c[l++]=a[i++];
while(j<l2&&l<) c[l++]=b[j++];
return l;
}
void DFS(int i,int f,int d)
{
fa[i][]=f,dep[i]=d;
for(int j=;(<<j)<d;j++){
fa[i][j]=fa[fa[i][j-]][j-];
sz[i][j]=merge(info[i][j-],info[fa[i][j-]][j-],sz[i][j-],sz[fa[i][j-]][j-],info[i][j]);
}
for(int p=first[i];p;p=E[p].next){
int j=E[p].to;
if(j==f) continue;
DFS(j,i,d+);
}
}
void cc(int *n,int &l,int x,int i)
{
l=merge(n,info[x][i],l,sz[x][i],tmp);
for(int k=;k<l;k++) n[k]=tmp[k];
}
void LCA(int x,int y,int *n,int &l)
{
if(dep[x]<dep[y]) swap(x,y);
int len=dep[x]-dep[y]; l=;
for(int i=;(<<i)<=len;i++)
if((<<i)&len){ cc(n,l,x,i); x=fa[x][i]; }
if(x==y){ cc(n,l,x,); return; }
for(int i=;i>=;i--) if(fa[x][i]!=fa[y][i]){
cc(n,l,x,i); cc(n,l,y,i);
x=fa[x][i],y=fa[y][i];
}
cc(n,l,x,); cc(n,l,y,);
cc(n,l,fa[x][],);
}
void work()
{
DFS(,,);
int x,y,a,l;
for(int i=;i<=Q;i++){
scanf("%d%d%d",&x,&y,&a);
LCA(x,y,ans,l);
printf("%d ",min(a,l));
for(int j=;j<min(a,l);j++) printf("%d ",ans[j]);
printf("\n");
}
}
int main()
{
data_in();
work();
return ;
96 }

Codeforces Round #326 Div.1 C.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. 2) D. Duff in Beach dp

    D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...

  3. Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题

    C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  4. Codeforces Round #326 (Div. 2) B. Duff in Love 分解质因数

    B. Duff in Love Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/proble ...

  5. Codeforces Round #326 (Div. 2) A. Duff and Meat 水题

    A. Duff and Meat Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...

  6. Codeforces Round #326 (Div. 2) B Duff in Love 简单数论 姿势涨

    B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  8. 【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 coun ...

  9. Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting

    B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends' phone ...

随机推荐

  1. 轻量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 语句

    *本文中所用类声明见上一篇博文<轻量ORM-SqlRepoEx (三)Select语句>中Customers类 一.增加记录 1.工厂一个实例仓储 var repository = Rep ...

  2. RMAN备份与恢复(三)--备份相关概念

    (1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件: --数据文件:备份指定的一个或多个数据文件: --表空间:备份指定的一个或多个表空间: --归档重 ...

  3. 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释

    原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml       标题所代表的意思为 使用操作系统本地验证,一般不 ...

  4. mac 开启mysql日志

    step1: 进入终端进入mysql: step2 : 开启mysql日志 step3 : 查看mysql的日志文件所在位置 step4 : 在终端中用tail -f 命令打开该日志文件:

  5. Linux系统文件和目录的属性及权限

    1 文件属性概述 Linux系统中的文件或目录的属性主要包括:索引节点(inode).文件类型.权限属性.硬链接数.所归属的用户和用户组.最近修改时间等内容(文件名严格来说不属于文件的属性): 下面是 ...

  6. over开窗函数的用法

    over(partition by c1.pmid,d1.type,e1.objid  order by e1.objid ) pinum 先根据字段排序,pinum.在取第一条数据and p1.pi ...

  7. 堆数据结构(heapq)简单应用

    ## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...

  8. Yii 2.0.6 - 从入口到Action执行

    defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); r ...

  9. python计算MD5

    python有自带的MD5模块hashlib,用起来简单很多.Python Hashlib模块的使用说明 http://docs.python.org/2/library/hashlib.htmlfd ...

  10. Hadoop(19)-MapReduce框架原理-Combiner合并

    1. Combiner概述 2. 自定义Combiner实现步骤 1). 定义一个Combiner继承Reducer,重写reduce方法 public class WordcountCombiner ...