运输计划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 掌管一家 ...
随机推荐
- sql语句经验
1:拼接字段模糊查询 where aaa(字段) not like CONCAT(DATE_FORMAT(new(),"%Y-%m-%d"),'%完成%'): 2:备份表中数据导 ...
- Java基础/时间日期格式
Java时间日期格式转换 一.Date转String和String转Date 参考博客:https://www.cnblogs.com/sharpest/p/7879377.html public s ...
- 红帽学习笔记[RHCSA] 第二课[文件、目录、相关命令]
第二课 常用的目录结构与用途 / 根目录 /boot 存储的是系统起动时的信息和内核等 /dev 存储的是设备文件 /etc 存储的是系统的配置文件 /root 存储的是root用户的家目录 /hom ...
- go io库
1 io.Reader和io.Writer的获取 tcp协议下的io.Reader是从conn中获取到的,因为要先建立conn,conn建立成功之后,然后读写数据. 2 真正的读写 2.1 io.Co ...
- mui前端框架下拉刷新分页加载数据
前台 mui.init(); (function($) { //阻尼系数 var deceleration = mui.os.ios?0.003:0.0009; $('.mui-scroll-wrap ...
- Nginx 配置二级虚拟目录访问 Laravel 重写
server { listen 80; server_name _; root /opt/sites; index index.php index.html index.htm; etag on; g ...
- SSM框架中数据库无法连接的问题
首先是SSM框架中所有的配置都是没有问题的,而且项目在其他人的环境上也能正常访问数据库:那么最有可能的就是数据库版本的问题导致数据库连接不上,服务器给我的报错是: 15:37:25.902 [C3P0 ...
- python中输入某年某月某日,判断这一天是这一年的第几天?
输入某年某月某日,判断这一天是这一年的第几天?程序分析 特殊情况,闰年时需考虑二月多加一天: 直接上代码 #定义一个函数,判断是否为闰年 def leapyear(y): return (y % 40 ...
- 转载 PowerDesigner导出mysql数据结构
转自:https://blog.csdn.net/dkingyaoyao/article/details/84586146 好久没有使用PowerDesigner,突然想用它导出数据结构,居然忘记了. ...
- es7.2版本安装ik分词
(一)到官网下载https://github.com/medcl/elasticsearch-analysis-ik对应版本的ik(直接下载releases版本,避免maven打包!!!如果不是这个版 ...