SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)
Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a way that can go from any intersection to any other intersection moving along some of these roads.
Every day he starts walking in the city following a simple strategy; if he's at some intersection he has to pick one of the roads connected to it at random such that he hasn't walked through it before and walk through it and and if there isn't any, he stops and goes home.
His only problem is that he's afraid of dogs. He doesn't even like seeing dogs. So he's wondering in the worst scenario, how many dogs he'll have to see during his walk until he stops if he starts walking at some intersection. Can you help him?
Input
The input starts with an integer T (1 <= T <= 10), the number of test cases. following T blocks describing each test case.
Each block starts with a line containing an integer n (2 <= n <= 105), the number of intersections in the city. Intersections are numbers 1 through n.
Followed by n-1 lines each containing integers u, v, (1 <= u, v <= n) and d (1 <= d <= 109), the numbers of intersections at the end of this road and the number od dogs Eagle will see walking in this road.
Output
For each test case print a line containing n integers, the ith integer represents the maximum number of dogs Eagle might see if he starts his walk at intersection i.
Example
Input:
1
4
1 2 3
3 2 4
3 4 5
Output:
12 9 7 12
题意:问树上每个点最远可以走到哪里,不能回走。
结论:先走树的直径,那么最远路的终点一定是直径的端点,所以从树的直径的端点dfs两次得到距离,较大的一个就是最远距离。
(不过我队友用DP过了此题,ORZ,后面附图。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<],cost[maxn<<],cnt,S,T;
long long ans[maxn],dis[maxn];
void read(int &x){
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c>=''&&c<='') x=(x<<)+(x<<)+c-'',c=getchar();
}
void add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
cost[cnt]=d;
}
void dfs(int u,int fa)
{
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=fa){
dis[To[i]]=dis[u]+cost[i];
dfs(To[i],u);
}
}
}
int main()
{
int Case,N,u,v,d,i,j;
scanf("%d",&Case);
while(Case--){
scanf("%d",&N); cnt=; S=T=;
for(i=;i<=N;i++) ans[i]=Laxt[i]=;
for(i=;i<N;i++){
read(u); read(v); read(d);
add(u,v,d); add(v,u,d);
}
dis[]=; dfs(,);
for(i=;i<=N;i++) if(dis[i]>dis[S]) S=i;
dis[S]=; dfs(S,);
for(i=;i<=N;i++) {
if(dis[i]>dis[T]) T=i;
if(dis[i]>ans[i]) ans[i]=dis[i];
}
dis[T]=; dfs(T,);
for(i=;i<=N;i++)
if(dis[i]>ans[i]) ans[i]=dis[i];
for(i=;i<N;i++) printf("%lld ",ans[i]);
printf("%lld\n",ans[N]);
}
return ;
}

SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)的更多相关文章
- cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
2450. 距离 ★★ 输入文件:distance.in 输出文件:distance.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个村子里有N个房子,一 ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12276 Accepted: 3886 Description ...
- 牛客小白月赛6 C 桃花 dfs 求树上最长直径
链接:https://www.nowcoder.com/acm/contest/136/C来源:牛客网 题目描述 桃花一簇开无主,可爱深红映浅红. ...
- [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)
[51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...
- 求树上任意一点所能到达的最远距离 - 树上dp
A school bought the first computer some time ago(so this computer's id is 1). During the recent year ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
随机推荐
- 牛客练习赛1 矩阵 字符串二维hash+二分
题目 https://ac.nowcoder.com/acm/contest/2?&headNav=www#question 解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash ...
- HDU 4341 Gold miner(分组背包)
题目链接 Gold miner 目标是要在规定时间内获得的价值总和要尽可能大. 我们先用并查集把斜率相同的物品分在同一个组. 这些组里的物品按照y坐标的大小升序排序. 如果组内的一个物品被选取了,那该 ...
- 关于FileZilla Server的安装问题
网上很多FileZilla Server教程到最后一步在本机上测试访问成功就没了,实际上还是不完整的,一般情况下外网还是访问不了,外网访问同样很重要. 可能有点童鞋会说安装的时候防火墙提示的时候我也点 ...
- DFS与BFS对比
前面已经说过了深搜和广搜了,是不是有点还不是很好的分清他们?(其实分不分的请都没大有关系) 下面我们来看一看广搜与深搜的区别吧. 算法步骤上的区别 深度优先遍历图算法步骤: 1.访问顶点v 2,.依次 ...
- jfinal使用idea启动 访问报404 action not found
公司一个项目,在eclipse里面启动正常,换到idea里面启动后,启动没有报错,但是访问的时候会提示404 action not found. 百度了很多种解决方法 都没有解决. 今天脑子一转,想到 ...
- Chrome查看同步状态
最近Hosts不太稳定,翻出去之后安装了一些插件,那么会面临一些问题,比如插件是否已经同步成功,其它PC能否获取等等. 下面是一些查询同步状态的入口: https://www.google.com/s ...
- hadoop-mapreduce中reducetask执行分析
ReduceTask的执行 Reduce处理程序中须要运行三个类型的处理, 1.copy,从各map中copy数据过来 2.sort,对数据进行排序操作. 3.reduce,运行业务逻辑的处理. Re ...
- CVE-2014-4114 和 CVE-2014-3566
这两天关注安全的人员都会特别留意这两个新披露的漏洞:CVE-2014-4114 和 CVE-2014-3566.以下我们就针对这两个漏洞最一些简要说明. CVE-2014-4114------- ...
- 微信小程序 - 提取字体图标与其优化
微信小程序,无论是字体图标还是图标,都差不多,只不过是为了以后字体图标修改方便,或者加效果方便而使用它而已! 1. 下载font-awesome http://fontawesome.dashgame ...
- mysqldbcopy 数据库复制工具
命令参考 mysqldbcopy --source=root:'xxxxxxx'@database s --destination=root:'^%xxxxxz'@databases orange:o ...