A tree with n knots starts from 0 (root node), Alice and Bob go from 0 to the leaf node, Alice goes through the shortest path, and Bob goes through the longest path, bob first selects the next node, then the two come together to that node, then Alice selects the next node ......, The total length must be within [L, R] and the longest route length (1 <= n <= 500000, 0 <= L, R <= 1000000000, 1 <= distance between adjacent nodes <= 1000 ).
--> Angry brush tree dp...
Set da [I] to the shortest distance from node I to the leaf, then
The state transition equation is da [x] = min (da [x], db [v [e] + w [e]).
Set db [I] As Bob and start from node I to reach the longest distance of the leaf, then
The state transition equation is db [x] = max (db [x], da [v [e] + w [e]).
I/O optimization is added and submitted in C ++ ~
#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>using namespace std;const int maxn = 500000 + 10;const int INF = 0x3f3f3f3f;int n, L, R, head[maxn], nxt[maxn], u[maxn], v[maxn], w[maxn], ecnt, da[maxn], db[maxn];void init(){ ecnt = 0; memset(head, -1, sizeof(head));}void addEdge(int uu, int vv, int ww){ u[ecnt] = uu; v[ecnt] = vv; w[ecnt] = ww; nxt[ecnt] = head[uu]; head[uu] = ecnt; ecnt++;}int nextInt(){ char c = getchar(); while(!isdigit(c)) c = getchar(); int ret = 0; while(isdigit(c)){ ret = ret * 10 + c - '0'; c = getchar(); } return ret;}void read(){ int uu, vv, ww, i; for(i = 0; i < n-1; i++){ uu = nextInt(); vv = nextInt(); ww = nextInt(); addEdge(uu, vv, ww); }}void dp(int x, int cur){ da[x] = head[x] == -1 ? 0 : INF; db[x] = 0; for(int e = head[x]; e != -1; e = nxt[e]){ int len = cur + w[e]; if(len <= R) dp(v[e], len); if(len + db[v[e]] >= L && len + db[v[e]] <= R) da[x] = min(da[x], db[v[e]] + w[e]); if(len + da[v[e]] >= L && len + da[v[e]] <= R) db[x] = max(db[x], da[v[e]] + w[e]); }}void solve(){ dp(0, 0); if(db[0]) printf("%d\n", db[0]); else puts("Oh, my god!");}int main(){ while(scanf("%d%d%d", &n, &L, &R) == 3){ init(); read(); solve(); } return 0;}