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 ...
随机推荐
- iOS之一个iOS开发人员完整的学习路线
iOS开发能力 掌握(最好是精通)OC语言和runtime各种细节(读过相关的clang源码和runtime源码为佳).精通基本的framework(Foundation,UIKit等,平时干活用得最 ...
- Struts2知识点小结(二)
一.结果视图的配置 <result name="success">/success.jsp</result> 1.局部结果视图 ...
- WebGL学习笔记(4)
本篇笔记加强了上篇笔记示例代码的程序,实现了使用nodejs-websocket来广播每个玩家的坐标数据并在同一个世界模型中进行多人在线交互. websocket服务端: 安装nodejs与npm,创 ...
- 【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)
There are N children in kindergarten. Miss Li bought them N candies. To make the process more intere ...
- 配置p6spyLog输出sql完整日志
第一步: 配置maven <dependency> <groupid>p6spy</groupid> <artifactid>p6spy< ...
- css表格
今天写某个平台的前端数据展示 主要使用表格展示 正好复习总结一下css的表格 首先说说thead.tbody.tfoot <thead></thead> <tbody&g ...
- Python学习 :格式化输出
方式一:使用占位符 % 常用占位符:% s (s = string 字符串) % d (d = digit 整数(十进制)) % f ( f = float 浮点数) na ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- C语言Windows程序开发—Windows窗口样式与常用控件样式【第04天】
(一)Windows窗口(MDICLIENT)样式介绍 /* Windows窗口样式 */ WS_BORDER //带有边框的窗口 WS_CAPTION //带有标题栏的窗口 WS_CHILD //子 ...
- vue2.0 Axios 的简单用法
安装 使用 npm: $ npm install axios 使用 bower: $ bower install axios 使用 cdn: <script src="https:// ...