http://acm.hdu.edu.cn/showproblem.php?pid=5441

无向图,n个点,m条带权边,q次询问,给出数值d,点a可以到点b当且仅当存在一条路线其中最大的权值不超过d。

用sum记录连通块节点个数,两个连通块合并,结果增加( sum[a] + sum[b] ) * ( sum[a] + sum[b] - 1 ) - sum[a] * ( sum[a] - 1 ) - sum[b] * ( sum[b] -1 )

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct node
{
int a;
int b;
int c;
};
int father[N];
node p[N];
int num[N];
ll sum[N];//表示该点所在连通块的节点个数
int r[N];
ll ans[N];
int finds(int x)
{
if(father[x]!=x)
father[x]=finds(father[x]);
return father[x];
}
bool cmp(node x,node y)
{
return x.c<y.c;
}
void connect(int a,int b)
{
if(r[a]>r[b])
father[b]=a;
else if(r[a]<r[b])
father[a]=b;
else
{
father[a]=b;
r[b]++;
}
sum[a]=sum[b]=sum[a]+sum[b];
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)
{
sum[i]=1;
father[i]=i;
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].c);
}
sort(p+1,p+m+1,cmp);//首先按权值进行排序
for(int i=1;i<=m;i++)
{
int a=finds(p[i].a);
int b=finds(p[i].b);
if(a!=b)
{
ans[i]=ans[i-1]+(sum[a]+sum[b])*(sum[a]+sum[b]-1)-sum[a]*(sum[a]-1)-sum[b]*(sum[b]-1);//连通块合并结果增加
connect(a,b);
}
else
ans[i]=ans[i-1];
num[i]=p[i].c;
}
while(q--)
{
int x;
scanf("%d",&x);
int t=upper_bound(num+1,num+m+1,x)-num-1;
printf("%lld\n",ans[t]);
}
}
return 0;
}

HDU5441 Travel 并查集的更多相关文章

  1. Travel 并查集

    题意:给一个图,若干询问,每次询问只经过边权<=w的边,x能到达的点数 并查集啊,对询问和边排序,直接合并,维护size,查询 #include<cstdio> #include&l ...

  2. bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra

    Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...

  3. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  4. HDU5441 Travel 离线并查集

    Travel Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, ...

  5. 【BZOJ-1576】安全路径Travel Dijkstra + 并查集

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1044  Solved: 363[Sub ...

  6. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  7. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  8. Travel(HDU 5441 2015长春区域赛 带权并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  9. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集

    [BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...

随机推荐

  1. IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]

    CSS 2.1:   IE 5.0 IE 5.5 IE 6.0 IE 7.0 IE8 Beta 1 IE8 Beta 2 IE 8.0 @charset No Yes Yes Yes Yes Yes ...

  2. SDWebImage源码刨根问底

    前言: SDWebImage是iOS中一款处理图片的框架, 使用它提供的方法, 一句话就能让UIImageView,自动去加载并显示网络图片,将图片缓存到内存或磁盘缓存,正好有阅读开源项目的计划,于是 ...

  3. linux查看是否已安装GCC及安装GCC

    输入:gcc -v;如果提示未找到命令即表示没有安装 使用:yum install gcc即可

  4. Sql server 日期函数和日期转换

    时间函数 SQL Server Date 函数 下面的表格列出了 SQL Server 中最重要的内建日期函数: 函数 描述 GETDATE() 返回当前日期和时间 DATEPART(Type,dat ...

  5. AP聚类算法(Affinity propagation Clustering Algorithm )

    AP聚类算法是基于数据点间的"信息传递"的一种聚类算法.与k-均值算法或k中心点算法不同,AP算法不需要在运行算法之前确定聚类的个数.AP算法寻找的"examplars& ...

  6. A-Making the Grade(POJ 3666)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4656   Accepted: 2206 ...

  7. fill_parent和wrap_content的区别

    在Android布局文件中定义视图垂直或水平大小: android:layout_width和android_layout_height的属性有fill_parent.wrap_content和mat ...

  8. ES mlockall作用——preventing that memory from being paged to the swap area

    elasticsearch还有一个重要的参数bootstrap.mlockall,这个参数的目的是当你无法关闭系统的swap的时候,建议把这个参数设为true.防止在内存不够用的时候,elastics ...

  9. linux系统字符集

    Linux中中文乱码问题通常是由于字符集与windows不兼容所引起,windows的中文字符集是双字节的GBK编码linux采用的是3字节的utf-8编码,所以在windows下用工具连接linux ...

  10. 第二章 XHTML基础

    1.一个网页,也就是一个XHTML文档,是由元素组成.元素定义了文本和图形在XHTML文档中的结构.XHTML文档的扩展名通常是.html或者htm. 2.XHTML元素使用XHTML标记定义,每个标 ...