【CF617D】Roads in Yusland

题面

蒯的洛谷的

题解

我们现在已经转化好了题目了,戳这里

那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\)。

再将路径\(u\rightarrow v(dep_u>dep_v)\)以其权值为关键字丢到以\(u\)为根的左偏树(小根)上,

再进行\(dfs\),合并\(x\)的所有儿子的左偏树,对于点\(x\),我们能选则选,

还有打标记等细节,详见代码。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
namespace IO {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char gc() {
if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return *is++;
}
}
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = IO::gc();
if (ch == '-') w = -1 , ch = IO::gc();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::gc();
return w * data;
}
const int MAX_N = 3e5 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}; fir[u] = e_cnt++; }
struct chain { int u, v, w; } a[MAX_N];
int N, M, c[MAX_N], dep[MAX_N];
long long ans = 0;
void dfs(int x, int f) {
dep[x] = dep[f] + 1;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == f) continue;
dfs(v, x), c[x] += c[v];
}
}
struct Node {
int ls, rs, dis;
int v, ed, tag;
} t[MAX_N];
int rt[MAX_N];
void puttag(int x, int v) { t[x].tag += v, t[x].v += v; }
void pushdown(int x) {
if (!t[x].tag) return ;
if (t[x].ls) puttag(t[x].ls, t[x].tag);
if (t[x].rs) puttag(t[x].rs, t[x].tag);
t[x].tag = 0;
}
int merge(int x, int y) {
if (!x || !y) return x + y;
pushdown(x), pushdown(y);
if (t[x].v > t[y].v) swap(x, y);
t[x].rs = merge(t[x].rs, y);
if (t[t[x].ls].dis < t[t[x].rs].dis) swap(t[x].ls, t[x].rs);
t[x].dis = t[t[x].rs].dis + 1;
return x;
}
void solve(int x, int f) {
for (int i = fir[x]; ~i; i = e[i].next)
if (e[i].to != f) {
solve(e[i].to, x);
rt[x] = merge(rt[e[i].to], rt[x]);
}
if (x == 1) return ;
while (dep[t[rt[x]].ed] >= dep[x]) rt[x] = merge(t[rt[x]].ls, t[rt[x]].rs);
int w = t[rt[x]].v;
ans += w, puttag(rt[x], -w);
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi(), M = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(u, v), Add_Edge(v, u);
}
for (int i = 1; i <= M; i++) a[i] = (chain){gi(), gi(), gi()};
for (int i = 1; i <= M; i++) c[a[i].u]++, c[a[i].v]--;
dfs(1, 0);
for (int i = 2; i <= N; i++) if (c[i] <= 0) return puts("-1") & 0;
for (int i = 1; i <= M; i++) t[i] = (Node){0, 0, 0, a[i].w, a[i].v, 0}, rt[a[i].u] = merge(rt[a[i].u], i);
solve(1, 0);
cout << ans << endl;
return 0;
}

跑到洛谷\(rank1\)纪念一下:

好像还是cf的\(rank1\)呢qaq:

【CF617D】Roads in Yusland的更多相关文章

  1. 【CF671D】Roads in Yusland(贪心,左偏树)

    [CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...

  2. 【CF671D】 Roads in Yusland(对偶问题,左偏树)

    传送门 洛谷翻译 CodeForces Solution emmm,先引入一个对偶问题的概念 \(max(c^Tx|Ax \leq b)=min(b^Ty|A^Ty \ge c)\) 考虑这个式子的现 ...

  3. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

  4. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

  5. 【题解】Paid Roads [SP3953] [Poj3411]

    [题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...

  6. 【网络流】One-Way Roads

    [网络流]One-Way Roads 题目描述 In the country of Via, the cities are connected by roads that can be used in ...

  7. 【图论】Codeforces 711D Directed Roads

    题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...

  8. 【NOIP模拟】roads(最短路径转最小生成树)

    题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长度是相同 ...

  9. 【Codeforces 25C】Roads in Berland

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...

随机推荐

  1. 转:eclipse的快捷键

    22 21 Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...

  2. 限定pan手势只能在圆内移动view

    限定pan手势只能在圆内移动view 效果: 虽然看起来很简单,但实现原理还是稍微有点复杂-_-!! 核心的地方,就是需要计算pan手势的点与指定点的距离,不能超过这个距离,超过了就让动画还原,很容易 ...

  3. 《C++ Primer Plus》读书笔记之十二—C++中的代码重用

    第14章 C++中的代码重用 1.C++代码重用方法:公有继承.使用本身是另一个类的对象的类成员(这种方法称为包含.组合或层次化).私有或保护继承.类模板等. 2.模板特性意味着声明对象时,必须指定具 ...

  4. JAVA入门之程序设计环境搭建

    这篇文章写给刚接触或者想学JAVA的新朋友.学习JAVA,需要找一本好的入门书籍,推荐<Java从入门到精通>,然后就是JAVA程序设计开发环境的搭建. 首先,我们需要安装JAVA开发工具 ...

  5. 统计过程控制与评价 Cpk、SPC、PPM

    Cpk(Process capability index)--工序能力指数 SPC(Statisical Process Control)--工艺过程统计受控状态分析 PPM(Parts Per Mi ...

  6. 【原创】python web应用性能测试出现的问题以及使用的工具

    uwsgi参数设置问题 问题汇总 listen 参数设置过小导致并发上不去 解决办法 更改listen参数, 请先确认linux系统的上限(tcp_max_syn_backlog,somaxconn) ...

  7. 1026. [SCOI2009]windy数【数位DP】

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个windy数? I ...

  8. 3192: [JLOI2013]删除物品

    3192: [JLOI2013]删除物品 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1366 Solved: 794 [Submit][Statu ...

  9. asp.net Web API简单的特性路由配置

    一,控制器代码: [RoutePrefix("ajilisiwei")]  //加URL前缀 (非必需)    public class ShoppingController : ...

  10. PHP MemCached高级缓存配置图文教程

    memcache是一个高性能的分布式的内存对象缓存系统,它能够用来存储各种格式的数据,包括图像.视频.文件以及数据库检索的结果等.     1.Memcache相关介绍 memcache是一个高性能的 ...