1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define N 500010 #define INF 0x3f3f3f3f #define cl(xx,yy) memset((xx),(yy),sizeof((xx)))
int n,L,R,tot,head[N],dp[N],len[N]; struct edge{ int u,v,w,next; edge(){} edge(int __u,int __v,int __w){ u = __u; v = __v; w = __w; } }e[N];
bool check(int x){ if(x >= L && x <= R) return true; return false; }
inline void add(int u,int v,int w){ e[tot] = edge(u,v,w); e[tot].next = head[u]; head[u] = tot++; }
void dfs(int u,int d,bool people){ len[u] = d; if(len[u] > R){ dp[u] = 0; return ; } bool leaf = true; dp[u] = people ? 0 : INF; for(int k = head[u]; k != -1; k = e[k].next){ int v = e[k].v; int w = e[k].w; leaf = false; dfs(v,d+w,(!people)); if(check(len[u] + w + dp[v])){ if(people) dp[u] = max(dp[u] , w + dp[v]); else dp[u] = min(dp[u] , w + dp[v]); } } if(leaf) dp[u] = 0; }
int main(){ while(scanf("%d%d%d",&n,&L,&R)!=EOF){ tot = 0; cl(head,-1); for(int i = 1; i < n; i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); } dfs(0,0,true); if(!check(dp[0])) puts("Oh, my god!"); else printf("%d\n",dp[0]); } return 0; }
|