hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3006 Accepted Submission(s): 346
For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e6+10;
int num[N],f[N];
vector<int> G[N]; struct edge{
int u,v,cost,flag;
}e[N]; bool cmp(edge a,edge b)
{
return a.cost<b.cost;
} int findr(int u)
{
if(f[u]!=u)
f[u]=findr(f[u]);
return f[u];
} int dfs_clock; void dfs(int u,int pre)
{
int p=++dfs_clock;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==pre) continue;
dfs(v,u);
}
num[u]=dfs_clock-p+1;
} int main()
{
int cas,n,m;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);
e[i].flag=0;
}
sort(e+1,e+m+1,cmp);
for(int i=1;i<=n;i++) f[i]=i;
ll cost=0;
for(int i=1;i<=m;i++)
{
int u=findr(e[i].u),v=findr(e[i].v);
if(u==v) continue;
f[u]=v;
e[i].flag=1;
cost+=e[i].cost;
G[e[i].u].push_back(e[i].v);
G[e[i].v].push_back(e[i].u);
} dfs_clock=0;
dfs(1,-1);
double q=0;
for(int i=1;i<=m;i++)
if(e[i].flag)
{
int u=e[i].u,v=e[i].v;
ll k=min(num[u],num[v]);
q+=k*(n-k)*e[i].cost;
} q=2*q/n/(n-1);
printf("%lld %.2f\n",cost,q);
}
return 0;
}
分析:刚开始以为是道kruskal+统计子节点的水题,,,,后来写了下,,发现这道题目有个很迷的地方,就是连接起所有村庄的最小的总cost和最小的期望值,最小的cost当然跑kruskal,那会不会在同一种
cost的情况下出现不同期望值?那该怎么选择,,,于是瞬间懵逼,,就放弃了。。。
可见,,比赛时缺乏基本的随机应变和见招拆招的能力,,这要靠比赛来加强了。。。。比赛时胆子真的太小了,,,只有多多比赛慢慢克服了。。。
解决:其实因为题目说了所有边的权值均不同,,kruskal中对边sort后,形成最小生成树的边的选取方案是
唯一的,所以最小生成树唯一;
hdu 5723 Abandoned country 最小生成树+子节点统计的更多相关文章
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
随机推荐
- Python面试常考点之深入浅出链表操作
Python面试常考点之深入浅出链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对 ...
- spring boot 学习笔记(三)之 配置
一:概述 在Spring boot 中根据业务需求,我们往往会在不同地方配置我们所需的key-value 配置项,配置文件存在不同的地方的场景如下: (1) 默认存在 application.prop ...
- unity 打包Error:WebException: The remote server returned an error: (403) Forbidden.
記一下在ios上打包出錯: UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlaye ...
- shell、bash、terminal和kernel之间的关系
shell.bash.terminal和kernel之间的关系 本文是<鸟哥的Linux私房菜>的学习笔记 什么是kernel kernel的中文是"操作系统核心",主 ...
- O057、Delete Volume 操作
参考https://www.cnblogs.com/CloudMan6/p/5648665.html 状态为Available 的volume 才能够被delete,如果volume当前已经被at ...
- O031、Start Instance 操作详解
参考https://www.cnblogs.com/CloudMan6/p/5470723.html 本节将通过日志文件分析 instance start 的操作过程,下面是 start inst ...
- python检测域名
pip install python-whois import whois print(whois.whois('baidu.com')) #输出有关baidu.com的所有域名
- python与pip
python , pip 相关命令汇总 1) 在python3 下升级pip3 pip3 install --upgrade pip
- element-ui 中 el-table 根据scope.row行数据变化动态显示行内控件
加入本行的数据为scope.row,其数据格式为 { "propertyId": 4, "propertyName": "标题", &quo ...
- 【Swift后台】目录
背景介绍 环境安装