传送门

Bzoj

解题思路

构造法。

对于每一次的倾倒操作,连边 \(newnode\to u,newnode\to v\)。

最后所有的反应都会在构造出来的树上的对应两点的 \(\text{LCA}\) 处发生。

把所有的反应按照 \(\text{LCA}\) 深度排序,深度相同则按输入顺序排序,模拟一下就好了。

记得用并查集判连通性,还有树剖LCA跑得比倍增LCA快多了。

细节注意事项

  • 咕咕咕。

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
} const int _ = 400002;
const int __ = 500002; int tot, head[_], nxt[_], ver[_];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; } int n, m, k, tt, g[_], Fa[_];
int fa[_], dep[_], siz[_], son[_], top[_];
struct node { int c, d, lca, id; } t[__];
inline bool cmp(const node& x, const node& y)
{ return dep[x.lca] == dep[y.lca] ? x.id < y.id : dep[x.lca] > dep[y.lca]; } inline int findd(int x) { return Fa[x] == x ? x : Fa[x] = findd(Fa[x]); } inline void dfs1(int u, int f) {
siz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
dfs1(v, u), siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
} inline void dfs2(int u, int topf) {
top[u] = topf;
if (!son[u]) return ; dfs2(son[u], topf);
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == son[u]) continue;
dfs2(v, v);
}
} inline int LCA(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
x = fa[fx], fx = top[x];
}
return dep[x] < dep[y] ? x : y;
} int main() {
read(n), read(m), read(k), tt = n;
for (rg int i = 1; i <= n; ++i) read(g[i]);
for (rg int i = 1; i <= n + m; ++i) Fa[i] = i;
for (rg int u, v, i = 1; i <= m; ++i) {
read(u), u = findd(u);
read(v), v = findd(v);
Fa[u] = Fa[v] = ++tt;
Add_edge(tt, u), Add_edge(tt, v);
}
for (rg int i = 1; i <= tt; ++i)
if (findd(i) == i) dfs1(i, 0), dfs2(i, i);
for (rg int c, d, i = 1; i <= k; ++i)
read(c), read(d), t[i] = (node) { c, d, LCA(c, d), i };
sort(t + 1, t + k + 1, cmp);
long long ans = 0;
for (rg int u, v, i = 1; i <= k; ++i) {
if (findd(u = t[i].c) != findd(v = t[i].d)) continue;
int tmp = min(g[u], g[v]);
ans += 2ll * tmp, g[u] -= tmp, g[v] -= tmp;
}
printf("%lld\n", ans);
return 0;
}

完结撒花 \(qwq\)

「PA2014」Fiolki的更多相关文章

  1. 「PA2014」Kuglarz

    传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  4. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  5. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  6. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  7. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  8. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  9. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

随机推荐

  1. properties配置文件在idea中默认utf-8可能会乱码

    改一个设置就好了

  2. 线程同步 - POSIX互斥锁

    线程同步 - POSIX互斥锁 概括 本文讲解POSIX中互斥量的基本用法,从而能达到简单的线程同步.互斥量是一种特殊的变量,它有两种状态:锁定以及解锁.如果互斥量是锁定的,就有一个特定的线程持有或者 ...

  3. HTMLUNIT另一种注册方法

    1 环境搭建: 1)下载 从链接:http://sourceforge.net/projects/htmlunit/files/htmlunit/ 下载最新的bin文件 2)关于bin文件 里面主要包 ...

  4. Suffix Tree(后缀树)

    这篇简单的谈谈后缀树原理及实现. 如前缀树原理一般,后缀trie树是将字符串的每个后缀使用trie树的算法来构造.例如banana的所有后缀: 0: banana 1: anana 2: nana 3 ...

  5. 《JavaScript高级程序设计》读书笔记(序)

    1.现大三暑假中,计划9月初北上找前端工作,大三一年时间都在健身和学习专业课知识,技术有点荒废了,7月份忙于学校安排的实习javaweb方向的,到现在才有整段的时间好好把基础巩固一. 2.这几天也在关 ...

  6. HA: Armour-Write-up

    下载地址:点我 bilibili:点我 信息收集 nmap扫存活找到IP为:192.168.116.140 ➜ ~ nmap -sn 192.168.116.1/24 Starting Nmap 7. ...

  7. MD5 加密解密字符串

    方法1: using System.Text; using System.Security.Cryptography; public string Hash(string toHash) { MD5C ...

  8. ipmitool命令

    1.remote access control powerIpmitool -I lanplus -H 192.168.0.10 -U username -P Password chassis pow ...

  9. JDBC 创建连接对象的三种方式 、 properties文件的建立、编辑和信息获取

    创建连接对象的三种方式 //第一种方式 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ ...

  10. 吴裕雄--天生自然ORACLE数据库学习笔记:优化SQL语句

    create or replace procedure trun_table(table_deleted in varchar2) as --创建一个存储过程,传入一个表示表名称的参数,实现清空指定的 ...