Codeforces Round #326 Div.1 C.Duff in the Army 树上倍增
题意概述:
给出一棵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 树上倍增的更多相关文章
- 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. 2) D. Duff in Beach dp
D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
随机推荐
- java和c通信相关的数据类型转换
利用socket进行网络传输的时候往往需要将int转换为bytes,将string转换为bytes以及一些其他类型的数据转换 java和c类型的区别: 变量类型 C中字节数 Java中字节数 int ...
- iOS 直播类APP开发流程解析
1 . 音视频处理的一般流程: 数据采集→数据编码→数据传输(流媒体服务器) →解码数据→播放显示1.数据采集:摄像机及拾音器收集视频及音频数据,此时得到的为原始数据涉及技术或协议:摄像机:CCD.C ...
- iOS之一些实用的Demo
图像浏览及处理 FLAnimatedImage - gif播放处理的工具. CLImageEditor - 超强的图片编辑库,快速帮你实现旋转,防缩,滤镜等等一系列麻烦的事情. ios-image-f ...
- iOS之某公司iOS开发笔试题
参考答案不唯一,大家可以根据自己的理解回答,没有必要跟笔者的一样.参考笔者的答案,也许给你带来灵感! 1.对数组中的元素去重复 例如: NSArray *array = @[@"12-11& ...
- onblur事件和click事件冲突
在js中onblur事件的优先级click事件,所以同一个元素上绑定两个事件的时候,onblur事件会冲掉click事件. 解决方案:将click事件改成mousedown事件
- LeetCode 中级 - 有序链表转换二叉搜索树(109)
给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水
链接:https://www.nowcoder.com/acm/contest/90/L 来源:牛客网 1.题目描述 -- 在一个风雨交加的夜晚,来自异世界的不愿透露姓名的TMK同学获得了两种超强药水 ...
- 2822: [AHOI2012]树屋阶梯
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1161 Solved: 694[Submit][Status][Discuss] Descriptio ...
- Intellij IDEA切换maven
问题描述: IDEA自带Maven,但不想用,想用自己安装的. 解决方案: File->Settings(快捷键:Ctrl+Alt+S) 这里分为了两个,竟然还有默认配置一说,上面的只是修改了当 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling
题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...