HDU - 4514 湫湫系列故事——设计风景线(并查集判环)
题目:
随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好。
现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度。请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?
其中,可以兴建的路线均是双向的,他们之间的长度均大于0。
思路:
将给出的边的两个端点用并查集放在一起,如果这两个点的祖先相等说明构成了一个环。
在这个用并查集连成的连通分量里边,找到他的root(fa[i] = i),然后先用一个BFS找到他的端点,然后从这个端点开始找树上的最长距离。
代码:
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef unsigned long long ll;
const int maxn = ;
int n,m;
struct Edge
{
int to,w;
};
vector<Edge> mp[maxn];
int fa[maxn],vis[maxn],d[maxn]; int _find(int x)
{
return fa[x] == x ? x : fa[x] = _find(fa[x]);
} bool judgeLoop(int u,int v)
{
int x = _find(u);
int y = _find(v);
if(x!=y)
{
fa[x] = y;
return false;
}
return true;//x==y说明两者已经在一个连通分量里边了,也就是有环了
} int BFS(int s)//两个作用1.查找最远的结点2.计算最长的距离
{
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
vis[s] = ;
queue<int> que;
que.push(s);
int ed = s,mmax=;
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i=; i<mp[u].size(); i++)
{
Edge e = mp[u][i];
if(vis[e.to] == )
{
d[e.to] = d[u]+e.w;//获取从根节点到e.to结点的距离
vis[e.to] = ;
que.push(e.to);
if(d[e.to]>mmax)//找到这棵树的距离根节点最远的结点
{
ed = e.to;
mmax = d[e.to];
}
}
}
}
return ed;
} int main()
{
//FRE();
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<maxn; i++) { fa[i] = i; mp[i].clear();}
bool ok = false;
for(int i=; i<m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[u].push_back(Edge{v,w});
mp[v].push_back(Edge{u,w});
if(judgeLoop(u,v)) { ok = true; }
}
if(ok)
{
printf("YES\n");
continue;
}
int ans = ;
for(int i=; i<=n; i++)
{
if(fa[i]==i)//找到这个树的根节点(人为设置的)
{
int root = BFS(i);
int temp = BFS(root);
ans = max(ans,d[temp]);
}
}
printf("%d\n",ans);
}
return ;
}
HDU - 4514 湫湫系列故事——设计风景线(并查集判环)的更多相关文章
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 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 湫湫系列故事――设计风景线(求树的直径)
随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置 ...
- 刷题总结——湫湫系列故事——设计风景线(hdu4514 并差集判环+树的直径)
题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探 ...
- HDU 4514 湫湫系列故事――设计风景线 (树形DP)
题意:略. 析:首先先判环,如果有环直接输出,用并查集就好,如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存, 还有用C++交用的少一些,我用G++交的卡在32764 ...
- 湫湫系列故事——设计风景线 HDU - 4514
题目链接:https://vjudge.net/problem/HDU-4514 题意:判断没有没有环,如果没有环,通俗的讲就是找出一条最长的路,相当于一笔画能画多长. 思路:dfs判环. 最后就是没 ...
随机推荐
- 【179】IDL 读写 NetCDF 文件
NetCDF(network Common Data Form)由位于科罗拉多州波尔市的 Unidata 程序中心开发,主要应用于大气科学的研究.NetCDF 的数据模式具有简单性和灵活性的特点.Ne ...
- 476. Number Complement(补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- TensorFlow博客翻译——用TensorFlow在云端进行机器学习
https://github.com/tensorflow/tensorflow 原文地址 Machine Learning in the Cloud, with TensorFlow Wednesd ...
- Ubuntu 环境变量及 ADB 配置 (转载)
转自:http://blog.csdn.net/ithomer/article/details/7307449 同Windows一样,Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环 ...
- Hibernate对集合属性的操作---基础学习
1:Set集合属性操作 1).Hibernate3以后支持大部分重要的JDK集合接口映射,Set集合接口的配置: >在xxx.hbm.xml文件中使用<set>标签 2).< ...
- A+B Problem——经典中的经典
A+B Problem,这道题,吸收了天地的精华,是当之无愧的经典中的经典中的经典.自古以来OIer都会经过它的历练(这不是白说吗?),下面就有我herobrine来讲讲这道题的各种做法. 好吧,同志 ...
- SwipeLayou与ScrollerView滑动冲突
在SwipeLayout内嵌套ScorllerView滑动会出现上滑滑动冲突,ScollerView不能往上滑,,,,,, mSlv.getViewTreeObserver().addOnScroll ...
- python中字典的陷阱
把字典与列表组合,如 i=20 s=[]#定义一个空列表 b={'d':i}#定义一个字典 while i>0: i=i-1 b['d']=i#更新字典的值 s.append(b) print( ...
- Multitenant best Practice clone pdb seed and Clone a Pluggable Database – 12c Edition
1. 1.Tnsnames when connecting to either Container or Pluggable instance The tnsnames.ora should be c ...
- ABP教程(一)- ABP介绍
ABP是什么 ABP是”ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用 ...