运输计划noip
靠!这道题TM搞了我好几天,真是烦死人!!!早上打了一个倍增的TM只有95分QAQ。。。
然后一气之下开始不断卡常,各种玄学优化,可是就是T。。TAT。。
可恶!晚上我就直接打了个tarjan,还好跑过了,可是打的我身心俱残!!!仿佛想起了打splay的岁月,各个代码3000+。。。
不过一次就打成功了,还算比较好吧。。。
先上95分的代码
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define maxn 600500
#define ll long long
using namespace std;
struct st{
ll u,v,next,val;
}s[maxn]; struct qu{
ll a,b,lca,len;
}quest[maxn]; ll n,m,u,v,root,tot,val,head[maxn],ceng[maxn],fa[maxn][],len[maxn],fedge[maxn],zou[maxn],cf[maxn],maxlen,l,r,ma; inline void add(ll u,ll v,ll val)
{
tot++;
s[tot].val = val;
s[tot].u = u;
s[tot].v = v;
s[tot].next = head[u];
head[u] = tot;
} inline void dfs(ll f,ll now)
{
fa[now][] = f;
ceng[now] = ceng[f] + ;
for(ll i = head[now];i;i = s[i].next)
{
v = s[i].v;
if(!ceng[v])
{
fedge[v] = s[i].val;
len[v] = len[now] + s[i].val;
dfs(now,v);
}
}
} inline void init()
{
for(ll j=;(<<j)<=n;j++)
{
for(ll i=;i<=n;i++)
{
fa[i][j]=fa[fa[i][j-]][j-];
}
}
} inline ll lca(ll a,ll b)
{
if(ceng[a] > ceng[b]) swap(a,b);
ll cha = ceng[b] - ceng[a];
for(ll i = ;( << i) <= cha;i++)
{
if(( << i) & cha) b = fa[b][i];
}
if(a!=b)
{
for(ll i=(ll)log2(n);i>=;i--)
{
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];
b=fa[b][i];
}
}
a = fa[a][];
}
return a;
} inline ll find(ll pos,ll num)
{
ll val = cf[pos];
zou[pos] = ;
for(ll i=head[pos];i;i=s[i].next)
{
if(zou[s[i].v]) continue;
val += find(s[i].v,num);
}
if(val >= num) ma = max(ma,fedge[pos]);
return val;
} inline ll check(ll ans)
{
memset(zou,,sizeof(zou));
memset(cf,,sizeof(cf));
ll ce = ;
for(ll i=;i<=m;i++)
{
if(quest[i].len > ans)
{
cf[quest[i].a]++;
cf[quest[i].b]++;
cf[quest[i].lca] -= ;
ce++;
}
}
ma = -;
find(,ce);
if(ma == -) return ;
if(ans + ma >= maxlen) return ;
return ;
} int main(){
scanf("%lld%lld%",&n,&m);
for(ll i=;i<n;i++)
{
scanf("%lld%lld%lld",&u,&v,&val);
add(u,v,val);
add(v,u,val);
} dfs(,);
init();
for(ll i=;i<=m;i++)
{
scanf("%lld%lld",&u,&v);
quest[i].a = u;
quest[i].b = v;
quest[i].lca = lca(u,v);
quest[i].len = len[u] + len[v] - * len[quest[i].lca];
maxlen = max(maxlen , quest[i].len);
}
r = maxlen;
while(l < r)
{
ll mid = l + r >> ;
if(check(mid)) r = mid;
else l = mid + ;
}
printf("%lld",l);
}

