poj 1849 Two】的更多相关文章

/*poj 1849 two 思考一下会发现 就是求直径 直径上的中点就是两个人分开的地方(不再有交集)*/ #include<cstdio> #define maxn 100010 using namespace std; ],sum,M; struct node{ int v,t,pre; }e[maxn*]; int init(){ ,f=;char s=getchar(); ;s=getchar();} +s-';s=getchar();} return x*f; } int max(…
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一个点,那么这两个点就是他的直径的短点,距离就是路径长度.具体证明见http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html 其实这个自己画画图也能理解. POJ 1985 题意:直接让求最长路径. 可以用dfs也可以用bfs bfs代…
POJ 1849 Two(遍历树) http://poj.org/problem?id=1849 题意: 有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, 可是最后机器人不要求回到出发点. 问你两个机器人走的路总长之和的最小值是多少? 分析: 首先本题仅仅要求出树的直径, 然后用树的总长sum*2-树的直径就是所求结果. 以下一步步来说明为什么是这种. 个机器人遍历树,且要求回到原点, 它最少须要走多少路? 答: 它须要走树总长sum的两倍, 即每条树…
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考.并且答案和派出时的起点无关 求最长路两遍dfs或bfs就可以,从随意点bfs一遍找到最长路的一个终点,再从这个终点bfs找到起点 //1032K 79MS C++ 1455B #include<cstdio> #include<iostream> #include<cstrin…
Time Limit: 1000MS Memory Limit: 30000K Description The city consists of intersections and streets that connect them. Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These s…
Two Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1092   Accepted: 527 Description The city consists of intersections and streets that connect them. Heavy snow covered the city so the mayor Milan gave to the winter-service a list of st…
Two 思路: 树形DP求直径: 答案是边权总和*2-直径: dp[i][1]::以i为根的子树中最长的路径: dp[i][0]::以i为根的子树中次长的路径: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 ],V[maxn<<],W[maxn…
如果一个点开始遍历一棵树再回到原点那么每条边走两次. 现在是两个人从同一点出发,那么最后遍历完以后两人离得越远越好. 最后两人所处位置的路径上的边走了一次,其他边走了两次. 要使总路程最小,两人最后停在直径两端. 所以最终答案就是总权值*2 - 树的直径 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> u…
Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798   Special Judge Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets…
Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000…