N vertices and n-1 edges are given. If the distance from node 0 to the farthest leaf is L <= dis <= r, Bob always selects the largest edge, alice always selects the smallest one and asks if the requirements can be met.
Method: tree-like DP. The dis [u] array indicates the distance from 0 nodes to the current node u. The DP [u] array indicates the distance from the farthest slave node to the U node to meet the requirements, finally, DP [0] is the answer. This advantage of DP is the use of DFS backtracking, from the farthest sub-node to the zero node for DP
Paste a TLE code and a will be available!
#include <list>#include <map>#include <set>#include <queue>#include <string>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>#include <string.h>using namespace std;#define LL long long#define PI acos(-1.0)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-8#define FRE freopen("a.txt","r",stdin)#define MOD 1000000007#define N 500010int max(int a,int b){return a>b?a:b;}int min(int a,int b){return a>b?b:a;}struct edge{ int to; int val;};vector<edge> v[N];int dis[N],dp[N];int l,r;void dfs(int u,int who){ int i,j; if(v[u].size()==0){dp[u]=0;return ;} dp[u]= who?0:MAX; if(dis[u]>r)return ; for(i=0;i<v[u].size();i++){ int to=v[u][i].to; int w=v[u][i].val; dis[to]=dis[u]+w; dfs(to,!who); if(dis[u]+w+dp[to]<=r && dis[u]+w+dp[to]>=l){ if(who) dp[u]=max(dp[u],dp[to]+w); else dp[u]=min(dp[u],dp[to]+w); } }}int main(){ int n; while(~scanf("%d%d%d",&n,&l,&r)){ int i,j,k; for(i=0;i<=n;i++)v[i].clear(); for(i=1;i<n;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); edge tmp; tmp.to=b; tmp.val=c; v[a].push_back(tmp); } dis[0]=0; dfs(0,1); if(dp[0]<=r && dp[0]>=l)printf("%d\n",dp[0]); else printf("Oh, my god!\n"); } return 0;}