就是一个基本的dfs。可关键问题是c/c++/g++光输入就超时了。还是写java过的,毕竟时限4s。都放弃希望了,没想到还真过了。
import java.lang.*;
import java.io.*;
import java.util.*; public class Main { public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in, out);
out.close();
}
} class TaskA {
public final static int maxv = (int)(5e5+5);
public final static int INF = 0x3f3f3f3f;
int[] V = new int[maxv];
int[] W = new int[maxv];
int[] nxt = new int[maxv];
int[] head = new int[maxv];
int m, n, L, R; void init() {
m = 0;
Arrays.fill(head, -1);
} void addEdge(int u, int v, int w) {
V[m] = v;
W[m] = w;
nxt[m] = head[u];
head[u] = m++;
} public void solve(InputReader in, PrintWriter out) {
int i;
int u, v, w;
int ans; while (true) {
try {
n = in.nextInt();
} catch (RuntimeException e) {
break;
}
L = in.nextInt();
R = in.nextInt();
init();
for (i=1; i<n; ++i) {
u = in.nextInt();
v = in.nextInt();
w = in.nextInt();
addEdge(u, v, w);
}
ans = dfs(0, 0, 0);
if (ans == INF)
out.println("Oh, my god!");
else
out.println(ans);
}
} private int dfs(int u, int len, int now) {
int ans = -1; if (head[u] == -1)
return 0;
int i, v, w; for (i=head[u]; i!=-1; i=nxt[i]) {
v = V[i];
w = W[i];
int tmp = dfs(v, len+w, now^1) + w;
if (tmp == INF)
continue;
if (tmp>=L-len && tmp<=R-len) {
if (now == 0) {
if (ans==-1 || tmp>ans)
ans = tmp;
} else {
if (ans==-1 || tmp<ans)
ans = tmp;
}
}
} if (ans == -1)
return INF;
return ans;
}
} class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}