xdoj-1319 求树上任意一点的最大距离----利用树的直径
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 求树上任意一点的最大距离----利用树的直径的更多相关文章
- 求树上任意一点所能到达的最远距离 - 树上dp
A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...
- [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)
[51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...
- 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治
题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...
- LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))
原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q) ...
- 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- POJ 1985 求树的直径 两边搜OR DP
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...
- Bellman_Ford算法(求一个点到任意一点的最短距离)
单源最短路问题是固定一个起点,求它到任意一点最短路的问题. 记从起点出发到顶点 i 的最短距离为d[i],则有以下等式成立 d[i]=min{d[j]+(从j到 i 的边的权值) 看代码 #inclu ...
- HDU 2376 树形dp|树上任意两点距离和的平均值
原题:http://acm.hdu.edu.cn/showproblem.php?pid=2376 经典问题,求的是树上任意两点和的平均值. 这里我们不能枚举点,这样n^2的复杂度.我们可以枚举每一条 ...
- loj 1257 (求树上每一个点到树上另一个点的最长距离)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1257 思路:首先需要用到一个知识点就是树上任一点到树上最长直径的某一个端点的距离最远, ...
随机推荐
- Spring之缓存注解@Cacheable
https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...
- JS--理解参数,argument,重载
ECMAScript函数的参数与大多数其他语言函数的参数不同.ECMAScript函数不介意传递进来多少个参数,也不在乎传递进来的参数是什么数据类型. 原由在于,ECMAScript中的参数在内部是用 ...
- python 关闭垃圾回收
import gc gc.disable() http://blog.csdn.net/aixiaohei/article/details/6446869
- pycuda安装 python<3.0
cd pycudapython ./configure.py –cuda-root=/usr/local/cuda –cudadrv-lib-dir=/usr/lib –boost-inc-dir=/ ...
- 【框架】Testng用例失败自动重跑(五)
arrow是testng的一个扩展插件,参考arrow的源代码 1.新建一个工程,结果如图: 2.RetryListener.java的代码 package com.netease.qa.testng ...
- Python 自然语言处理笔记(一)
一. NLTK的几个常用函数 1. Concordance 实例如下: >>> text1.concordance("monstrous") Displaying ...
- JavaScript中如何对一个对象进行深度clone
<!doctype html><html><head><meta charset="utf-8"><title>深克隆& ...
- 每天CSS学习之text-align
text-align是CSS的一个属性,其作用是设置文本的对齐方式.其值如下所示: 1.left:文本左对齐.如下所示: div{ text-align:left; } 结果: 2.right:文本右 ...
- TreeMap - 源代码学习笔记
TreeMap 实现了 NavigableMap 接口,而NavigableMap 接口继承于 SortedMap接口. 所有本文还会记录 SortedMap 和 NavigableMap 的阅读笔记 ...
- get 和post 请求的写法
get请求 import requests base_url = 'http://httpbin.org' # 定义请求所需的参数,参数之间以英文逗号隔开 param_data = {'} # 发送G ...