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 ...
随机推荐
- 利用Node的chokidar 监听文件改变的文件。
最近维护一个项目.每次改完东西,都要上传到服务器.然后有时候就忘记一些东西,于是就想有没有可以方法能监听文件的改变.然后我再利用程序把更改的文件一键上传到服务器. 于是就找到了nodejs 的chok ...
- New UWP Community Toolkit - RotatorTile
概述 UWP Community Toolkit 中有一个为图片或磁贴提供轮播效果的控件 - RotatorTile,本篇我们结合代码详细讲解 RotatorTile 的实现. RotatorTi ...
- Java KeyTool command
Create a new key: keytool -genkey -alias keyAlias -keyalg RSA -validity 1000 -keystore d:\keyPath\k ...
- kubernetes入门(01)kubernetes是什么?
一.kubernetes是什么? Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理.在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以 ...
- hadoop2.6.0实践:引入开发依赖的jar包
hadoop-2.5.0\share\hadoop\common 所有jar,hadoop-2.5.0\share\hadoop\common\lib 所有jar,hadoop-2.5.0\sha ...
- 阿里云API网关(6)用户指南(开放 API )
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- 版本名称GA的含义:SNAPSHOT->alpha->beta->release->GA
SNAPSHOT->alpha->beta->release->GA ----------------------------------------------------- ...
- OAuth2.0学习(1-9)新浪开放平台微博认证-web应用授权(授权码方式)
1. 引导需要授权的用户到如下地址: URL 1 https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&respons ...
- C#配置文件config的使用
做程序的时候总会有一些参数,可能会调整,这时候一般情况下我都会写在配置文件里,这样方便一点. 配置文件的读取 <?xml version="1.0" encoding=&qu ...
- HashMap就是这么简单【源码剖析】
前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合以及散列表.Map集合.红黑树的基础了: Collection总览 List集合就这么简单[源码剖析] Ma ...