看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和。

用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块中的点数。

下面是过程分析。过程中一共运用了两个并查集。

[数据]
1
10 3
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 9
5 10
9 6 3 1 50
1 4 2 5 20
9 10 8 10 40
[分析]
首先我们将图构建出来

我们将m次询问按权值从小到大排序后:

1 4 2 5 20
9 10 8 10 40
9 6 3 1 50

对于(1,4) (2,5) 20 这一组询问:

  我们做如下操作, 首先查找1,4的lca(此例中lca即为1),先合并4与lca之间的边:利用up[] 往上合并直到超过或者到达lca。在往上合并的过程中,可以把下面的边权合并至父节点。利用up[]的路径压缩性质,下次再访问up[4],up[2]时会直接跳至1。此时cnt[1]已经加上了了从4到1路径中点的个数,cost[1]里面加上了了从4到1所有边权的和(利用一个并查集维护)。经过上述操作 parent[4] = parent[2] = 1, up[4] = up[2] = 1。同理我们继续合并1与lca(lca=1);

  合并完之后,我们处理2,5这一对点,lca(2,5)=2; 首先我们合并5与lca之间的边,利用up[5]=5,我们在并查集中合并5与anc[5][0](是倍增lca,所以即为5的2^0级父节点) 我们知道anc[5][0]=2;所以在merge(5,anc[5][0],cost)的过程中,实际上是把边权和点数附加到了anc[5][0]的父节点上,即1(详见上一段操作中,2已经被合并至1上,即parent[2] = 1),也就是说cnt[1]记录了5到lca路径中点的个数,cost[1]里面加上了5到lca所有边权的和,且经过这一段操作,up[5] = 1, parent[5] = 1。同理,我们继续合并2与lca(lca=2)。

对于9 10 8 10 40 这一组询问,

  lca[9,10]=5,照上面的过程合并中将{1,2,4,5,9 ,10}都加进了父节点中,在处理8,10(lca(8,10)=2)中我们发现,810路径之中有已经被赋值的边且肯定比当前要赋的值(因为询问是按权值从小到大来的),这个时候 up数组 就要发挥作用了。第一次询问up[8] = 8,合并[8,anc[8][0]=4],下一次询问up[8] = 4,合并[4 , anc[4][0]=2],这个时候就,因为parent[2] = 1,所以8被记录进了cnt[1],且边权也被加到了cost[1],再之后up[4] = 1,直接跳到了lca=2节点的上面,提示我们不需要再继续合并了(deep[1]<deep[2])。之后都进行相似的操作就可以得出答案 [9, 280]。

就像这样 , 9 10 8 10 50这组询问实际上只将3条蓝色边赋值成了40,橙色的边早已赋值成了更优的20。

  实际上,up数组保存的是与自己不在一个联通块(另一个并查集维护联通快性质)的且最接近自己的父节点,比如此例子中up[4]=1,up[8]=1,(此例子中较特殊,已经不存在深度更小的顶点了,假如第一组询问换成2 4 2 5 20,那么up[2] = up[4] = 1)直接跳到了最高点,提示我们不需要继续合并了,之后的边肯定都已经赋过值了,且值更优。

