Abandoned country
A Abandoned country
最小生成树 + 树上任意点对距离之和
树上任意点对距离之和:
计算时考虑每一个边,它的贡献值为它的 左端点以左的点的个数 * 右端点以右的点的个数 * 该边的权值。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,ll> PIL;
const int N =1e5+10;
const int NN = 1e6+10;
int father[N];
int n,m,cntt;
int vis[N];
ll tot;
double di;
struct Edge
{int a, b;ll v;bool operator< (const Edge &W) const{return v < W.v;}
}edge[NN];vector< pair<int,ll> >tedges[N];int find(int x)
{if (father[x] != x) father[x] = find(father[x]);return father[x];
}ll Kruskal()
{ll res =0;for (int i = 1; i <= n; i ++ ) father[i] = i;sort(edge, edge + m);for (int i = 0; i < m; i ++ ){int a = edge[i].a, b = edge[i].b;ll v =edge[i].v;if (find(a) != find(b)){tedges[a].push_back({b,v});tedges[b].push_back({a,v});res += v;father[find(a)] = find(b);}}return res;
}
ll dfs(int cur)
{vis[cur]=1;ll now = 0 ,ans = 1;for(int i=0;i<tedges[cur].size();i++){if(!vis[tedges[cur][i].first]){now = dfs(tedges[cur][i].first);ans += now;tot += tedges[cur][i].second * (n-now) * now; //tot保存边的贡献和//下方的点数可由dfs求得,其余的点为(n-下方的点)}}return ans;
}
int main()
{int T;scanf("%d",&T);while(T--){tot=0;cntt = 0;memset(vis,0,sizeof(vis));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)tedges[i].clear();//tedges[0] = {1,0};di = (double)n*(n-1)/2;for(int i=0;i<m;i++){scanf("%d%d%lld",&edge[i].a,&edge[i].b,&edge[i].v);}ll ans = Kruskal();dfs(1);printf("%lld %.2f\n",ans,(double)tot/di);}return 0;
}
Abandoned country相关推荐
- HDU-5723 Abandoned country
Problem Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Si ...
- hdu-5723 Abandoned country(最小生成树+期望)
题目链接: Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java ...
- 2019.9.17最小生成树知识点回顾
POJ 1797 Heavy Transportation(最大生成树-Prim) 最大生成树,方法模仿最小生成树,每次选最大边进行操作,即可. HDU 5723 Abandoned country( ...
- Codeforces 894.D Ralph And His Tour in Binary Country
D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...
- 2021牛客多校6 - Defend Your Country(点双缩点求割点)
题目链接:点击查看 题目大意:给出一个 nnn 个点和 mmm 条边组成的无向图,每个点都有点权,现在可以删除掉任意条边,问如何才能使得贡献和最大 贡献和是指,当删掉边后,对于每个连通块而言,如果连通 ...
- The Trip On Abandoned Railway(线段树+树状数组)
链接:https://ac.nowcoder.com/acm/problem/13891 来源:牛客网 题目描述 There are many ghosts at the abandoned stat ...
- A Story of One Country (Hard)(中途相遇法/启发式分裂)
A Story of One Country (Hard) https://www.luogu.com.cn/problem/solution/CF1181E2 首先考虑暴力的做法,就是每次排序然后寻 ...
- ARC078F - Mole and Abandoned Mine(状压DP)
ARC078F - Mole and Abandoned Mine Solution 状压dpdpdp. 首先去掉边最小=选取边最大,答案为所有边权和减掉选取边权和,于是我们想要最大化选取的边权和. ...
- Defend Your Country
Defend Your Country 题意: n个点,m条边的简单无向连通图,每个点一个权值ai,一个连通块的贡献:(−1)块内点数∗∑ai[点i在该连通块内](-1)^{块内点数}*\sum a_ ...
最新文章
- oracle修改备库状态,Oracle-CRSCTL命令显示备库状态不正确
- 【STM32】位操作、按位与、按位或、按位异或、取反、左移、右移等基础 C 语言知识补充
- TypeScript里的interface扩展,多继承以及对应的JavaScript代码
- 蒙昧的意思_蒙昧的意思
- numpy 最大值_使用 NumPy 让你的 Python 科学计算更高效
- Openfiler + Redhat 5.4 Oracle 11gR2 RAC 安装文档
- python自动给头像添加圣诞帽
- js如何监听元素事件是否被移除_javascript – 如果一个DOM元素被删除,它的监听器也从内存中删除?...
- 20165218 《网络对抗技术》Exp0 Kali安装 Week1
- golang调用c++的dll库文件
- oracle负数,oracle取交集_oracle取负数_oracle case when
- matlab仿真怎么添加干扰信号,噪声干扰信号的matlab仿真
- 计算机丢失msvcr100.dll解决办法,如何解决Msvcr100.dll丢失问题?两种方法可以解决...
- 网站被挂马的处理办法以及预防措施
- openwrt 3G上网功能配置(联通版本)
- GI 中新的基础架构 --MDNS, gipc 和 gpnp 是如何协同工作的
- 学习笔记,什么是双活
- 自定义导航栏 UINavigationBar
- 03_心理咨询_微信小程序项目实战_首页静态效果实现
- java动态性,java动态性
热门文章
- 2022-2027年中国气体绝缘金属封闭开关设备行业发展前景及投资战略咨询报告
- Opencv简单介绍
- 明日之后辐射高校145层怎么过 辐射高校145层攻略
- vscode 保存自动格式化代码
- 软件质量之道:SourceMonitor
- HTML知识点总结还涉及一些subline的基本操作(有很多不足,现为初学者,以后补充)
- 让人和猪都很感动的广告
- 深入浅出 Gradle Sync 优化
- python定义一个学生类姓名、年龄、性别、成绩、你成绩评价_.定义一个学生类Student,包含三个属性姓名、年龄、性别,创建三个学生对象存入ArrayList集合中。...
- 安卓wifi调试助手(单片机wifi上位机)