【CF617D】Roads in Yusland
【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的更多相关文章
- 【CF671D】Roads in Yusland(贪心,左偏树)
[CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...
- 【CF671D】 Roads in Yusland(对偶问题,左偏树)
传送门 洛谷翻译 CodeForces Solution emmm,先引入一个对偶问题的概念 \(max(c^Tx|Ax \leq b)=min(b^Ty|A^Ty \ge c)\) 考虑这个式子的现 ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
- 【题解】Paid Roads [SP3953] [Poj3411]
[题解]Paid Roads [SP3953] [Poj3411] 传送门:\(\text{Paid}\) \(\text{Roads}\) \(\text{[SP3953]}\) \(\text{[ ...
- 【网络流】One-Way Roads
[网络流]One-Way Roads 题目描述 In the country of Via, the cities are connected by roads that can be used in ...
- 【图论】Codeforces 711D Directed Roads
题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...
- 【NOIP模拟】roads(最短路径转最小生成树)
题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 有 N 个城市,这些城市通过 M 条无向边互相连通,每条边有一个权值 Ci ,表示这条边的长度为 2^(Ci) ,没有两条边的长度是相同 ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
随机推荐
- The operation names in the portType match the method names in the SEI or Web service implementation class.
The Endpoint validation failed to validate due to the following errors: :: Invalid Endpoint Interfac ...
- REST Framework组件的解析源码
首先我们要知道解析器的作用 解析器就是对你请求体中的数据进行反序列化.封装 把你的所有的请求数据都封装在request.data中 以后就在request.data中获取数据 我们先导入rest_fr ...
- 《C++ Primer Plus》读书笔记之十—类和动态内存分配
第12章 类和动态内存分配 1.不能在类声明中初始化静态成员变量,这是因为声明描述了如何分配内存,但并不分配内存.可以在类声明之外使用单独的语句进行初始化,这是因为静态类成员是单独存储的,而不是对象的 ...
- 解决Oracle11g密码180天过期,账号锁住的问题
1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码 ...
- 铁乐学python_day29_模块与包学习4
大部份内容摘自授课老师的博客http://www.cnblogs.com/Eva-J/ 编译python文件 编译python文件是为了提高加载模块的速度,强调强调强调:提高的是加载速度而绝非运行速度 ...
- 铁乐学python-面向对象的更多说明
以下内容摘自授课老师的博客http://www.cnblogs.com/Eva-J/ 面向对象的更多说明 面向对象的软件开发 很多人在学完了python的class机制之后,遇到一个生产中的问题,还是 ...
- November 17th 2016 Week 47th Thursday
Don't cry because it is over. Smile because it happened. 不要因为结束而哭泣:微笑吧,因为我们曾经拥有. My ex-girlfriend ha ...
- 题解 P1894 【[USACO4.2]完美的牛栏The Perfect Stall】
题面 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- Zepto的SwipeUp 在 android 和微信 的解决方案
Zepto的SwipeUp 在 android 和微信 的解决方案 时间:2016-04-19 22:20:09 作者:zhongxia 问题解决方案: Q:为什么swipeUp和swipeDown在 ...
- 团队作业—预则立&&他山之石(改)
首先特别感谢刘乾学长腾出他宝贵的时间接受我的采访,为我们提出宝贵的建议,深表感谢. 1.他山之石,可以攻玉.借鉴前人的经验可以使我们减少很多走弯路的地方,这也是本次采访的目的,参考历届学长的经验,让我 ...