Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads.
Our child is very smart. Imagine the child want to go from area p to area q. Firstly he considers all the simple routes from p to q. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q). Finally, the child chooses one of the routes for which he writes down the value f(p, q).
After the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p ≠ q)? Can you answer his question?
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each line contains two integers xi and yi (1 ≤ xi, yi ≤ n; xi ≠ yi), denoting the road between areas xi and yi.
All roads are bidirectional, each pair of areas is connected by at most one road.
Output a real number — the value of .
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
4 3
10 20 30 40
1 3
2 3
4 3
16.666667
3 3
10 20 30
1 2
2 3
3 1
13.333333
7 8
40 20 10 30 20 50 40
1 2
2 3
3 4
4 5
5 6
6 7
1 4
5 7
18.571429
Consider the first sample. There are 12 possible situations:
- p = 1, q = 3, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 4, q = 3, f(p, q) = 30.
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 4, f(p, q) = 20.
- p = 4, q = 1, f(p, q) = 10.
Another 6 cases are symmetrical to the above. The average is .
Consider the second sample. There are 6 possible situations:
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 1, q = 3, f(p, q) = 10.
Another 3 cases are symmetrical to the above. The average is .
(Tag好像很吓人的样子,不过这个是两个解法的Tag取并)
题目大意 定义一条路径的权为这条路径所有经过点的点权的最小值,无向连通图中任意不同的两点的"距离"为这两点间所有简单路径中最大的权。给定一个无向连通图,求它的任意不同的两点间的"距离"的平均值。
显然你需要求出任意不同的两点间"距离"的和,所以你需要在图上进行路径统计。
然而表示并不会,所以我们先把问题放在树上,然后再考虑推广到图上。
对于树上路径统计的问题常用算(套)法(路)->树分治。由于不会边分只会点分,所以现在考虑计算经过重心的所有路径的权和。
根据常用点分套路,肯定需要计算每个子树中每个点到这个子树的根的"距离",然后对当前分治的树进行统计,然后减去每个子树内部的不合法路径(不是简单路径)。
这个统计很简单,你只需排个序,然后你就可以知道每个点到哪些点的"距离"是自己到所在子树的根的"距离"。
于是便可以在的时间内水掉这个子任务。
现在考虑推广的问题(其实这也不算推广吧。。反向搜索比较合适吧)。仔细看题,然后用贪心的思想你可以得到一个结论:任意两个不同点之间的最优路径一定在最大生成树上。
所以你只需要用Kruskal先建出最大生成树(边权是它连接的两个点的点权的最小值),然后再进行点分治就好了。(下面是编程复杂度的点分治的代码)
Code
/**
* Codeforces
* Problem#437D
* Accepted
* Time:202ms
* Memory:15452k
*/
#include <bits/stdc++.h>
using namespace std;
#define smax(a, b) a = max(a, b)
typedef bool boolean; typedef class union_found {
public:
int *f; union_found() { }
union_found(int n) {
f = new int[(n + )];
for(int i = ; i <= n; i++)
f[i] = i;
} int find(int x) {
return (f[x] == x) ? (x) : (f[x] = find(f[x]));
} void unit(int fa, int so) {
int ffa = find(fa);
int fso = find(so);
f[fso] = ffa;
} boolean isConnected(int a, int b) {
return find(a) == find(b);
}
}union_found; typedef class Edge1 {
public:
int u;
int v;
int w; boolean operator < (Edge1 b) const {
return w > b.w;
}
}Edge1; int n, m;
int *vals;
Edge1* es;
union_found uf;
vector<int> *g; inline void init() {
scanf("%d%d", &n, &m);
vals = new int[(n + )];
es = new Edge1[(m + )];
g = new vector<int>[(n + )];
for(int i = ; i <= n; i++)
scanf("%d", vals + i);
for(int i = ; i <= m; i++)
scanf("%d%d", &es[i].u, &es[i].v), es[i].w = min(vals[es[i].u], vals[es[i].v]);
} boolean *vis;
int *siz;
long long sum = ;
void tree_dp(int node, int fa) {
siz[node] = ;
for(int i = ; i < (signed)g[node].size(); i++) {
int& e = g[node][i];
if(e == fa || vis[e]) continue;
tree_dp(e, node);
siz[node] += siz[e];
}
} void getG(int node, int fa, int all, int& mins, int& G) {
int msiz = ;
for(int i = ; i < (signed)g[node].size(); i++) {
int& e = g[node][i];
if(e == fa || vis[e]) continue;
getG(e, node, all, mins, G);
smax(msiz, siz[e]);
}
smax(msiz, all - siz[node]);
if(msiz < mins) mins = msiz, G = node;
} int cnt;
int* dis;
void dfs(int node, int fa, int d) {
dis[++cnt] = d;
// printf("%d: %d\n", node, d);
for(int i = ; i < (signed)g[node].size(); i++) {
int& e = g[node][i];
if(e == fa || vis[e]) continue;
dfs(e, node, min(d, vals[e]));
}
} long long calc(int l, int r, int lim) {
long long rt = ;
sort(dis + l, dis + r + );
for(int i = l; i <= r; i++) {
rt += (r - i) * 1LL * min(dis[i], lim);
// printf("%d %d %d %d %d\n", l, r, i, dis[i], rt);
}
return rt;
} void dividing(int node) {
int mins = , G;
tree_dp(node, );
if(siz[node] == ) return;
getG(node, , siz[node], mins, G);
// cout << G << endl;
cnt = ;
vis[G] = true;
for(int i = , l; i < (signed)g[G].size(); i++) {
int& e = g[G][i];
if(vis[e]) continue;
l = cnt;
dfs(e, G, vals[e]);
// cout << l << " " << e << " " << cnt << endl;
sum -= calc(l + , cnt, vals[G]);
// cout << cnt << endl;
}
dis[++cnt] = vals[G];
sum += calc(, cnt, vals[G]);
// cout << sum << endl;
for(int i = , l; i < (signed)g[G].size(); i++) {
int& e = g[G][i];
if(vis[e]) continue;
dividing(g[G][i]);
}
} inline void solve() {
sort(es + , es + m + );
uf = union_found(n);
int fin = ;
for(int i = ; i <= m && fin < n; i++) {
if(!uf.isConnected(es[i].u, es[i].v)) {
uf.unit(es[i].u, es[i].v);
g[es[i].u].push_back(es[i].v);
g[es[i].v].push_back(es[i].u);
// printf("connect %d %d\n", es[i].u, es[i].v);
fin++;
}
}
vis = new boolean[(n + )];
siz = new int[(n + )];
dis = new int[(n + )];
memset(vis, false, sizeof(boolean) * (n + ));
dividing();
long long c = n * 1LL * (n - );
printf("%.9lf", (sum << ) * 1.0 / c);
} int main() {
init();
solve();
return ;
}
The Child and Zoo(Point Division)
不得不说贪心世界博大精深。下面将用一个神奇的贪心将时间复杂度去掉一个log。
在跑最大生成树的时候其实就可以直接出答案了。对于一条边将两个原本不连通的连通块连接起来,因为是第一次连接,所以这条边在最大生成树上,这条边对总和有贡献。那么会贡献多少次呢?乘法原理算一算,就是两边点数的乘积(两个联通块内的边的权值都比它大,所以一个点在其中的一个联通块中,另一个点在另外一个联通块中,它们的"距离"就是这条边的权值)。
于是编程复杂度成功下降到O(能1a)。
Code
/**
* Codeforces
* Problem#437D
* Accepted
* Time: 62ms
* Memory: 4408k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; typedef class union_found {
public:
int *f;
int *s; union_found() { }
union_found(int n) {
f = new int[(n + )];
s = new int[(n + )];
for(int i = ; i <= n; i++)
f[i] = i, s[i] = ;
} int find(int x) {
return (f[x] == x) ? (x) : (f[x] = find(f[x]));
} void unit(int fa, int so) {
int ffa = find(fa);
int fso = find(so);
f[fso] = ffa;
s[ffa] += s[fso];
} boolean isConnected(int a, int b) {
return find(a) == find(b);
}
}union_found; typedef class Edge {
public:
int u;
int v;
int w; boolean operator < (Edge b) const {
return w > b.w;
}
}Edge; int n, m;
int *vals;
Edge* es;
union_found uf; inline void init() {
scanf("%d%d", &n, &m);
vals = new int[(n + )];
es = new Edge[(m + )];
for(int i = ; i <= n; i++)
scanf("%d", vals + i);
for(int i = ; i <= m; i++)
scanf("%d%d", &es[i].u, &es[i].v), es[i].w = min(vals[es[i].u], vals[es[i].v]);
} long long sum = ;
inline void solve() {
sort(es + , es + m + );
uf = union_found(n);
int fin = ;
for(int i = ; i <= m && fin < n; i++) {
if(!uf.isConnected(es[i].u, es[i].v)) {
sum += uf.s[uf.find(es[i].u)] * 1LL * uf.s[uf.find(es[i].v)] * es[i].w;
uf.unit(es[i].u, es[i].v);
fin++;
}
}
long long c = n * 1LL * (n - );
printf("%.9lf", (sum << ) * 1.0 / c);
} int main() {
init();
solve();
return ;
}
Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树的更多相关文章
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- Codeforces 437D The Child and Zoo(并查集)
Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...
- 【BZOJ4025】二分图(线段树分治,并查集)
[BZOJ4025]二分图(线段树分治,并查集) 题面 BZOJ 题解 是一个二分图,等价于不存在奇环. 那么直接线段树分治,用并查集维护到达根节点的距离,只计算就好了. #include<io ...
- 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
[CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...
- codeforces 437D The Child and Zoo
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Dash Speed【好题,分治,并查集按秩合并】
Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...
- [BZOJ3038]上帝造题的七分钟2 树状数组+并查集
考试的时候用了两个树状数组去优化,暴力修改,树状数组维护修改后区间差值还有最终求和,最后骗了40分.. 这道题有好多种做法,求和好说,最主要的是开方.这道题过的关键就是掌握一点:在数据范围内,最多开方 ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- 【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
题目描述 给你 $n$ 个点,支持 $m$ 次操作,每次为以下两种:连一条边,保证连完后是一棵树/森林:询问一个点能到达的最远的点与该点的距离.强制在线. $n\le 3\times 10^5$ ,$ ...
随机推荐
- node.js连接MongoDB数据库,db.collection is not a function完美解决
解决方法一. mongodb数据库版本回退: 这个错误是出在mongodb的库中,在nodejs里的写法和命令行中的写法不一样,3.0的api已经更新和以前的版本不不一样,我们在npm中没指定版本号的 ...
- Service Fabric本地开发部署修改数据目录
以修改5节点非安全模式为例: 在C:\Program Files\Microsoft SDKs\Service Fabric\ClusterSetup\NonSecure\FiveNode目录下,修改 ...
- 002-golang安装配置
1.安装位置: 2.环境变量. path的值如下: 3.工作目录.
- 超简单系列:ubuntu 13.04 安装 apache2.2+mod_wsgi+Django
1,Ubuntu更新系统 sudo apt-get update sudo apt-get upgrade 2,安装apache,mod_wsgi,Django sudo apt-get instal ...
- css实现文字太长,显示省略号
/*显示为省略号*/ overflow:hidden;/*隐藏*/ white-space:nowrap;/*文本不进行换行*/text-overflow:ellipsis;/*省略号*/ /*强制 ...
- 20155228 2016-2017-2 《Java程序设计》第8周学习总结
20155228 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量, ...
- 关于this指向性的问题
函数调用 首先需要从函数的调用开始讲起. JS(ES5)里面有三种函数调用形式: func(p1, p2) obj.child.method(p1, p2) func.call(context, p1 ...
- hive 实现一个字段多行转一行 和 一行转多行
1.多行转一行 多行转一行可以通过concat_ws(',',collect_set(col_name)) as col_new的方式实现,可以参考:https://www.cnblogs.com/s ...
- mybatis源码解析8---执行mapper接口方法到执行mapper.xml的sql的过程
上一篇文章分析到mapper.xml中的sql标签对应的MappedStatement是如何初始化的,而之前也分析了Mapper接口是如何被加载的,那么问题来了,这两个是分别加载的到Configura ...
- c# 图像呈现控件PictureBox
在c#中可以使用PictureBox控件来呈现图像,图像资源可以来自文件,也可以是存在内存中的位图对象.可以显示本地图像文件或来自网络的图片,也可以来自项目文件中的图像. 从URI加载图像文件. 调用 ...