1 #include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
vector < vector <int> > g(N);
int d1[N],d2[N],d[N];
bool visit[N];
struct node {
int id;
int w;
};
queue <node> q;
int n,m;
void dfs (int rt) {
memset (visit,,sizeof(visit));
while (!q.empty()) q.pop();
node tmp={rt,};
q.push(tmp); visit[tmp.id]=; d[rt]=;
while (!q.empty()) {
tmp=q.front(); q.pop();
for (int i=;i<g[tmp.id].size();i++) {
int _next=g[tmp.id][i];
if (!visit[_next]) {
node k={_next,tmp.w+};
q.push(k); visit[k.id]=; d[_next]=k.w;
}
}
}
return ;
}
int find_max () {
int ans=; int k;
for (int i=;i<=n;i++)
if (d[i]>ans) {
ans=d[i];
k=i;
}
return k;
}
void my_copy (int *a,int *b) {
for (int i=;i<=n;i++)
a[i]=b[i];
}
int main ()
{
int T; scanf ("%d",&T);
while (T--) {
scanf ("%d",&n);
for (int i=;i<=n;i++) g[i].clear();
for (int i=;i<=n-;i++) {
int u,v; scanf ("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
int x1,x2;
dfs (); x1=find_max();
dfs (x1); x2=find_max();
my_copy (d1,d);
dfs (x2);
my_copy (d2,d);
scanf ("%d",&m);
while (m--) {
int k,p; scanf ("%d %d",&k,&p);
int len=max (d1[k],d2[k]);
if (p<=len) printf("%d\n",p+);
else printf("%d\n",min (n,len+(p-len)/+) );
}
}
return ;
}

感谢金牌果善意的提醒~~~树形dp还是有点麻烦。。

xdoj-1319 求树上任意一点的最大距离----利用树的直径的更多相关文章

  1. 求树上任意一点所能到达的最远距离 - 树上dp

    A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...

  2. [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)

    [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...

  3. 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治

    题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...

  4. LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))

    原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q)  ...

  5. 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

  6. POJ 1985 求树的直径 两边搜OR DP

    Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...

  7. Bellman_Ford算法(求一个点到任意一点的最短距离)

    单源最短路问题是固定一个起点,求它到任意一点最短路的问题. 记从起点出发到顶点 i 的最短距离为d[i],则有以下等式成立 d[i]=min{d[j]+(从j到 i 的边的权值) 看代码 #inclu ...

  8. HDU 2376 树形dp|树上任意两点距离和的平均值

    原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...

  9. loj 1257 (求树上每一个点到树上另一个点的最长距离)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1257 思路:首先需要用到一个知识点就是树上任一点到树上最长直径的某一个端点的距离最远, ...

随机推荐

  1. matlab server mapreduce

    >> Z = server.rpc('zeros', 2, 3);>> Z = [2x3 double] [2x3 double] >> Z{1}ans = 0 0 ...

  2. 逆袭之旅DAY16.东软实训.Oracle.修改用户

    2018-07-12 15:49:51

  3. windows 执行bat脚本

    bat脚本中运行另外一个bat脚本 call 命令 call1.bat内容如下 echo running call1 call2.bat内容如下 @echo off echo start call c ...

  4. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  5. 四. Python基础(4)--语法

    四. Python基础(4)--语法 1 ● 比较几种实现循环的代码 i = 1 sum = 0 while i <= 10: # 循环10-1+1=10次     sum += i     i ...

  6. Bootstrap--思维导图

    Bootstrap--思维导图

  7. Centos7下cratedb数据导入导出copy to copy from

    crate 创建表结构: 查看表: show tables; 创建表结构: create table tablename (k1 type,k2 type,k3 type);  (type = int ...

  8. 对称加密-java实现

    主要步骤如下: 1.利用SecretKeyFactory.getInstance("加密算法")创建密钥工厂,加密算法如"DES","AES" ...

  9. 一次Web请求返回406原因与解决方案

    ajax请求,响应信息返回的却是报错406,. 1.断点调试,进入对应处理方法,且得到正确信息返回到解析器.使用的是ssm,前端ftl 2.js将返回错误信息打出来,类似为: 百度406错误出现的原因 ...

  10. tf.trainable_variables() and tf.all_variables()

    tf.trainable_variables()  返回的是 所有需要训练的变量列表 tf.all_variables() 返回的是 所有变量的列表 v = tf.Variable(0, name=' ...