[CC-TRIPS]Children Trips
[CC-TRIPS]Children Trips
题目大意:
\(n(n\le10^5)\)座城市构成一棵树,且树上的每条边的长度\(l_i\)满足\(1\le l_i\le 2\)。\(m(m\le10^5)\)个询问,每次询问从\(u\)到\(v\),每天最多开\(p\)公里,至少需要多少天可以到达。注意,晚上必须停留在某座城市,而不能将车停在某两座城市之间。
思路:
当\(p\le100\)时,倍增预处理每个点向上跳到哪些位置,否则直接暴力跳。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,logN=17,M=1e5,B=100;
struct Edge {
int to,w;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &w) {
e[u].push_back((Edge){v,w});
e[v].push_back((Edge){u,w});
}
struct Query {
int u,v,p,id;
bool operator < (const Query &rhs) const {
return p<rhs.p;
}
};
Query q[M];
int ans[M];
int dep[N],dis[N],anc[N][logN],top[N][logN];
inline int lg2(const float &x) {
return ((unsigned&)x>>23&255)-127;
}
void dfs(const int &x,const int &par) {
anc[x][0]=par;
dep[x]=dep[par]+1;
for(register int i=1;i<=lg2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-1]][i-1];
}
for(auto &j:e[x]) {
const int &y=j.to;
if(y==par) continue;
dis[y]=dis[x]+j.w;
dfs(y,x);
}
}
inline int lca(int x,int y) {
if(dep[x]<dep[y]) std::swap(x,y);
for(register int i=lg2(dep[x]-dep[y]);i>=0;i--) {
if(dep[anc[x][i]]>=dep[y]) {
x=anc[x][i];
}
}
for(register int i=lg2(dep[x]);i>=0;i--) {
if(anc[x][i]!=anc[y][i]) {
x=anc[x][i];
y=anc[y][i];
}
}
return x==y?x:anc[x][0];
}
inline int dist(const int &x,const int &y) {
const int z=lca(x,y);
return dis[x]+dis[y]-dis[z]*2;
}
inline int jump(int x,int step) {
for(register int i=lg2(dep[x]);i>=0;i--) {
if(dis[x]-dis[anc[x][i]]<=step) {
step-=dis[x]-dis[anc[x][i]];
x=anc[x][i];
}
}
return x;
}
int main() {
const int n=getint();
for(register int i=1;i<n;i++) {
const int u=getint(),v=getint(),w=getint();
add_edge(u,v,w);
}
dfs(1,0);
const int m=getint();
for(register int i=0;i<m;i++) {
q[i].u=getint();
q[i].v=getint();
q[i].p=getint();
q[i].id=i;
}
std::sort(&q[0],&q[m]);
for(register int l=0,r=0;l<m;l=r) {
while(r<m&&q[r].p==q[l].p) r++;
const int &step=q[l].p;
if(step<=B) {
memset(top,0,sizeof top);
for(register int i=1;i<=n;i++) {
top[i][0]=jump(i,step);
}
for(register int j=1;j<=lg2(n);j++) {
for(register int i=1;i<=n;i++) {
top[i][j]=top[top[i][j-1]][j-1];
}
}
for(register int i=l;i<r;i++) {
int x=q[i].u,y=q[i].v;
const int z=lca(x,y);
int &ans=::ans[q[i].id];
for(register int i=lg2(dep[x]);i>=0;i--) {
if(dep[top[x][i]]>dep[z]) {
x=top[x][i];
ans+=1<<i;
}
}
for(register int i=lg2(dep[y]);i>=0;i--) {
if(dep[top[y][i]]>dep[z]) {
y=top[y][i];
ans+=1<<i;
}
}
if(dist(x,y)>0) ans++;
if(dist(x,y)>step) ans++;
}
} else {
for(register int i=l;i<r;i++) {
int x=q[i].u,y=q[i].v,t;
const int z=lca(x,y);
int &ans=::ans[q[i].id];
for(t=jump(x,step);dep[t]>dep[z];t=jump(x,step)) {
ans++;
x=t;
}
for(t=jump(y,step);dep[t]>dep[z];t=jump(y,step)) {
ans++;
y=t;
}
if(dist(x,y)>0) ans++;
if(dist(x,y)>step) ans++;
}
}
}
for(register int i=0;i<m;i++) {
printf("%d\n",ans[i]);
}
return 0;
}
[CC-TRIPS]Children Trips的更多相关文章
- Codechef TRIPS Children Trips (分块、倍增)
题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...
- 【codechef】Children Trips
Portal -->CC_Children Trips Solution (英文题解看得真爽qwq不过写的好详细啊ovo) 首先这题有一个很重要的条件就是边权是\(1\)或者\(2\),所以虽然 ...
- CODECHEF Oct. Challenge 2014 Children Trips
@(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...
- 【CODECHEF】Children Trips 倍增
此题绝了,$O(n^{1.5}\ log\ n)$都可以过掉.... 题目大意:给你一颗$n$个点的树,每条边边权不是2就是$1$,有$m$个询问,每次询问一个人从$x$点走到$y$点,每天可以走的里 ...
- 题解 Children Trips
题目传送门 Description 给出一个大小为 \(n\) 的边权全为 \(1,2\) 的带权树,有 \(q\) 此查询,每次给出 \(u,v,p\) ,问 \(u\to v\) 每次可以最多走边 ...
- (转载)构建public APIs与CORS
from: https://segmentfault.com/a/1190000000709909 理由:在操作层面详细的讲解了跨域的操作.尤其是对于option请求的详解.收藏. 在构建Public ...
- easyui扩展-日期范围选择.
参考: http://www.5imvc.com/Rep https://github.com/dangrossman/bootstrap-daterangepicker * 特性: * (1)基本功 ...
- [easyui] datebox源码阅读. 批注
jquery.datebox.js 文件. (function($){ /** * create date box */ function createBox(target){ var state = ...
- easyui源码翻译1.32--TreeGrid(树形表格)
前言 扩展自$.fn.datagrid.defaults.使用$.fn.treegrid.defaults重写默认值对象.下载该插件翻译源码 树形表格用于显示分层数据表格.它是基于数据表格.组合树控件 ...
随机推荐
- php 日期格式转换万能公式
思路用strtotime转换时间的字符串 $t='2017-03-09 02:30'; echo(date('Y-m-d H-i', strtotime($t)));
- 自动化监控白皮书——WAS监控
WebSphere(WAS)是一些大型企业常用的中间件,由于was自身提供的工具有时不能满足多样化的监控需求,而我们又会经常遇到对was进行监控的应用场景,所以我们有必要自己动手做一些was的监控脚本 ...
- 通达OA2008从windows环境移植到linux部署手册
通达OA2008从windows环境移植到linux中(centos5.5及以上版本) OA系统拓扑图: 环境搭建(安装lamp环境) 1.安装xampp集成lamp包xampp-linux-1.6. ...
- 同时装了Python3和Python2,怎么用pip
作者:匿名用户链接:https://www.zhihu.com/question/21653286/answer/95532074来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Day5----------------------文件合并与文件归档
一.文件合并 1.命令: >:覆盖式 >>:追加式 例如:cat /etc/passwd > a.txt 把/etc/passwd下的内容合并到a.txt内,若没有文 ...
- python之类中的super函数
作用 实现代码重用 思考:super真的只是调用父类么? super函数是按照mro算法去调用的,不bb上代码: class A: def __init__(self): print('A') cla ...
- Flask之数据库设置
4 数据库 知识点 Flask-SQLALchemy安装 连接数据库 使用数据库 数据库迁移 邮件扩展 4.1 数据库的设置 Web应用中普遍使用的是关系模型的数据库,关系型数据库把所有的数据都存储在 ...
- java多线程快速入门(六)
多线程应用实例(批量发送短信) 1.创建实体类 package com.cppdy; public class UserEntity { private int id; private String ...
- bzoj2152 树分治
还是太菜了,自己写的wa,但是找不到哪里错了,, 感觉现在学树分治早了点..以后回来再看吧 /* 多少点对之间的路径是3的倍数 */ #include<iostream> #include ...
- Lucene.Net简单例子-01
前面已经简单介绍了Lucene.Net,下面来看一个实际的例子 1.1 引用必要的bll文件.这里不再介绍(Lucene.Net PanGu PanGu.HightLight PanGu.Luc ...