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的堆数是偶 ...
随机推荐
- Bernese单点定位数据准备及处理
原创作者 blog :http://yifeiyao.blog.163.com/blog/static/2058932752012669731170/1.准备所需用的数据文件,如下: 原始观测.O文件 ...
- Bernese安装及使用
一.安装: 伯尔尼软件的安装很简单,但是在64位下,可能perl解释器安装不成功,我找了一个,并且可用,下载地址: 链接:http://pan.baidu.com/s/1hr8fgEC 密码:fj8b ...
- Android Camera开发:使用GLSurfaceView预览Camera 基础拍照
GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而且在预览Camera上有其独到之处.独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就只有使用GLSur ...
- 二分图最大匹配(匈牙利算法Dfs模板)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2020 using name ...
- HighCharts基本用法
var options={ chart: {type: 'column',renderTo: 'ChartDesigner1'},//type :图表类型(柱状图,饼状图),renderTo :指向页 ...
- 很棒的jQuery代码片段分享
jQuery实现的内链接平滑滚动 不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动 $('a[href^="#"]').bind('click.smoot ...
- Oracle解析 xml 记录一下(未完待续)
Oracle解析 xml 记录一下. SQL> desc xmlparser; PROCEDURE FREEPARSER Argument Name Type ...
- 设置Imindmap默认字体
创建一个新的字体样式 根据如下步骤创建新的字体样式: 1.打开一个mindmap,选中工具栏上的 [样式][Styles ]. 2.选择 Font > Create New Font Optio ...
- socket (转,吴秦,http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html)
Linux Socket编程(不限Linux)2010-12-12 21:58 by 吴秦 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web ...
- underscorejs-pluck学习
2.14 pluck 2.14.1 语法: _.pluck(list, key) 2.14.2 说明: pluck方法根据key对list数组中的每个对象进行检索,返回检索成功的属性值,否则返回und ...