hdu 4514 湫湫系列故事――设计风景线(求树的直径)
现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度。请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?
其中,可以兴建的路线均是双向的,他们之间的长度均大于0。
Input 测试数据有多组,每组测试数据的第一行有两个数字n, m,其含义参见题目描述;
接下去m行,每行3个数字u v w,分别代表这条线路的起点,终点和长度。
TechnicalSpecificationTechnicalSpecification
1. n<=100000
2. m <= 1000000
3. 1<= u, v <= n
4. w <= 1000
Output 对于每组测试数据,如果能够建成环形(并不需要连接上去全部的风景点),那么输出YES,否则输出最长的长度,每组数据输出一行。
Sample Input
3 3
1 2 1
2 3 1
3 1 1
Sample Output
YES
求树的直径,用再次bfs。证明见:树的直径(最长路) 的详细证明(转)
首先先判环,如果有环直接输出YES,用并查集就好。如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存,
还有用C++交用的少一些,我用G++交的卡在32764K,限制是32768K。。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-;
const int maxn = 1e5 + ;
const LL mod = ;
const int N = 1e6 + ;
const int dr[] = {-, , , , , , -, -};
const int dc[] = {, , , -, , -, , -};
const char *Hex[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
inline LL gcd(LL a, LL b){ return b == ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= && r < n && c >= && c < m;
}
vector<P> G[maxn];
int p[maxn], dp[maxn];
bool vis[maxn], viss[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int bfs(int root){
memset(vis, false, sizeof vis);
memset(dp, , sizeof dp);
queue<int> q;
q.push(root);
vis[root] = viss[root] = true;
int ans = root, maxx = ; while(!q.empty()){
int u = q.front(); q.pop();
for(int i = ; i < G[u].size(); ++i){
P p = G[u][i];
int v = p.first;
int w = p.second;
if(vis[v]) continue;
vis[v] = viss[v] = true;
dp[v] = dp[u] + w;
if(maxx < dp[v]){
maxx = dp[v];
ans = v;
}
q.push(v);
}
}
return ans;
} int solve(int root){
int u = bfs(root);
int v = bfs(u);
return dp[v];
} int main(){
while(scanf("%d %d", &n, &m) == ){
int u, v, c;
for(int i = ; i <= n; ++i) G[i].clear(), p[i] = i;
bool ok = false;
for(int i = ; i < m; ++i){
scanf("%d %d %d", &u, &v, &c);
int x = Find(u);
int y = Find(v);
if(x != y) p[y] = x;
else ok = true;
G[u].push_back(P(v, c));
G[v].push_back(P(u, c));
}
if(ok){ puts("YES"); continue; } memset(viss, false, sizeof viss);
int ans = ;
for(int i = ; i <= n; ++i)
if(!viss[i]) ans = Max(ans , solve(i));
printf("%d\n", ans);
}
return ;
}
如果没想到上面的方法,用dp也是可以做的。
树的直径是其子树直径的最大值,也是叶子节点中的距离的最大值。
dp[i]表示子树i 的高度
枚举所有的节点u,找出以u为中间节点的距离的最大值。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int v,w;
node(int _v, int _w) : v(_v), w(_w) {}
};
vector<node> e[];
int n,m,fa[],dp[],ans;
int find(int x)
{
if(x!=fa[x])
fa[x]=find(fa[x]);
return fa[x];
}
void dfs(int u,int father)
{
int maxx=;
for(int i=;i<e[u].size();i++)
{
int v=e[u][i].v;
int w=e[u][i].w;
if(v==father)
continue;
dfs(v,u);
ans=max(ans,dp[v]+maxx+w);
maxx=max(maxx,dp[v]+w);
}
dp[u]=maxx;
}
void slove()
{
for(int i=;i<=n;i++)
{
if(dp[i]==-)
dfs(i,-);
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
bool flag=false;
ans=;
for(int i=;i<=n;i++)
fa[i]=i,dp[i]=-,e[i].clear();
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
e[x].push_back(node(y,z));
e[y].push_back(node(x,z));
if(flag)
continue;
int fx,fy;
fx=find(x),fy=find(y);
if(fx!=fy)
{
fa[fx]=fy;
}
else
{
flag=true;
continue;
}
}
if(flag)
{
printf("YES\n");
continue;
}
slove();
}
return ;
}
hdu 4514 湫湫系列故事――设计风景线(求树的直径)的更多相关文章
- HDU 4514 湫湫系列故事——设计风景线 树的直径
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4514 湫湫系列故事--设计风景线 Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 4514 湫湫系列故事——设计风景线(并查集+树形DP)
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- Hdu 4514 湫湫系列故事——设计风景线
湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- hdu-----(4514)湫湫系列故事——设计风景线(树形DP+并查集)
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU - 4514 湫湫系列故事——设计风景线(并查集判环)
题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n ...
- 刷题总结——湫湫系列故事——设计风景线(hdu4514 并差集判环+树的直径)
题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探 ...
- HDU 4514 湫湫系列故事――设计风景线 (树形DP)
题意:略. 析:首先先判环,如果有环直接输出,用并查集就好,如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存, 还有用C++交用的少一些,我用G++交的卡在32764 ...
- 湫湫系列故事——设计风景线 HDU - 4514
题目链接:https://vjudge.net/problem/HDU-4514 题意:判断没有没有环,如果没有环,通俗的讲就是找出一条最长的路,相当于一笔画能画多长. 思路:dfs判环. 最后就是没 ...
随机推荐
- 《Lucene in Action第二版》学习总结---如何在Windows下编译luceneinAction2Edition源码包
1. 下载此源码包,位置是: www.manning.com/hatcher3,得到:lia2e.tar.gz,然后解压缩得到目录:lia2e,为了以后能辨识,我将此目录改名为:luceneinAct ...
- 什么是 Service Mesh
作者|敖小剑 微服务方兴未艾如火如荼之际,在 spring cloud 等经典框架之外,Service Mesh 技术正在悄然兴起.到底什么是 Service Mesh,它的出现能带来什么,又能改变什 ...
- python 学习2 测试报告
1. py.test test_class.py --resultlog=./log.txt 2.生成html格式 py.test test_class.py --html=./report.htm ...
- eclipse导入web工程变成Java工程,解决方案
经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目. 解决步骤: 1.进入项目目录,可看到.project文件,文本编辑器打开. 2.找到<nature ...
- 自学宝典:10个学习Android开发的网站推荐
1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...
- 九度OJ 1341:艾薇儿的演唱会 (最短路)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:528 解决:241 题目描述: 艾薇儿今天来到了中国,她计划两天后在哈尔滨举行一场个人的演唱会.由于出现了紧急情况,演唱会的举办方要求艾薇儿 ...
- 九度OJ 1202:排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...
- 优化tomcat启动速度
1.去掉不需要的jar包,这样tomcat在启动时就可以少加载jar包里面的class文件. 2.跳过一些与TLD files.注解.网络碎片无关的jar包,通过在conf/catalina.prop ...
- WIn10远程:mstsc:出现身份验证错误,要求的函数不支持, 这可能是由于CredSSP加密Oracle修正
a.单击 开始 > 运行,输入 regedit,单击 确定. b.定位到 HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Syst ...
- 【python】-- SQLAlchemy操作MySQL
ORM.SQLAchemy orm英文全称object relational mapping,就是对象映射关系程序,简单来说就是类似python这种面向对象的程序来说一切皆对象,但是使用的数据库却都是 ...