大意: 给定n结点树, m个询问, 每次给出两个旅馆的位置, 求树上所有结点到最近旅馆距离的最大值

先考虑一些简单情形.

若旅馆只有一个的话, 显然到旅馆最远的点是直径端点之一

若树为链的话, 显然是直径端点或两旅馆的中点

这样的话思路就来了, 也就是说最远点要么是直径端点, 要么是到两旅馆距离差不超过1的点

实际求的时候并不需要求出具体在那个点取得最大, 只需简单分类讨论一下, 用RMQ预处理一下, O(1)回答询问

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
using namespace std; const int N = 1e5+10, INF = 0x3f3f3f3f;
int n, m, r1, r2, mx;
vector<int> g[N];
int vis[N], fa[N], s[N], dep[N], no[N];
int Log[N], w[N], f[2][20][N]; int bfs(int x) {
memset(vis, 0, sizeof vis);
queue<int> q;
fa[x] = 0, vis[x] = 1, q.push(x);
while (!q.empty()) {
x = q.front();q.pop();
for (int y:g[x]) if (!vis[y]) {
vis[y] = 1, fa[y]=x, q.push(y);
}
}
return x;
} void dfs(int x, int fa, int d, int rt) {
dep[x]=d, no[x]=rt, w[rt] = max(w[rt], d);
for (int y:g[x]) if (!vis[y]&&y!=fa) {
dfs(y,x,d+1,rt);
}
} int RMQ(int l, int r, int p) {
if (l>r) return -INF;
int t = Log[r-l+1];
return max(f[p][t][l],f[p][t][r-(1<<t)+1]);
} int main() {
scanf("%d", &n);
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
r1 = bfs(1), r2 = bfs(r1);
memset(vis, 0, sizeof vis);
for (int i=r2; i; i=fa[i]) {
vis[i]=1, s[++*s]=i, no[i]=*s;
}
REP(i,1,*s) dfs(s[i],0,0,i);
Log[0] = -1;
memset(f,-63,sizeof f);
REP(i,1,*s) {
f[0][0][i]=w[i]+i,f[1][0][i]=w[i]-i;
Log[i] = Log[i>>1]+1;
}
REP(j,1,19) for (int i=1;i+(1<<j-1)<=*s; ++i) {
f[0][j][i] = max(f[0][j-1][i],f[0][j-1][i+(1<<j-1)]);
f[1][j][i] = max(f[1][j-1][i],f[1][j-1][i+(1<<j-1)]);
}
scanf("%d", &m);
REP(i,1,m) {
int x, y, ans;
scanf("%d%d", &x, &y);
int l=no[x], r=no[y];
if (l>r) swap(l,r),swap(x,y);
if (l==r) {
printf("%d\n", max(l-1,*s-l)+min(dep[x],dep[y]));
continue;
}
int mid = l+r-dep[x]+dep[y];
if (mid<=2*l) ans = max(r-1,*s-r)+dep[y];
else if (mid>=2*r) ans = max(l-1,*s-l)+dep[x];
else {
mid /= 2;
int L=max(l-1,RMQ(l+1,mid,0)-l)+dep[x];
int R=max(*s-r,RMQ(mid+1,r-1,1)+r)+dep[y];
ans = max(L,R);
}
printf("%d\n", ans);
}
}

Big Problems for Organizers CodeForces - 418D (贪心,直径)的更多相关文章

  1. Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]

    题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...

  2. @codeforces - 418D@ Big Problems for Organizers

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个点连成一棵树,经过每条边需要花费 1 个单位时间. 现给出 ...

  3. CF418D Big Problems for Organizers 树的直径、ST表

    题目传送门:http://codeforces.com/problemset/problem/418/D 大意:给出一棵有$N$个节点的树,所有树边边权为$1$,给出$M$次询问,每个询问给出$x,y ...

  4. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  5. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  6. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  7. CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  8. C - Ordering Pizza CodeForces - 867C 贪心 经典

    C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...

  9. Codeforces 570C 贪心

    题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...

随机推荐

  1. 多线程---ReentrantLock

    package com.test; import java.util.Collection; import java.util.concurrent.locks.Lock; import java.u ...

  2. c++第五天:默认初始化

    1.算数类型.(整型和浮点型) 类型决定了数据所占的比特数以及该如何解释这些比特的内容. 练习2.1... 各种类型在计算机中所占的比特数不同,解释方法不同.有符号要花费一个比特存储符号,最大正值要比 ...

  3. python文件操作-r、w、a、r+、w+、a+和b模式

    对文件操作的基本步骤 f=open('a.txt','r',encoding='utf-8') data=f.read() print(data) f.close() 文件的打开和关闭使用open() ...

  4. JavaScript 创建动态表格

    JavaScript 创建动态表格 版权声明:未经授权,严禁转载! 案例代码 <div id="data"></div> <script> va ...

  5. JavaScript 中语法规范及调试

    JavaScript 中语法规范及调试 版权声明:未经博主授权,内容严禁分享转载 JavaScript 开发环境 JavaScript 脚本可以使用任意一款纯文本编辑器进行编程开发. 常见的前端开发编 ...

  6. FTP-FileZilla

    服务器上安装FileZilla Server连接时报You appear to be behind a NAT router. Please configure the passive mode se ...

  7. Android widget

    1,TextView :走马灯效果 2,EditText ,AutoCompleteText MutiAutoCompleteTextView 3,Button,ImageButton,RadioBu ...

  8. 托管C++调用C#

    拿到了一个第三方demo,有dll,有.cpp..h,打开解决方案,如下图: 网上资料貌似很少,根据猜测: 这是使用托管C++来调用C#的方式. 过程: 1.先使用C#代码实现界面和功能,其实就是一个 ...

  9. ZOJ 2083 Win the Game(SG函数)题解

    题意:给一端n块的板,两人玩,每次能涂相邻两块没涂过的板,不能涂的人为输,先手赢输出yes 思路:sg函数打表,练习题 代码: #include<queue> #include<cs ...

  10. VS中自动选择x86或x64的dll

    http://www.cnblogs.com/lzjsky/archive/2010/09/06/1819321.html 原来使用Win7的32位系统,进行C#工程的开发,后来重装系统,换成了win ...