/*hdu6074[并查集+LCA+思维] 2017多校4*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct node {
int a, b, c, d;
LL w;
bool operator < (node& x) {
return w < x.w;
}
} q[];
vector<int> G[];
int T, m, n;
int up[], p[], deep[], anc[][];
LL cnt[], cost[];
void init() {
for (int i = ; i <= n; i++) {
G[i].clear();
}
memset(deep, , sizeof(deep));
for (int i = ; i <= n; i++)
cost[i]=,cnt[i]=,p[i] = up[i] = i;
}
int getfa(int x) {
return p[x] == x ? x : p[x] = getfa(p[x]);
}
int getup(int x) {
return up[x] == x ? x : up[x] = getup(up[x]);
}
void dfs(int u, int fa) {
anc[u][] = fa;
for (int i = ; i < ; i++) {
anc[u][i] = anc[anc[u][i - ]][i - ];
}
for (int i = ; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
deep[v] = deep[u] + ;
dfs(v, u);
}
}
int lca(int u, int v) {
if (deep[u] < deep[v]) swap(u, v);
for (int i = ; ~i; i--) {
if (deep[v] <= deep[anc[u][i]]) {
u = anc[u][i];
}
}
if (u == v) return u;
for (int i = ; ~i; i--) {
if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
}
return anc[u][];
}
void merge(int u, int v, LL w) {
int x = getfa(u);
int y = getfa(v);
if (x == y) return;
p[x] = y;
cnt[y] += cnt[x];
cost[y] += cost[x] + w;
}
void jump(int u, int v, LL w) {
for (;;) {
u = getup(u);
if (deep[u] <= deep[v]) return;
merge(u, anc[u][], w);
up[u] = anc[u][];
}
}
void solve() {
dfs(, );
sort(q + , q + m + );
for (int i = ; i <= m; i++) {
int k = lca(q[i].a, q[i].b);
//cout << q[i].w << endl;
jump(q[i].a, k, q[i].w);
jump(q[i].b, k, q[i].w);
k = lca(q[i].c, q[i].d);
jump(q[i].c, k, q[i].w);
jump(q[i].d, k, q[i].w);
merge(q[i].a, q[i].c, q[i].w);
}
printf("%lld %lld\n",cnt[getfa()],cost[getfa()]);
}
int main() {
scanf("%d", &T);
while (T--) {
int u, v;
scanf("%d%d", &n, &m);
init();
for (int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = ; i <= m; i++) {
scanf("%d%d%d%d%lld", &q[i].a, &q[i].b, &q[i].c, &q[i].d, &q[i].w);
}
solve();
}
return ;
}
/*
2
10 3
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 9
5 10
1 4 2 5 20
9 10 8 10 40
9 6 3 1 50
10 1
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 9
5 10
1 7 9 10 20
*/

顺便附上st表lca的版本.

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = ;
typedef long long LL; struct node {
int a, b, c, d;
LL w;
bool operator < (node& x) {
return w < x.w;
}
} q[];
int pos[MAX_N], dep[MAX_N << ], h[MAX_N << ], up[MAX_N], p[MAX_N], s[MAX_N];
int st[MAX_N << ][], len[MAX_N << ], tot = , deep[MAX_N];
vector<int> G[MAX_N];
int T, m, n;
LL cnt[MAX_N], cost[MAX_N];
void init() {
tot = ;
for (int i = ; i <= n; i++) {
G[i].clear();
}
for (int i = ; i <= n; i++)
cost[i] = , cnt[i] = , p[i] = up[i] = i;
}
int getfa(int x) {
return p[x] == x ? x : p[x] = getfa(p[x]);
}
int getup(int x) {
return up[x] == x ? x : up[x] = getup(up[x]);
}
void dfs(int u, int fa, int dps) {
s[u] = fa;
deep[u]=dps;
h[++tot] = u, pos[u] = tot, dep[tot] = dps;
for (int i = ; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u, dps + );
h[++tot] = u;
dep[tot] = dps;
}
}
void st_init() {
len[] = -;
for (int i = ; i <= tot; i++) {
st[i][] = i;
len[i] = ((i & (i - )) == ) ? len[i - ] + : len[i - ];
}
for (int j = ; j <= len[tot]; j++) {
for (int i = ; i + ( << j) - < tot; i++) {
int a = st[i][j - ], b = st[i + ( << (j - ))][j - ];
st[i][j] = dep[a] < dep[b] ? a : b;
}
}
}
int query(int l, int r) {
int k = len[r - l + ];
int a = st[l][k], b = st[r - ( << k) + ][k];
return dep[a] < dep[b] ? a : b;
}
int lca(int u, int v) {
int l = pos[u], r = pos[v];
if (l > r) swap(l, r);
return h[query(l, r)];
}
void merge(int u, int v, LL w) {
int x = getfa(u);
int y = getfa(v);
if (x == y) return;
p[x] = y;
cnt[y] += cnt[x];
cost[y] += cost[x] + w;
}
void jump(int u, int v, LL w) {
for (;;) {
u = getup(u);
if (deep[u] <= deep[v]) return;
merge(u, s[u], w);
up[u] = s[u];
//cout << u << ' ' << s[u] << endl;
}
}
void solve() {
dfs(, , );
st_init();
sort(q + , q + m + );
for (int i = ; i <= m; i++) {
int k = lca(q[i].a, q[i].b);
//cout << k << endl;
jump(q[i].a, k, q[i].w);
jump(q[i].b, k, q[i].w);
k = lca(q[i].c, q[i].d);
jump(q[i].c, k, q[i].w);
jump(q[i].d, k, q[i].w);
merge(q[i].a, q[i].c, q[i].w);
}
printf("%lld %lld\n", cnt[getfa()], cost[getfa()]);
}
int main() {
scanf("%d", &T);
while (T--) {
int u, v;
scanf("%d%d", &n, &m);
init();
for (int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = ; i <= m; i++) {
scanf("%d%d%d%d%lld", &q[i].a, &q[i].b, &q[i].c, &q[i].d, &q[i].w);
}
solve();
}
return ;
}

