UVA 12161 Ironman Race in Treeland
题目大意:
每一条边都有两个权值,val和路径长度d,要保证在val<=m 的条件下,求最长的d.
解题报告:
一开始想错了,后来发现还不如直接暴力点分,思想很套路..
平时我们统计时,都会用合法减不合法,但这题不是方案统计,需要枚举每一棵子树,这里其实直接暴力枚就行,但是用到了val和d的单调性,首先明确:如果对于两条路径i,j:\(vali>valj,di<dj\) 这样显然不会最优,所以我们就去掉这些路径,剩下的就满足单调性了,d随val增加而增加,这样就可以二分了.
所以我们把子树分为两个部分,一部分是已经处理好的,另一部分是未知的集合e,我们枚举处理好的集合c,然后将e按val从小到大排序后,二分最大满足\(val_c+val_e<=m\) 的e,此时的e一定是d最大的,更新答案后,我们再把e加入到c集合中,此时c集合依旧满足单调性.
复杂度\(O(TNlog2N)\)
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define ls (node<<1)
#define rs (node<<1|1)
using namespace std;
const int N=30005,inf=100000005;
int gi(){
int str=0;char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')
str=(str<<1)+(str<<3)+ch-48,ch=getchar();
return str;
}
int m,head[N],num=0,to[N<<1],nxt[N<<1],dis[N<<1],c[N<<1],n,ans=0;
int f[N]={inf},root=0,son[N],sum,sz=0,CNT=0;bool vis[N];
void link(int x,int y,int z,int k){
nxt[++num]=head[x];to[num]=y;dis[num]=z;c[num]=k;
head[x]=num;
}
il void getroot(int x,int last){
int u;son[x]=1;f[x]=0;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last || vis[u])continue;
getroot(u,x);
son[x]+=son[u];
f[x]=Max(f[x],son[u]);
}
f[x]=Max(f[x],sum-son[x]);
if(f[x]<f[root])root=x;
}
struct node{
int d,c;
bool operator<(const node &pp)const{
return d<pp.d;
}
}v[N];
il void getdis(int x,int last,int dist,int cost){
int u;
v[++sz].d=dist;v[sz].c=cost;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last || vis[u])continue;
getdis(u,x,dist+dis[i],cost+c[i]);
}
}
int q[N][2],tot=0;
void Unique(){
int p=0,mx=0;
sort(v+1,v+sz+1);
for(int i=1;i<=sz;i++){
if(v[i].c<=mx)continue;
else {
v[++p]=v[i];
mx=v[i].c;
}
}
sz=p;
}
int midit(int x){
int l=1,r=sz,mid,ret=-1;
while(l<=r){
mid=(l+r)>>1;
if(x+v[mid].d<=m)ret=mid,l=mid+1;
else r=mid-1;
}
return ret;
}
void cal(){
Unique();
int tmp;
for(int i=1;i<=tot;i++){
tmp=midit(q[i][0]);if(tmp==-1)continue;
if(v[tmp].c+q[i][1]>ans)ans=v[tmp].c+q[i][1];
}
for(int i=1;i<=sz;i++){
q[++tot][0]=v[i].d;q[tot][1]=v[i].c;
}
sz=0;
}
il void solve(int x){
int u;tot=0;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(vis[u])continue;
getdis(u,x,dis[i],c[i]);
cal();
}
}
il void dfs(int x){
int u;vis[x]=true;
solve(x);
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(vis[u])continue;
sum=son[u];root=0;getroot(u,x);
dfs(root);
}
}
void Clear(){
num=0;ans=0;memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
}
void work()
{
Clear();
int x,y,z,r;
n=gi();m=gi();
for(int i=1;i<n;i++){
x=gi();y=gi();z=gi();r=gi();
link(x,y,z,r);link(y,x,z,r);
}
root=0;sum=n;getroot(1,1);
dfs(root);
printf("Case %d: %d\n",CNT,ans);
}
int main()
{
int T=gi();
while(T--)CNT++,work();
return 0;
}
UVA 12161 Ironman Race in Treeland的更多相关文章
- UVA 12161 Ironman Race in Treeland (树分治)
题意:求树上的一条费用不超过m的路径,使得总长度尽量大. 人参第一发树分治,紫书上思路讲得比较清晰,这里不再赘述. 实现的时候,用一个类似时间戟的东西,记录结点首次访问的时间,并保存结点序列. 合并的 ...
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- UVA 11762 - Race to 1(概率)
UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,假设不是质因子,就保持不变,假设是的话.就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数. ...
- UVa 11762 - Race to 1
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 【UVA】【11762】Race to 1(得到1)
数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- UVa 12034 - Race(递推 + 杨辉三角)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Race UVA - 12034(dp+打表)
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded ...
随机推荐
- 本地通知UILocalNotification
1.增加一个本地推送 //设置20秒之后 ]; //chuagjian一个本地推送 UILocalNotification *noti = [[[UILocalNotification alloc] ...
- Flask 蓝图(Blueprint)
蓝图使用起来就像应用当中的子应用一样,可以有自己的模板,静态目录,有自己的视图函数和URL规则,蓝图之间互相不影响.但是它们又属于应用中,可以共享应用的配置.对于大型应用来说,我们可以通过添加蓝图来扩 ...
- raid5 / raid5e / raid5ee的性能对比及其数据恢复原理
RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保障程度要比Mirror低 ...
- Linq GroupJoin
static void Main(string[] args) { List<Person> persons = new List<Person> { }, }, }; Lis ...
- VMware网络配置
NAT模式 首先保证虚拟机网卡和主机对接,虚拟机网络连接要和主机在同一网段 1. 控制面板\网络和 Internet\网络连接中配置VMnet8 2. 编辑虚拟机网络配置 此处子网ip需要和Vnet8 ...
- axure 预览"HTTP/1.1 302 Found"
使用Axure编辑原型时,点击预览出现"HTTP/1.1 302 Found" 第一想到的就是重新安装Axure和检查原型文件是否损坏,验证后证明前Axure和.rp文件都是完好的 ...
- eclipse版本对应的jdk版本
Installing Eclipse is relatively easy, but does involve a few steps and software from at least two d ...
- 前端开发必备之Chrome开发者工具(一)
本文介绍的 Chrome 开发者工具基于 Chrome 65版本,如果你的 Chrome 开发者工具没有下文提到的那些内容,请检查下 Chrome 的版本 简介 Chrome 开发者工具是一套内置于 ...
- mysql解压缩版本的安装、初始化等
https://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 启动或者暂停mysql服务: https://dev.mysq ...
- IT技术有感
今天看技术文章,spring相关的,某一个点以前每次看一直不理解, 可是不知道为什么隔了1年左右,中间什么都没做,现在却都懂了. 在看懂的那一刻,笼罩在我心上的躁动突然平静了许多,我的心这一年来前所未 ...