C - Roads in the North DFS+树的直径】的更多相关文章

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. Give…
Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 1157 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such…
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次长距离, 然后某个点的最长+次长就是直径. #include<stdio.h> #include<vector> #include<algorithm> #include<string.h> #include<iostream> using name…
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. Give…
题解: 求树的直径. 转一篇博客:http://www.cnblogs.com/hanyulcf/archive/2010/10/23/tree_radius.html 树的直径是指树的最长简单路.求法: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径:              原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点              证明: 1) 如果u 是直径上的点,则v显然是直径的终点(…
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进行缩点,同色连通块当作一点,用dfs实现并得到新图.答案即为(最长直径+1)/2. 关于最长直径的求法:http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html #include<bits/stdc++.h> using names…
楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图很容易看出来,这个题实际上要把每个三角形当作点,而把有临边的三角形连边,这样我们就可以大体上得到一棵树,至于怎么连这个边,使用map和pair进行操作,具体说来很麻烦,可以上网某度以下. 建完图了之后,不难看出,我们要的结果就是建出来的这个树的最长链,然后就是找到一个最长链的端点的问题.由于这是一棵…
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does…
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径) 分析: 对于树的直径问题, <<算法导论>>(22 2-7)例题有说明. 详细解法: 首先从树上随意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b…
Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 2157 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such…