hdu6074[并查集+LCA+思维] 2017多校4的更多相关文章

  1. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. hdu6035[dfs+思维] 2017多校1

    /*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...

  3. HDU6074 Phone Call (并查集 LCA)

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Tota ...

  4. Gym 100814C Connecting Graph 并查集+LCA

    Description standard input/output Statements Alex is known to be very clever, but Walter does not be ...

  5. Network-POJ3694并查集+LCA

    Network Time Limit: 5000MS   Memory Limit: 65536K       Description A network administrator manages ...

  6. [并查集+LCA USACO18OPEN ] Disruption

    https://www.luogu.org/problemnew/show/P4374 一看这道题就是一个妙题,然后题解什么树链剖分...珂朵莉树... 还不如并查集来的实在!我们知道并查集本来就是路 ...

  7. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

  8. 习题:过路费(kruskal+并查集+LCA)

    过路费  [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...

  9. BZOJ 4668 冷战(按秩合并并查集+LCA)

    4668: 冷战 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 627  Solved: 303[Submit][Status][Discuss] D ...

随机推荐

  1. LibreOJ #514. 「LibreOJ β Round #2」模拟只会猜题意

    内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 给定一个长度为 nnn 的序列 AAA . 定义 f(l,r)=∑i=lrAif(l,r ...

  2. 洛谷 P1426 小鱼会有危险吗

    题目描述 有一次,小鱼要从A处沿直线往右边游,小鱼第一秒可以游7米,从第二秒开始每秒游的距离只有前一秒的98%.有个极其邪恶的猎人在距离A处右边s米的地方,安装了一个隐蔽的探测器,探测器左右x米之内是 ...

  3. Codeforces 786E. ALT 最小割+倍增

    E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...

  4. IT的学习部落(持续更新)

    1.易百教程 - 专注于IT教程和实例     http://www.yiibai.com/ 2.站长特效 - js特效      http://www.zzjs.net/ 3.酷站-享受编程和技术所 ...

  5. 字符串转换JSON 的方法

    function (sJSON) { if (window.JSON) { return window.JSON.parse(sJSON); } else { return eval('(' + sJ ...

  6. 手机屏幕材质详解(TFT,TPS,OLED,AMOLED等)

    手机屏幕概括起来就是两种,一个是LCD,一个是OLED屏幕,这两个是屏幕显示技术的两大基础. 一 . LCD:Liquid Crystal Display,这是一种介于固态和液态之间的物质,称为液晶技 ...

  7. apropos linux

    Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...

  8. HTML5<section>元素

    HTML5<section>元素用来定义页面文档中的逻辑区域或内容的整合(section,区域),比如章节.页眉.页脚或文档中的其他部分. 根据W3C HTML5文档中:section里面 ...

  9. js cookie 操作

    <html> <head> <meta charset="utf-8"> <title>Javascript cookie</ ...

  10. python入门:UTF-8转换成GBK编码

    #!/usr/bin/env python # -*- coding:utf-8 -*- #UTF-8转换成GBK编码 #temp(临时雇员,译音:泰坡) #decode(编码,译音:迪口德) #en ...