然后上个100 的!!!
#include<cstdio>
#include<algorithm>
#define maxn 300300 << 1
using namespace std;
int head_edge[maxn],head_quest[maxn],fedge[maxn],f[maxn],len[maxn],vis[maxn],cf[maxn],n,m,ma,maxlen,tot,u,v,val,l,r; struct st{
int v,val,next;
}s[maxn]; struct que{
int u,v,lca,len,next;
que *es;
}quest[maxn]; inline int find(int x)
{
if(f[x] == x) return x;
f[x] = find(f[x]);
return f[x];
} inline void add_edge(int u,int v,int val)
{
tot++;
s[tot].v = v;
s[tot].val = val;
s[tot].next = head_edge[u];
head_edge[u] = tot;
} inline void add_quest(int u,int v)
{
tot++;
quest[tot].u = u;
quest[tot].v = v;
if(tot & ) quest[tot].es = &quest[tot + ];
else quest[tot].es = &quest[tot - ];
quest[tot].next = head_quest[u];
head_quest[u] = tot;
} inline void dfs_tarjan(int father,int now)
{
for(int i=head_edge[now];i;i=s[i].next)
{
if(father == s[i].v) continue;
fedge[s[i].v] = s[i].val;
len[s[i].v] = len[now] + s[i].val;
dfs_tarjan(now,s[i].v);
}
} inline void lca_tarjan(int father,int now)
{
for(int i=head_edge[now];i;i=s[i].next)
{
if(s[i].v == father) continue;
lca_tarjan(now,s[i].v);
}
for(int i=head_quest[now];i;i=quest[i].next)
{
if(vis[quest[i].v])
{
quest[i].lca = find(quest[i].v);
quest[i].len = len[quest[i].u] + len[quest[i].v] - * len[quest[i].lca];
quest[i].es -> lca = quest[i].lca;
quest[i].es -> len = quest[i].len;
maxlen = max(maxlen,quest[i].len);
}
}
vis[now] = ;
f[now] = father;
} inline int dfs_check(int pos,int num)
{
vis[pos] = ;
int val = cf[pos];
for(int i=head_edge[pos];i;i=s[i].next)
{
if(vis[s[i].v]) continue;
val += dfs_check(s[i].v,num);
}
if(val >= num) ma = max(ma,fedge[pos]);
return val;
} inline int check(int ans)
{
for(int i=;i<=n;i++)
{
vis[i] = ;
cf[i] = ;
}
int num = ;
ma = -;
for(int i=;i<=m;i++)
{
if(quest[i].len > ans)
{
cf[quest[i].u]++;
cf[quest[i].v]++;
cf[quest[i].lca] -= ;
num++;
}
}
if(!num) return ;
dfs_check(,num);
if(ma == - || ans + ma < maxlen) return ;
return ;
} int main(){
scanf("%d%d",&n,&m); for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&val);
add_edge(u,v,val);
add_edge(v,u,val);
}
dfs_tarjan(,);
tot = ;
for(int i=;i<=n;i++) f[i] = i;
for(int i=;i<=m;i++)
{
scanf("%d%d",&u,&v);
add_quest(u,v);
add_quest(v,u);
} lca_tarjan(,);
for(int i=;i<=m;i++) quest[i] = quest[i * ]; r = maxlen;
l = max(,r - );
while(l < r)
{
int mid = l + r >> ;
if(check(mid)) r = mid;
else l = mid + ;
}
printf("%d",l);
}

运输计划noip的更多相关文章
- 4632 NOIP[2015] 运输计划
4632 NOIP[2015] 运输计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 公元 2044 ...
- [NOIP 2015]运输计划-[树上差分+二分答案]-解题报告
[NOIP 2015]运输计划 题面: A[NOIP2015 Day2]运输计划 时间限制 : 20000 MS 空间限制 : 262144 KB 问题描述 公元 2044 年,人类进入了宇宙纪元. ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分
2109. [NOIP 2015] 运输计划 ★★★☆ 输入文件:transport.in 输出文件:transport.out 简单对比时间限制:3 s 内存限制:256 MB [题 ...
- NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)
Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...
- 【NOIP 2015 DAY2 T3】 运输计划 (树链剖分-LCA)
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- NOIP[2015] 运输计划
传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...
- [noip 2015]运输计划 [LCA][树链剖分]
用了luogu上的题目描述 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的 ...
- NOIP 2015运输计划
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
随机推荐
- Java——LinkedHashMap源码解析
以下针对JDK 1.8版本中的LinkedHashMap进行分析. 对于HashMap的源码解析,可阅读Java--HashMap源码解析 概述 哈希表和链表基于Map接口的实现,其具有可预测的迭 ...
- shell脚本每隔几秒执行
while true do cmd(shell 命令) sleep x(x为秒数) done ————————————————版权声明:本文为CSDN博主「这年头起名真难3232」的原创文章,遵循 C ...
- Java GC日志
JVM 命令:-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+UseSerialGC [GC (Allocatio ...
- 终于有人把 Docker 讲清楚了,万字详解!
一.简介 1.了解Docker的前生LXC LXC为Linux Container的简写.可以提供轻量级的虚拟化,以便隔离进程和资源,而且不需要提供指令解释机制以及全虚拟化的其他复杂性.相当于C++中 ...
- 数据库与前端与Django目录
数据库 [怀心抱素]初步认识数据库 [怀心抱素]mysql的表操作 [怀心抱素]mysql记录操作 [怀心抱素]mysql索引与补充 [怀心抱素]python操作mysql 前端 [怀心抱素]HTML ...
- 计算机体系结构总结_Pipeline
Textbook:<计算机组成与设计——硬件/软件接口> HI<计算机体系结构——量化研究方法> QR 在前面一节里我们有了一块简单的RISC CPU,包括 ...
- 剑指offer-重构二叉树-树-python
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- python学习笔记(4)
第六章 字符串操作 1.字符串处理 (1)字符串字 spam='Say hi to Bob\' s mother 面量 python中输入字符串:以单引号开始和结束 (2)双引号:字符串可以用双引号开 ...
- 电子邮件协议:SMTP、POP3、IMAP4
常见的电子邮件协议:SMTP.POP3.IMAP4 邮件发送协议:SMTP协议 邮件读取协议:POP3.IMAP4协议 SMTP协议(simple mail transfer protocol ...
- Jmeter--函数助手之随机函数_Random(随机函数)
各函数调用方法如下:1)__Random( , , ),获取值的方式:${__Random( param1,param2 ,param3 )},param1为随机数的下限,param2为随机数的上限, ...