n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)
先把边的权值 从小到大排序 询问值也按从小到大排序
num记录集合里元素的个数
每合并两个集合 ans增加 2*num[u]*num[v]

Sample Input
1
5 5 3 //n w q
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000

Sample Output
2
6
12

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int MAXN = ; int F[MAXN];
int num[MAXN] ;
int save[MAXN] ;
int n , m , q; struct Edge
{
int u ;
int v ;
int w ;
}e[MAXN * ]; bool cmp1(Edge x , Edge y)
{
return x.w < y.w ;
} struct Que
{
int id ;
int x ;
}a[MAXN]; bool cmp2(Que a , Que b)
{
return a.x < b.x ;
} int find(int x)
{
if(F[x]==x) return x;
return F[x]=find(F[x]);
}
void bing(int u,int v)
{
int x = find(u);
int y = find(v);
if ( y < x )
swap ( x , y );
F[y] = x;
num[x] += num[y];
} int main ()
{
//freopen("in.txt","r",stdin) ;
int T ;
scanf("%d" , &T) ;
while(T--)
{
int ans = ;
int j = ;
int i ;
scanf("%d%d%d" , &n , &m , &q) ;
for(i = ; i <= n ; i++)
{
F[i] = i ;
num[i] = ;
}
for(i = ; i < m ; i++)
scanf("%d%d%d" , &e[i].u , &e[i].v , &e[i].w) ;
sort(e , e+m , cmp1) ;
for(i = ; i < q ; i++)
{
scanf("%d" , &a[i].x) ;
a[i].id = i ;
}
sort(a,a+q,cmp2) ;
for(i = ; i < q ; i++)
{
while(j < m && e[j].w <= a[i].x)
{
int u = find(e[j].u);
int v = find(e[j].v);
j++ ;
if (u == v)
continue ;
ans += *num[u]*num[v] ;
bing(u,v) ;
}
save[a[i].id] = ans;
}
for(i = ; i < q ; i++)
printf("%d\n" , save[i]) ; } return ;
}

hdu 5441 (2015长春网络赛E题 带权并查集 )的更多相关文章

  1. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

    题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余 ...

  3. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  4. poj1182 and 携程预赛2第一题 带权并查集

    题意:       动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.  现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...

  5. hdu 5442 (ACM-ICPC2015长春网络赛F题)

    题意:给出一个字符串,长度是2*10^4.将它首尾相接形成环,并在环上找一个起始点顺时针或逆时针走一圈,求字典序最大的走法,如果有多个答案则找起始点最小的,若起始点也相同则选择顺时针. 分析:后缀数组 ...

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

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

  7. hdu 5443 (2015长春网赛G题 求区间最值)

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

  8. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 3038 How Many Answers Are Wrong(带权并查集)

    传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...

随机推荐

  1. jenkins迁移升级简述

    1.迁移背景 原有jenkins版本为2.32.2,由于需要安装git parameter插件,插件安装失败,依赖暂时无法解决,因此决定升级jenkins版本,升级版本为2.121.2.   2.je ...

  2. BZOJ2655 calc(动态规划+拉格朗日插值法)

    考虑暴力dp:f[i][j]表示i个数值域1~j时的答案.考虑使其值域++,则有f[i][j]=f[i][j-1]+f[i-1][j-1]*i*j,边界f[i][i]=i!*i!. 注意到值域很大,考 ...

  3. 无旋treap板子!

    #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...

  4. 【转】I²C总线上拉电阻阻值如何选择?

    I2C总线为何需要上拉电阻? I2C(Inter-Intergrated Circuit)总线是微电子通信控制领域中常用的一种总线标准,具有接线少,控制方式简单,通信速率高等优点. I2C总线的内部结 ...

  5. 【HDU5687】Trie

    题目大意:需要维护一个支持以下操作的数据结构:(1)支持插入一个字符串(2)支持删除所有前缀等于给定字符串的单词(3)查询该数据结构中是否存在一个以给定字符串为前缀的字符串 题解:由题目可知,需要维护 ...

  6. python+正态分布+蒙特卡洛预测男女身高概率!

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  7. oracle按照in的顺序进行排序

    oracle按照in的顺序进行排序 ,,) order by case id end;

  8. 第一节 Spring的环境搭建

     正在构建,扫一扫,敬请期待 和玩得来的人在一起玩才叫玩! 和玩不来的人在一起玩,那种感觉就像加班啊! 关注胖个人微信公众账号,希望对各位学生有所帮助! --胖先生 Spring框架,什么是Sprin ...

  9. 一些常见修改命令(针对ubuntu 14.04 持续更新中...)

    1.PS1 在哪: echo $PS1    vi /etc/bash.bashrc      /W输出最后一个目录    /w输出完整目录 2.设置静态IP地址:vim /etc/network/i ...

  10. bzoj千题计划202:bzoj3191: [JLOI2013]卡牌游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3191 每个人获胜的概率只与其在排列中与庄家的相对位置有关 dp[i][j] 还剩i个人时,从庄家数第 ...