「PA2014」Fiolki
传送门
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的更多相关文章
- 「PA2014」Kuglarz
传送门 memset0好评 解题思路 其实这是一道图论题 不难发现,如果知道了 \(\sum1...i\) 和 \(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
随机推荐
- 解决mysql和navicat乱码问题
1,首先进入mysql的my.ini文件,进行编码修改,全部改成utf8编码(这里就不赘述了,网上一搜一堆) 2,最重要的一点,把原先navicat创建的连接断开,重新创建新连接,在该新连接下创建库, ...
- XCOJ 1249: 全自动奖学金计算系统
1249: 全自动奖学金计算系统 时间限制: 1 Sec 内存限制: 64 MB提交: 305 解决: 54 标签提交统计讨论版 题目描述 宣城校区从今年开始,在原有奖学金制度上,设立专项奖学金. ...
- 【JQuery 选择器】 案例
1.查找以id的某个字段开头的元素 setTimeout(function () { $("a[id^='menu_']").each(function () { $(this). ...
- JavaScript中关于隐式转换的一些总结
JavaScript运算符中的隐式转换规律:一.递增递减运算符(前置.后置)1.如果包含的是有效数字字符串或者是有效浮点数字符串,则会将字符串转换(Number())为数值,再进行加减操作,返回值的类 ...
- C/C++网络编程7——多进程服务器端之fork函数
通过前面几节的内容,我们已经可以实现基本的C/S结构的程序了,但是当多个客户端同时向服务器端请求服务时,服务器端只能按顺序一个一个的服务,这种情况下,客户端的用户是无法忍受的.所以虚实现并发的服务器端 ...
- VB.NET中Sub和Function的区别
function是函数,sub是子程序,都可以传递参数,但函数有返回值,子程序没有 function 可以用自身名字返回一个值,sub 需定义别的变量,用传址方式传回值. Sub 过程与Functio ...
- Plastic Sprayers Manufacturer - Spray Principle, Spray Note
The Plastic Sprayers Manufacturer stated that the spray is artificial fogging. Simply put, th ...
- selenium webdriver 定位元素 第一部分
static final WebDriver driver = ExplorerBase.IESetting(); // 实例化一个浏览器对象 @Test //@Ignore public void ...
- 二十一 Struts的数据校验两种方式:手动编码和xml校验
数据的校验: 一.前台校验:JS校验 JS的校验不是必须的,JS可以被绕行,可以提升用户体验 二.后台校验:编码校验 必须的校验 三.校验的方式: 手动编码(不建议使用) 配置文件(支持) 手动编码的 ...
- Servlet 学习(四)
HTTP 响应的构成1.HTTP 响应行: 协议.状态.描述 HTTP 1.1 中定义的状态代码 100-199 是信息性代码,标示客户应该采取的其它动作 200-299 表示请求成功 300-399 ...