随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好。 
  现在已经勘探确定了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 湫湫系列故事――设计风景线(求树的直径)的更多相关文章

  1. HDU 4514 湫湫系列故事——设计风景线 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4514 湫湫系列故事--设计风景线 Time Limit: 5000/2000 MS (Java/Ot ...

  2. HDU 4514 湫湫系列故事——设计风景线(并查集+树形DP)

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  3. Hdu 4514 湫湫系列故事——设计风景线

    湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...

  4. hdu-----(4514)湫湫系列故事——设计风景线(树形DP+并查集)

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  6. HDU - 4514 湫湫系列故事——设计风景线(并查集判环)

    题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n ...

  7. 刷题总结——湫湫系列故事——设计风景线(hdu4514 并差集判环+树的直径)

    题目:   随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.   现在已经勘探 ...

  8. HDU 4514 湫湫系列故事――设计风景线 (树形DP)

    题意:略. 析:首先先判环,如果有环直接输出,用并查集就好,如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存, 还有用C++交用的少一些,我用G++交的卡在32764 ...

  9. 湫湫系列故事——设计风景线 HDU - 4514

    题目链接:https://vjudge.net/problem/HDU-4514 题意:判断没有没有环,如果没有环,通俗的讲就是找出一条最长的路,相当于一笔画能画多长. 思路:dfs判环. 最后就是没 ...

随机推荐

  1. POJ 1654 area 解题

    Description You are going to compute the area of a special kind of polygon. One vertex of the polygo ...

  2. python的list求和与求积

    python中,无论是对的list求和还是求积,我都给出了两种方法. 1.对list求和 1.1 s=0 for i in range(10): s+=i 1.2 s=sum(range(10)) 2 ...

  3. Java中synchronized用在静态方法和非静态方法上面的区别

    synchronized 修饰在 static方法和非static方法的区别   在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以sync ...

  4. java servlet中上传文件的简单实现(基于第三方jar)

    这里的案例使用了两种文件上传的组件.分别介绍 1.使用JSPSmartUpload完成上传 package test_servlet_package; import java.io.File; imp ...

  5. Arrays类--Arrays.asList()方法使用

    java.util类 Arrays java.lang.Object——java.util.Arrays public class Arrays extends Object 此类包含用来操作数组(比 ...

  6. mongo explain分析详解

    1 为什么要执行explain,什么时候执行 explain的目的是将mongo的黑盒操作白盒化. 比如查询很慢的时候想知道原因. 2 explain的三种模式 2.1 queryPlanner 不会 ...

  7. SQLite支持的并发访问数

    SQLite objects created in a thread can only be used in that same thread.The object was created in th ...

  8. ShowModal 代码分析

    下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. 如何将cordova导入Android studio,只需两步即可

    Cordova的技术交流新群 微信公众号:

  10. Android中子线程真的不能更新UI吗?

    Android的UI访问是没有加锁的,这样在多个线程访问UI是不安全的.所以Android中规定只能在UI线程中访问UI. 但是有没有极端的情况?使得我们在子线程中访问UI也可以使程序跑起来呢?接下来 ...