靠!这道题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的更多相关文章

  1. 4632 NOIP[2015] 运输计划

    4632 NOIP[2015] 运输计划  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题解       题目描述 Description 公元 2044 ...

  2. [NOIP 2015]运输计划-[树上差分+二分答案]-解题报告

    [NOIP 2015]运输计划 题面: A[NOIP2015 Day2]运输计划 时间限制 : 20000 MS 空间限制 : 262144 KB 问题描述 公元 2044 年,人类进入了宇宙纪元. ...

  3. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

  4. cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分

    2109. [NOIP 2015] 运输计划 ★★★☆   输入文件:transport.in   输出文件:transport.out   简单对比时间限制:3 s   内存限制:256 MB [题 ...

  5. NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

    Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...

  6. 【NOIP 2015 DAY2 T3】 运输计划 (树链剖分-LCA)

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  7. NOIP[2015] 运输计划

    传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...

  8. [noip 2015]运输计划 [LCA][树链剖分]

    用了luogu上的题目描述 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的 ...

  9. NOIP 2015运输计划

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

随机推荐

  1. Python --链接MYSQL数据库与简单操作 含SSH链接

    项目是软硬件结合,在缺少设备的情况,需要通过接口来模拟实现与设备的交互,其中就需要通过从数据库读取商品的ID信息 出于安全考虑  现在很多数据库都不允许通过直接访问,大多数是通过SSH SSH : 数 ...

  2. 第十届山东省acm省赛补题(2)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      ...

  3. Java抽象接口技巧(一)

    原文链接 http://blog.csdn.net/qq_35101189/article/details/70799155 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个接口,但实现这个接 ...

  4. 部署CM集群首次运行报错:Formatting the name directories of the current NameNode.

    1. 报错提示 Formatting the name directories of the current NameNode. If the name directories are not emp ...

  5. 小z的洞穴之旅 QDUOJ 并查集+连通块

    小z的洞穴之旅 QDUOJ 并查集+连通块 原题链接 题意 小 z 同学在某个闲暇的周末决定去野外探险一波,结果在丛林深处中误打误撞进入了一个神秘的洞穴,虽然洞穴中光线昏暗,但小 z 凭借其敏锐的眼力 ...

  6. [LeetCode] 126. 单词接龙 II

    题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...

  7. 卸载OpenIV

    最近把 GTA5 卸载了,于是也想把用来修改 MOD 的 OpenIV 也卸载了. 结果在设置中,进行卸载的时候,弹出这个窗口 解决方案 1.首先打开 文件所在位置 2.右键,选择 属性,打开文件所在 ...

  8. 使用css3的repeating-linear-gradient画虚线

    还在用 border-style: dashed 画虚线吗?虽然也是虚线,但是不能控制每一个虚线的宽度 .dashed { height: 1px; background-image: repeati ...

  9. 关于ES6的新特性

    1  let声明变量 01    let声明的变量,不可重复声明,比如 let   a=1  : let   a=2 :这样申明会报错 02    let声明的变量,有块级作用域,比如  if( ){ ...

  10. 13.解决SUSELinux用户登录Module is unknow问题

    问题原因: linux login:rootpasswd:Last login:Fri Jul 26 09:55:31 CST 2019 from 192.168.168.1 on pts.5You ...