qaq,现在内心真是各种草泥马。怪自己见识短浅。。。哎。。。

题意:

给你一幅图,然后求一个最小花费使得所有的点都连通(这就是最小生成树啊),然后在这棵树上【如果我要从任意起点到任意终点,这两个点不同,且这两个点的被选取概率都是一样,求一个最小的期望长度,我的神队友的解释就是树上所有任意不同点之间的边值都加起来然后除以边的数量】

思路:

①:最小生成树;

②:求一个所有边之和/边的数量,一个边的贡献,对总和的贡献,就是任意两点之和,这个边被加了多少次,那就用这个边把树分成两部分,一部分有a个点,一部分有b个。那这个边就被加了a*b个点,那这个边就被加了a*b个次。

= =、以上纯属YY,路人勿喷。

利用树的构造,查找儿子节点个数。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const double eps=1e-6;
const double pi=acos(-1.0);
const int mod=998244353;
const LL INF=0x3f3f3f3f;
const int N=1e5+10;
struct asd{
int to;
int next;
};
asd q[N];
int head[N],tol;
int sz[N]; void add(int a,int b)
{
q[tol].to=b;
q[tol].next=head[a];
head[a]=tol++;
} void dfs(int root,int father)
{
sz[root]=1;
for(int i=head[root];i!=-1;i=q[i].next)
{
int son=q[i].to;
if(son==father) continue;
dfs(son,root);
sz[root]+=sz[son];
}
} int main()
{
int n,m;
while(1){
cin>>n>>m;
tol=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
dfs(1,-1);
for(int i=2;i<=n;i++){
printf("第%d个点:\n",i);
printf("左:%d 右:%d\n",sz[i],n-sz[i]);
}
}
}



qaq , 实在看不懂就只能调试知道算法…然后就会知道这个算法就是找该节点的儿子数目(包括他本身)。

然后边的权值就是节点到前面那个父亲节点的距离。找儿子这波操作真是血亏。。。智障题。。

比赛的时候数组还开小了…T了N发还以为不行。。。虽然后来证明是不行。。。

瞎扔一份代码跑….

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const double eps=1e-6;
const double pi=acos(-1.0);
const int mod=998244353;
const LL INF=0x3f3f3f3f;
const int N=1e5+10;
struct asd{
int x,y;
LL num;
};
struct ad{
LL w;
int to;
int next;
};
ad ma[N*20];
int tol,head[N*20]; asd qq[N*10];
asd q[N*10];
int pre[N];
int vis[N];
int n;
LL val[N];
int sb[N]; bool cmp(asd z,asd x)
{
if(z.num<x.num)
return 1;
return 0;
}
void Init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int Find(int x)
{
int r=x;
while(r!=pre[r])
{
r=pre[r];
}
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void add(int a,int b,LL c)
{
ma[tol].w=c;
ma[tol].to=b;
ma[tol].next=head[a];
head[a]=tol++;
}
void dfs(int u,int v)
{
sb[u]=1;
for(int i=head[u];i!=-1;i=ma[i].next){
int tt=ma[i].to;
if(tt==v) continue;
val[tt]=ma[i].w;
dfs(tt,u);
sb[u]+=sb[tt];
}
}
int main()
{
int t;
cin>>t;
while(t--){
int m,a,b;
LL c;
scanf("%d%d",&n,&m);
LL temp;
temp=(LL)n*(n-1)*0.5;
Init();
for(int i=0;i<m;i++)
{
scanf("%d%d%lld",&a,&b,&c);
q[i].x=a;
q[i].y=b;
q[i].num=c;
}
sort(q,q+m,cmp);
LL ans=0;
tol=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
int aa=Find(q[i].x);
int bb=Find(q[i].y);
if(aa!=bb)
{
add(q[i].x,q[i].y,q[i].num);
add(q[i].y,q[i].x,q[i].num);
pre[aa]=bb;
ans+=q[i].num;
}
}
dfs(1,0);
LL sum=0;
for(int i=2;i<=n;i++){
sum+=val[i]*sb[i]*(n-sb[i]);
}
if(n==1)
printf("%lld 0.00\n",ans);
else
printf("%lld %.2lf\n",ans,(double)sum/(double)temp);
}
return 0;
}
/* 100
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
5 4
1 2 1
3 4 2
2 3 3
2 5 4
6 5
1 6 4
1 2 3
2 5 2
2 3 1
3 4 2
*/

2016 Multi-University Training Contest 1Abandoned country的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  5. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2016 Al-Baath University Training Camp Contest-1 G

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

  8. 2016 Al-Baath University Training Camp Contest-1 F

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

  9. 2016 Al-Baath University Training Camp Contest-1 D

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

随机推荐

  1. 使用Python写的第一个网络爬虫程序

    今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中. 此代码中主要使用到了python的以下几个功能,因为对python不 ...

  2. PS 如何使用液化工具给人物减肥

    进入"液化", 有个收缩按钮, 可以选择范围大小, 想瘦哪里, 瘦多少都OK   最终效果图     1.打开原图,进入通道面板,选择菜单图像计算,计算红色通道,保留人物见图.   ...

  3. linux nc,nmap,telnet ,natstat命令

    说明 在服务器运维中 通常需要知道 机器端口状态 是否开启 是否被防火墙拦截等.今天我们介绍这三个命令 用来检测端口. nc 命令 / TCP # 安装 yum install -y nc nc 命令 ...

  4. struts开发&lt;在eclipse中配置struts. 一&gt;

    1.获取struts的jar包 1.1首先在http://struts.apache.org/download.cgi#struts23163这里下载 struts的文件包(选择struts-2.3. ...

  5. poj 2154 Color 欧拉函数优化的ploya计数

    枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...

  6. iOS 把数据库文件打包到mainbundle中,查找不到路径的解决的方法;以及在删除bundle中文件的可行性

    在开发中有时我们须要把数据库文件打包到我们的项目中.一般我们都是在外部用工具生成数据库文件,然后拉入项目中.可是我们在程序中查找改文件时.返回的路径总是nil 解决的方法: 原因我们拉入其它资源文件( ...

  7. HDU 5344 MZL's xor (多校)[补7月28]

    MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  8. 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划

    淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划 SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库O ...

  9. OpenStack源码系列---起始篇

    近一年来我负责公司云点的自动化部署工作,包括公司自有云平台方案.XenServer.vSphere.Ovirt和OpenStack的自动化安装部署,目前已经到了OpenStack这一部分.自动化部署首 ...

  10. C++函数模板例子

    //C++函数模板实例 #include <iostream>template <class Any>void Swap(Any &a, Any &b); in ...