就是一个基本的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. 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)

    1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...

  2. ###g++编译器

    点击查看Evernote原文. #@author: gr #@date: 2014-07-20 #@email: forgerui@gmail.com 对g++编译器不是特别熟悉,希望借此熟悉一下. ...

  3. ### 学习《C++ Primer》- 6

    Part 6: 拷贝控制(第13章) // @author: gr // @date: 2015-01-08 // @email: forgerui@gmail.com 一.拷贝.赋值与销毁 拷贝构造 ...

  4. jq选取对象的方法

     $("#找id的")$(".找样式的")  $("div[id]") 选择所有含有id属性的div元素 $("input[nam ...

  5. C 语言 查找一个字符串2在字符串1中出现的次数

    #include <stdio.h> #include <windows.h> int main() { ], b[]; char *temp; ; memset( a, ); ...

  6. 再也不要看到Eclipse万恶的arg0,arg1提示

    不知道大家跟我是否一下,遇到arg的提示. @Override public void onDateChanged(DatePicker arg0, int arg1, int arg2, int a ...

  7. 06_init()和destroy()方法

    [工程截图] [HelloWorld.java] package com.HigginCui; public class HelloWorld { public HelloWorld(){ Syste ...

  8. SVN: revert all command

    If you accidentally marked all your files as "delete" (your file/folder has a red x on it) ...

  9. CentOS7 firewall的使用

    # 查看区域 firewall-cmd --get-zones # 查看默认区域 firewall-cmd --get-default-zone # 给区域添加永久性服务 firewall-cmd - ...

  10. WCF SOA服务应用

    WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service.MSMQ.Remoting等通信技术,通过灵活的配置,让服务编程更加容易.可扩展.这篇文章主要目的就是带领大家从开发 ...