题意:给定一个无向图,首先判定是否成环,然后求一条最长链。

分析:成环用并查集,最长链就是个最简单的树形dp了。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; const int N = ;
int n, m, ans;
int set[N];
int dp[N];
struct Edge {
int v, f;
Edge() {}
Edge(int _v, int _f) : v(_v), f(_f) {}
};
vector<Edge>vt[N]; int find(int x) {
return x == set[x] ? x : x = find(set[x]);
} void dfs(int p, int u) {
int forkmax = ;
for (int i = ; i < (int)vt[u].size(); ++i) {
int v = vt[u][i].v, f = vt[u][i].f;
if (v == p) continue;
dfs(u, v);
ans = max(ans, dp[v]+f+forkmax);
forkmax = max(forkmax, dp[v] + f);
}
dp[u] = forkmax;
} /*
6 4
1 2 2
1 3 4
4 5 4
4 6 3
*/ void solve() {
for (int i = ; i <= n; ++i) {
if (dp[i] == -) dfs(, i);
}
printf("%d\n", ans);
} int main() {
while (scanf("%d %d", &n, &m) != EOF) {
int a, b, c, x, y;
bool loop = false;
ans = ;
for (int i = ; i <= n; ++i) {
dp[i] = -;
set[i] = i;
vt[i].clear();
}
for (int i = ; i < m; ++i) {
scanf("%d %d %d", &a, &b, &c);
if (loop) continue;
x = find(a), y = find(b);
if (x != y) set[x] = y;
else loop = true;
vt[a].push_back(Edge(b, c));
vt[b].push_back(Edge(a, c));
}
if (loop) {
puts("YES");
continue;
}
solve();
}
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. HDU - 4514 湫湫系列故事——设计风景线(并查集判环)

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

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

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

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

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

随机推荐

  1. Calendar的问题

    1. include file is not work now. remove <!-- #include file="Calendar.js" -->, add &l ...

  2. Length of Last Word

    class Solution { public: int lengthOfLastWord(string s) { ; ; while(s[i]&&s[i]==' ') i++; // ...

  3. [转]Dynamics AX and Generic collections of .Net

    转自:http://blogs.msdn.com/b/emeadaxsupport/archive/2009/04/23/dynamics-ax-and-generic-collections-of- ...

  4. 【jqGrid for ASP.NET MVC Documentation】.学习笔记.3.本地化语言包

    1 引用本地化语言包 在 js/i18n 文件夹中,提供了大量预定义的语言包.它包括为所有字符串定义的,包括消息,标题,分页信息,搜索/添加/删除 的对话框 文本等. 在jQuery库文件后,在jqG ...

  5. POJ 3349:Snowflake Snow Snowflakes(数的Hash)

    http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K T ...

  6. .Net额外小工具

    http://blog.csdn.net/zhumi/article/details/49076 用于编写单元测试的 NUnit用于创建代码文档资料的 NDoc用于生成解决方案的 NAnt用于生成代码 ...

  7. python中split函数的使用

    最近学习python,对split函数做了下总结,内容如下:

  8. 在chrome浏览器和在IE浏览器中显示的页面样式不一样的解决办法

    在IE浏览器中添加 一行代码即可:<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 位 ...

  9. c#作业

    string a = this.textBox1.Text; // string [] b=a.Split("\r\n".ToCharArray(),StringSplitOpti ...

  10. Pipe(点积叉积的应用POJ1039)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9723   Accepted: 2964 Description ...