#include <stdio.h> #include <string.h> #include <vector> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define INF 0x3f3f3f3f const int N = 500005; int n, l, r, dp[N], E, first[N], next[N]; struct Edge { int u, v, w; } edge[N]; inline void scanf_(int &num)//无负数 { char in; while((in=getchar()) > ‘9‘ || in<‘0‘) ; num=in-‘0‘; while(in=getchar(),in>=‘0‘&&in<=‘9‘) num*=10,num+=in-‘0‘; } void dfs(int u, int fa, int sum, int who) { if (who && first[u] != -1) dp[u] = INF; else dp[u] = 0; for (int i = first[u]; i != -1; i = next[i]) { int v = edge[i].v, w = edge[i].w; if (v == fa) continue; dfs(v, u, sum + w, 1 - who); if (who == 0 && dp[v] + w + sum >= l && dp[v] + w + sum <= r) dp[u] = max(dp[u], dp[v] + w); if (who == 1 && dp[v] + w + sum >= l && dp[v] + w + sum <= r) dp[u] = min(dp[u], dp[v] + w); } } void add(int u, int v, int w) { edge[E].u = u; edge[E].v = v; edge[E].w = w; next[E] = first[u]; first[u] = E++; } int main() { while (~scanf("%d%d%d", &n, &l, &r)) { E = 0; memset(first, -1, sizeof(first)); int u, v, w; for (int i = 0; i < n - 1; i++) { scanf_(u); scanf_(v); scanf_(w); add(u, v, w); } dfs(0, -1, 0, 0); if (dp[0] < l || dp[0] > r) printf("Oh, my god!\n"); else printf("%d\n", dp[0]); } return 0; }
UVA 1484 - Alice and Bob's Trip(树形DP),码迷,mamicode.com
UVA 1484 - Alice and Bob's Trip(树形DP)
原文地址:http://blog.csdn.net/accelerator_/article/details/24797731