靠!这道题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. jmeter遍历时间戳

    list如下 实现步骤 实现步骤其实很简单,只需要一个foreach控制器,和一段转换时间戳的代码 第一步把时间戳提取出来 第二步把提取的时间戳传入foreach控制器,然后在控制器下面遍历转换 im ...

  2. [19/10/14-星期一] Python中的对象和类

    一.面向对象 ## 什么是对象? - 对象是内存中专门用来存储数据的一块区域. - 对象中可以存放各种数据(比如:数字.布尔值.代码) - 对象由三部分组成: 1.对象的标识(id) 2.对象的类型( ...

  3. [AGC040B]Two Contests

    Description 给出若干条线段 \((L[i], R[i])\) ,把他们分成两个非空的集合,最大化集合内线段交的和. \(n\le 10 ^ 5\) Solution 考虑最小的一个右端点 ...

  4. P2562kitty猫基因

    这道题是安徽NOI省选题,但是难度并没有那么难. 这道题是一个字符串的递归题,有很多大佬用线段树来写的(我也想学线段树,lowbit当头一棒).题意为全部相同则输出B或A,不同则分成长度相同的两个子串 ...

  5. P1106删数游戏

    这道题曾经在CQOJ上考过,是第二次做了. 这是一道使用字符串的贪心题.首先要根据机组例子来确定:删除递增序列的最后一位.即循环找到那一位后,把后面的数往前压.所以我在艰难处理完双重循环后(这个处理不 ...

  6. Luogu P5330 [SNOI2019]数论

    题目 如果\(P>Q\)的话我们先交换一下\(P,Q\). 我们先枚举所有满足第一个条件的数,对于\(x\equiv a_i(mod\ P)\),设\(x=a_i+kP(k\in[0,\lflo ...

  7. 小白学Python(20)—— Turtle 海龟绘图

    Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...

  8. 最长上升子序列(LIS) Medium2

    JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two ...

  9. django登录注册验证之密码包含特殊字符,确认密码一致实现,Form验证

    Form验证的原理 首先用户在注册界面提交表单,后台收到表单之后通过request.post取到数据然后传入已经写好的Form类 执行obj.is_valid()这里的obj为Form的实例,在For ...

  10. vue中获取滚动table的可视页面宽度,调整表头与列对齐(每列宽度不都相同)

    mounted() { // 在mounted中监听表格scroll事件 this.$refs.scrollTable.addEventListener( 'scroll',(event) => ...