靠!这道题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. 操作系统汇编语言之AT&T指令

    转载时格式有问题,大家看原版吧! 作者:EwenWanW  来源:CSDN  原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...

  2. Excel区域复制粘贴

    这段工作做的是利用JAVA实现Excel的一块区域的复制并粘贴. 就本身对于 Excel跟 鼠标来说,这也是一个非常简单的操作. 但是 用 java的poi来做,还是 有点儿吃力的. 下面是之前做的一 ...

  3. 【Linux开发】Ubuntu图形界面切换与磁盘扩展分区

    Ubuntu14.04设置字符界面快捷键:ctrl-alt-f1 切换回图形界面:ctrl-alt-f7 为虚拟机拓展了30G的空间,挂在了/mnt/sda3这个目录下: 说明一下Ubuntu14.0 ...

  4. java中public、private、protected区别

    类中的数据成员和成员函数据具有的访问权限包括:public.private.protect.friendly(包访问权限) 1.public:public表明该数据成员.成员函数是对所有用户开放的,所 ...

  5. Java——LinkedList底层源码分析

    1.简介 LinkedList 是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢.另外,他还提供了 List 接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈 ...

  6. uva-796.critical links(连通图的桥)

    本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...

  7. C++基础-类和对象

    本文为 C++ 学习笔记,参考<Sams Teach Yourself C++ in One Hour a Day>第 8 版.<C++ Primer>第 5 版.<代码 ...

  8. 环境变量和Path环境变量

    环境变量 百度百科下的定义 一般是指在操作系统中用来指定操作系统运行环境的一些参数,如:临时文件夹位置和系统文件夹位置等. 环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所 ...

  9. css是干什么的

    css这些长篇累牍的参数,其实就是这些所谓的css编程者每天要干的事情了,他们把这些参数熟记于心,就像c++程序员,把这些函数库熟记于心一样,都是编程. css定制了每一个单独的组件,这些组件要么是相 ...

  10. 分布式理论: CAP、BASE (转)

    分布式系统的CAP理论是由Eric Brewer于1999年首先提出的,又被称作布鲁尔定理(Brewer's theorem),CAP是对Consistency(一致性).Availability(可 ...