标签:des style blog io os ar strong for 数据
Description
Input
Output
Sample Input
3 3 1 2 1 2 3 1 3 1 1
Sample Output
YES 思路:RE到吐血的地步,HDU竟然有个栈的初始化:#pragma comment(linker, "/STACK:1024000000,1024000000"),一口老血喷出来,首先判断环形:并查集,然后就是树形DP#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int maxn = 100005; struct Edge { int v, f; Edge() {} Edge(int v, int f) { this->v = v; this->f = f; } }; vector<Edge> ve[maxn]; int n, m, ans; int fa[maxn], dp[maxn]; int find(int x) { if (x != fa[x]) fa[x] = find(fa[x]); return fa[x]; } void dfs(int p, int u) { int Max = 0; for (int i = 0; i < ve[u].size(); i++) { int v = ve[u][i].v, f = ve[u][i].f; if (v == p) continue; dfs(u, v); ans = max(ans, dp[v] + f + Max); //两条分支和的最大值 Max = max(Max, dp[v] + f); } dp[u] = Max; } void solve() { for (int i = 1; i <= n; i++) if (dp[i] == -1) dfs(0, i); printf("%d\n", ans); } int main() { int a, b, c, x, y; while (scanf("%d%d", &n, &m) != EOF) { ans = 0; int flag = 0; for (int i = 1; i <= n; i++) { dp[i] = -1; fa[i] = i; ve[i].clear(); } for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (flag) continue; x = find(a), y = find(b); if (x != y) fa[x] = y; else flag = 1; ve[a].push_back(Edge(b, c)); ve[b].push_back(Edge(a, c)); } if (flag) { printf("YES\n"); continue; } solve(); } return 0; }
标签:des style blog io os ar strong for 数据
原文地址:http://blog.csdn.net/u011345136/article/details/39503697