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的堆数是偶 ...
随机推荐
- GridView点击行,选中模版列中CheckBox
<asp:TemplateField ItemStyle-Width="40px" HeaderText="选择" ItemStyle-Horizonta ...
- exist的用法
http://blog.csdn.net/maladoufu/article/details/8194624 http://blog.csdn.net/xiwu1616/article/details ...
- Python算术运算符
Python 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4和5被称为操作数,"+"号为运算符. Python语言支持 ...
- centos yum
1.介绍 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器 ...
- [Ext JS 4] 实战之Grid, Tree Gird 添加按钮列
引言 贴一个grid 的例子先: 有这样一个需求: 1. 给 Grid(or Tree Grid)添加一列, 这一列显示是Button. 点击之后可以对这一行进行一些操作 2. 这一列每一行对应的按钮 ...
- openstack nova数据库计算结点IP地址
最近遇到一个问题就是在控制结点上查找nova数据库中 select * from compute_nodes\G;中出现IP地址一直是127.0.0.1不是计算结点的IP,就算修改成计算结点的IP,也 ...
- 初涉JavaScript模式 (10) : 函数 【进阶用法】
写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...
- iOS开发——OC篇&OC高级语法
iOS开发高级语法之分类,拓展,协议,代码块详解 一:分类 什么是分类Category? 分类就是类的补充和扩展部分 补充和扩展的每个部分就是分类 分类本质上是类的一部分 分类的定义 分类也是以代码的 ...
- 查询一周最近一周的数据,date String 随意转换,更有疯狂的排序
--查看一周随访的详情70007PROCEDURE GET_PATIENT_WEEKFU ( userId in NUMERIC, time in date, V_LIST OUT MYCURSOR ...
- MVC4 教程
http://blog.csdn.net/huangjihua0402/article/details/8507055 http://www.cnblogs.com/lc-chenlong/p/324 ...