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 sum of f(p, q) for all pairs p, q (p ≠ q)? Can you answer his question?
对于一张图我们定义一个快乐值为f(p,q)为p到q的简单路径上最小点权值的最大值,求出所有f(p,q)(p != q)的和。(CF437D是求的平均值, 需要在此基础上除以一个(n(n-1)))
输入输出格式
输入格式:
The first line contains two integers n and m (2 ≤ n ≤ 10^5; 0 ≤ m ≤ 10^5). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 10^5). 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.
第一行输入两个数n,m表示点数和边数
第二行有n个数,表示每个点的权值
第三行到第3 + m行每行有两个数,表示连个点之间有条边
输出格式:
Output a integer — the value of sum .
一行一个整数,表示所有f(u, v) (u != v)的和。
题解
由题目的描述我们可以知道我们所有要经过的路径一定是在该图的最大生成树上的,但是这道题没有边权只有点权,所以我们要定义两个点之间的边权w[u,v] = min(a[u],a[v]) (w[u, v]表示两点之间的边权,a[u]表示u的点权)。在有了这棵树之后,我们需要的就只是两个点之间的路径权值了。刚开始我想了n^2暴力枚举两个点,然后求LCA之后再求最短长度,但时间超了,于是我们考虑下述做法:
我们在建立最小生成树的过程就是利用并查集将两个连通块连通的过程,因为树上两个点之间的路径是唯一的,所以连通两个连通块后有且只有这两个连通块之间的点连通了但不对连通块内部造成影响。而且我们是按从大到小的顺序去枚举的边, 所以这条新连接的树边一定是这两个连通块之间两点路径的开心值。于是我们只用在维护并查集的同时维护一下并查集的大小,然后根据乘法原理就可以解决了。
代码
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = ;
- int Father[MAX], size[MAX];
- inline int Get_Father(int x)
- {
- return Father[x] == x ? x : Father[x] = Get_Father(Father[x]);
- }
- struct Edge
- {
- int from, to, w;
- }edge[MAX];
- int a[MAX];
- bool comp(const Edge & a, const Edge & b)
- {
- return a.w > b.w;
- }
- int main()
- {
- // freopen("zoo.in", "r", stdin);
- // freopen("zoo.out", "w", stdout);
- int n, m;
- int x, y;
- scanf("%d%d", &n, &m);
- //printf("%d %d", n, m);
- for(register int i = ; i <= n; ++ i) scanf("%d", &a[i]), Father[i] = i, size[i] = ;
- for(register int i = ; i <= m; ++ i)
- {
- scanf("%d%d", &x, &y);
- edge[i].from = x, edge[i].to = y, edge[i].w = min(a[x], a[y]);
- }
- sort(edge + , edge + m+, comp);
- int tot = ; long long ans = ;
- for(register int i = ; i <= m; ++ i)
- {
- if(tot == n - ) break;
- x = Get_Father(edge[i].from), y = Get_Father(edge[i].to);
- if(x == y) continue;
- ans = ans +(long long) size[x] * size[y] * edge[i].w;
- size[x] += size[y];
- Father[y] = x;
- tot ++;
- }
- printf("%lld\n", ans << );
- return ;
- }
The Child and Zoo 题解的更多相关文章
- Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...
- cf437D The Child and Zoo
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 437 D. The Child and Zoo 并查集
题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...
- CF437D(The Child and Zoo)最小生成树
题目: D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- 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 ...
- Codeforces D - The Child and Zoo
D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 437D The Child and Zoo(并查集)
Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...
随机推荐
- ReactJS 页面跳转保存当前scrollTop回来时,自动移动到上次浏览器的位置
在移动端的操作的时候,相信大家都遇到到这种情况,翻了好几页了,点击一项进去查,然后回来的时候,还想回来我原来的位置. google上也找了一此,有一个组件,但是好像是如果想实现这个功能,页面就得用那个 ...
- 在浏览器中对访问的网页中的cookie添加和修改
做权限相关的东西,使用到了cookie,关于它的安全性,cookie在浏览器中,通过插件是可以对其进行修改的,如下: 1.FireFox 安装Edit This Cookie 插件,之后点击插件图标即 ...
- CSP学习之CryptoAPI初识
Crypto API目的就是提供开发者在windows下使用PKI的编程接口. Crypto 提供了很多的加解密相关函数,如编码.解码.加密解密,哈希,数字证书.证书管理证书存储等. 有关 ...
- Linux 连接 Xshell 及网络配置
一.准备工具 在WMware上已经装有Linux系统:WMware安装CentOS7文章. xshell连接工具: 二.修改相关配置 切换到root用户下: 配置主机名(可选): #方法一:替换原主机 ...
- 《Unity Shader入门精要》读书笔记(1)
主要是对第二章的整理 渲染流水线:由一个三维场景出发,生成(渲染)一张二维图像. 渲染流程:应用阶段.几何阶段.光栅化阶段. 应用阶段: 1. 把数据加载到显存中 渲染所需数据从硬盘,到内存,再到显存 ...
- vim-plug
vim包管理器vim-plug 安装 curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent. ...
- scss-算术运算符
由于scss具有编程语言的特点,那么运算符是必不可少的. 下面就通过代码实例分别做一下介绍. 一.赋值运算符: 赋值运算符就是我们最为熟悉的冒号(:),用来给声明的变量赋值. 代码实例如下: $hig ...
- 【代码笔记】Java深入学习——实现客户端发送文件到服务器的文件传输
Server.java package com.huaxin.lesson02; import java.io.FileOutputStream; import java.io.InputStream ...
- 为mongodb添加账号
进入切换到某一个数据库,我这里是位每个模块分配一个DataBase use 0 执行添加账号命令 db.createUser( { user: "*****", pwd: &quo ...
- SQL Server 与 ADO.NET 数据类型映射
SQL Server 数据类型映射 .NET Framework 4.5 SQL Server 和 .NET Framework 基于不同的类型系统. 例如,.NET Framework Decima ...