就是一个基本的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());
}
}

【HDOJ】3660 Alice and Bob's Trip的更多相关文章

  1. hdu 3660 Alice and Bob's Trip(树形DP)

    Alice and Bob's Trip Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 【HDOJ】4122 Alice's mooncake shop

    RMQ的基础题目,简单题. /* 4122 */ #include <iostream> #include <sstream> #include <string> ...

  3. 【HDOJ】3220 Alice’s Cube

    状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...

  4. HDU 3660 Alice and Bob's Trip

    树形dp,这道题如果选G++的话,只输入都会超时.我是C++ 1900ms + 飘过的...但是输入优化后就快了很多了,1100ms左右.dfs按层次求最值就行了,差不多也算是博弈吧,到bob取的时候 ...

  5. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  6. 【博弈+GCD】C. Alice and Bob

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/C [题意] 初始时有n个数,定义操作为从n个数中取出两个数x,y,如果|x-y| ...

  7. UVA1484 Alice and Bob's Trip (hdu3660)

    一.前言 最开始卡这题是某大佬给出的树DP专题中的一个,据说类似于对抗搜索(这是啥?)的一题 但是在经历了若干艰难困苦之后发现这题在HDU上A不了——(先卡vector的时间,后卡输入的时间,上了输入 ...

  8. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...

  9. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

随机推荐

  1. javascript 第26节 jQuery对象

    <html> <head> <title>jQuery</title> <!--导入jquery库--> <script type=& ...

  2. 一篇关于学C++的感想(拿来与大家分享)

    学计算机的如果你有耐心看下去,我敢保证这绝对是一种收获 期待爱 大师提醒: 计算机专业不是学编程,而是懂得计算机的工作原理,以及和计算机相关的学科技术.一个高手不必懂得编程,coder是最底层的人物, ...

  3. enum 与 #define

    enum 与 #define 一.为什么既要有enum,又要define enum is derived from enumerate, from ex- + number,字面意思就是用数字排列,报 ...

  4. From MSI to WiX, Part 1 - Required properties, by Alex Shevchuk

    Following content is directly reprinted from From MSI to WiX, Part 1 - Required properties Author: A ...

  5. libpcap 主要函数及过程详解

    http://blog.chinaunix.net/uid-21556133-id-120228.html libpcap(Packet Capture Library),即数据包捕获函数库,是Uni ...

  6. Intellij IDEA 14的注册码

    IntelliJ IDEA 14 注册码 IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:6115 ...

  7. Super Hide IP 3.4.7.8允许您以匿名方式进行网上冲浪、 保持隐藏您的 IP 地址

    Super Hide IP 3.4.7.8 转自:http://www.0daydown.com/04/695.html Super Hide IP允许您以匿名方式进行网上冲浪. 保持隐藏您的 IP ...

  8. python zookeeper 学习笔记

    1.安装zookeeper 下载zookeeper后,解压,安装 cd zookeeper-/src/c ./configure make make install 2.启动zookeeper服务 c ...

  9. 仿今日头条最强顶部导航指示器,支持6种模式-b

    项目中经常会用到类似今日头条中顶部的导航指示器,我也经常用一个类似的库PagerSlidingTabStrip,但是有时并不能小伙伴们的所有需求,所以我在这个类的基础上就所有能用到的情况做了一个简单的 ...

  10. Asp.Net MVC ajax调用 .net 类库问题

    如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...