就是一个基本的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. iOS 各种控件默认高度

    1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图   用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半 ...

  2. OC中 block 的用法

    block 常用于反向传值 声明 返回值类型 (^block)(参数列表) 调用 闭包的名字=^(参数列表){}: 闭包的名字(): 如: void(^aaaaa)(int num,NSString ...

  3. android 权限总结

    1.拨打电话要权限 2.sd目录存东西要权限

  4. Java实战之04JavaWeb-01Servlet

    一.Http协议 1.什么是http协议? http协议就是描述客户端与服务器端交互过程的 2.http的请求 3.http的响应 二.Servlet的简介 1.Servlet的概述 Servlet: ...

  5. fsockopen/curl/file_get_contents效率比较

    前面小节 PHP抓取网络数据的6种常见方法 谈到了 fsockopen,curl与file_get_contents 的使用方法,虽然它们都能达到同一个使用目的,但是它们之间又有什么区别呢? 先谈谈c ...

  6. Apache Shiro入门实例

    Shiro是一个强大灵活的开源安全框架,提供身份验证.授权.会话管理.密码体系. 1.先创建一个Maven项目 2.配置pom <project xmlns="http://maven ...

  7. 第33条:用EnumMap代替序数索引

    有时候,会见到利用ordinal方法来索引数组的代码.例如下面这个简化的类,表示一种烹饪用的香草: public class Herb { public enum Type { ANNUAL, PER ...

  8. 我爱工程化 之 gulp 使用(二)

    上一篇  介绍了gulp的安装.环境等配置.基本使用,那么现在,我们快走进 速8,深入了解吧...... 一.各种安装.环境配置.插件安装(参考上一篇文章) 二.项目基本目录结构 三.编写 gulpf ...

  9. ajax、json一些整理(1)

    1.请求text数据,在success事件中手动解析 前台:                 $.ajax({                     type: "post", ...

  10. MVC 弹出提示框

    第一种弹框成功后要刷新界面 [HttpPost] public ActionResult Add(Maticsoft.Model.Project.ProjectMoneyPlan model) { m ...