HDU 3660 Alice and Bob's Trip
树形dp,这道题如果选G++的话,只输入都会超时。我是C++ 1900ms + 飘过的。。。但是输入优化后就快了很多了,1100ms左右。dfs按层次求最值就行了,差不多也算是博弈吧,到bob取的时候要选尽量大的分支(满足条件L和R之间的情况下),反之要alice选尽量小的分支。然后一遍dfs就可以了,时间复杂度为O(n)。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define MP make_pair
#define eps 1e-8
using namespace std; const int N = 500005;
const int INF = 1e9 + 7; bool read(int &x)
{
char c;
while(c=getchar())
{
if(c == EOF) return 0;
if(c<='9'&&c>='0')break;
}
x=c-'0';
while(c=getchar())
{
if(c == EOF) return 0;
else if(c>'9'||c<'0')break;
else x=x*10+c-'0';
}
return 1;
} struct Edge
{
int u, v, c;
} E[N << 1]; int fir[N], next[N << 1], tot, l, r; void Add_Edge(int u, int v, int c)
{
E[tot].u = u, E[tot].v = v, E[tot].c = c;
next[tot] = fir[u], fir[u] = tot ++;
} int dfs(int u, int fa, int c, bool lvl)
{
int v, ret = INF, tmp;
if(lvl) ret = 0;
for(int i = fir[u]; ~i; i= next[i])
{
v = E[i].v;
if(v != fa)
{
tmp = dfs(v, u, c + E[i].c, !lvl) + E[i].c;
if((tmp + c >= l) && (tmp + c <= r))
{
if(lvl) ret = max(ret, tmp);
else ret = min(ret, tmp);
}
}
}
if(ret == INF) ret = 0;
return ret;
} int main()
{
int n, u, v, c, ans;
while(read(n))
{
read(l);read(r);
CLR(fir, -1);
tot = 0;
for(int i = 1; i < n; i ++)
{
read(u);read(v);read(c);
Add_Edge(u, v, c);
Add_Edge(v, u, c);
}
ans = -1;
for(int i = fir[0]; ~i; i = next[i])
{
v = E[i].v;
int tmp = dfs(v, 0, E[i].c, 0) + E[i].c;
if(tmp >= l && tmp <= r)
{
ans = max(ans, tmp);
}
}
if(ans == -1) puts("Oh, my god!");
else printf("%d\n", ans);
}
}
HDU 3660 Alice and Bob's Trip的更多相关文章
- 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 ...
- 【HDOJ】3660 Alice and Bob's Trip
就是一个基本的dfs.可关键问题是c/c++/g++光输入就超时了.还是写java过的,毕竟时限4s.都放弃希望了,没想到还真过了. import java.lang.*; import java.i ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu 4268 Alice and Bob
Alice and Bob Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- hdu 4268 Alice and Bob(multiset|段树)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...
- HDU 5054 Alice and Bob(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5054 Problem Description Bob and Alice got separated ...
- hdu 4111 Alice and Bob(中档博弈题)
copy VS study 1.每堆部是1的时候,是3的倍数时输否则赢: 2.只有一堆2其他全是1的时候,1的堆数是3的倍数时输否则赢: 3.其他情况下,计算出总和+堆数-1,若为偶数,且1的堆数是偶 ...
随机推荐
- html02表格的使用
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C#中的IO流操作(FileStream)
StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作 不用担心编码问题 using (Stream s = new FileStream(@&quo ...
- css渐变色
<!DOCTYPE html><html><head> <meta http-equiv="content-type" content=& ...
- 7-http1.1和2.0的区别?
1.多路复用:减少tcp请求 合并成一个2.首部压缩:会把多个首部压缩3.服务器推送:不用request也可以response
- 生成PDF并下载。
例子是生成一个pdf格式的证书: //创建Document Document document = null; //为该Document创建一个Writer实例 PdfWriter writer = ...
- (转)PHP中的ob_start用法详解
用PHP的ob_start();控制您的浏览器cache Output Control 函数可以让你自由控制脚本中数据的输出.它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况.输出 ...
- Jquery 操作 select
1.判断select选项中 是否存在Value="paraValue"的Item $("#selectid option[@value='paraValue']" ...
- Linux下登陆mysql服务器不需要输入账号密码信息
linux下登录mysql服务器一般都是在命令行手动输入链接信息 [root@localhost ~]# mysql -hlocalhost -uroot -p11111 而在mysql 5.6之后版 ...
- UITouch触摸事件
UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...
- Tweet button with a callback – How to?
原文: http://jaspreetchahal.org/tweet-button-with-a-callback-how-to/ 两种方式:1. 原生的button <a href=&quo ...