题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
const int maxm = ; struct Edge
{
int u,v,w;
Edge(int u=,int v=,int w=): u(u), v(v), w(w) {}
bool operator < (const Edge& rhs) const
{
return w < rhs.w;
}
}edges[maxm];
struct Query
{
int id;
int num;
bool operator < (const Query& rhs) const
{
return num < rhs.num;
}
}Q[maxn*]; int pa[maxn];
int counts[maxn];
int ans[maxn*]; int find(int x)
{
return x == pa[x] ? x : pa[x] = find(pa[x]);
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int n,m,p;
while(cin>>n>>m)
{
for(int i=; i<n; i++)
{
pa[i] = i;
counts[i] = ;
}
for(int i=; i<m; i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
edges[i] = Edge(a,b,c);
}
sort(edges,edges+m); scanf("%d",&p);
for(int i=; i<p; i++)
{
scanf("%d",&Q[i].num);
Q[i].id = i;
}
sort(Q,Q+p); int cnt = ;
int sum = ;
int tot = n*(n-); //这么做前提是图是连通的,只能样例给的是连通的 for(int i=; i<p; i++)
{
while(cnt<m && edges[cnt].w < Q[i].num)
{
int u_fa = find(edges[cnt].u);
int v_fa = find(edges[cnt].v);
if(u_fa != v_fa)
{
sum = sum + counts[u_fa]*counts[v_fa]*;
pa[v_fa] = u_fa;
counts[u_fa] += counts[v_fa];
}
cnt++;
} ans[Q[i].id] = tot - sum;
}
for(int i=; i<p; i++)
printf("%d\n",ans[i]);
}
}

hdu 4750 Count The Pairs(并查集)的更多相关文章

  1. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  2. HDU 4750 Count The Pairs(并查集)

    题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  3. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  4. HDU 4750 Count The Pairs ★(图+并查集+树状数组)

    题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...

  5. 2013南京网赛1003 hdu 4750 Count The Pairs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...

  6. HDU 4750 Count The Pairs (离线并查集)

    按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...

  7. [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...

  8. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. Laravel_1 安装

    1>http://www.golaravel.com/post/install-and-run-laravel-5-x-on-windows/ 2>http://www.golaravel ...

  2. obj.onclick=fnClick与obj.onclick=fnClick()的区别

    先说结论:这段代码浏览器会报错,提示 aDiv[this.index] is undefined 所以正确的写法应该是去掉(),直接写为function fnClick.   不加括号的话,相当于指定 ...

  3. 关于Java中的数组转变成字符串问题

    1.用StringBuilder private static String arraytoString(int arr[]){ StringBuilder sb=new StringBuilder( ...

  4. java设计模式和设计原则

    一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  5. 【转】ASP.NET常用数据绑定控件优劣总结

    转自:http://www.cnblogs.com/Olive116/archive/2012/10/24/2736570.html ASP.NET常用数据绑定控件优劣总结   本文的初衷在于对Asp ...

  6. C++判断Office版本——转载

    自:http://blog.csdn.net/lpc_china/article/details/18359145 主要原理:查询windows注册表microsoft office软件项的值来判断版 ...

  7. mysql5.7.14安装与配置

    参考文章链接: http://jingyan.baidu.com/article/afd8f4de9006d934e286e9fd.html http://www.cnblogs.com/wenthi ...

  8. jquery获取css color 值返回RGB

    css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...

  9. mysql备份,还原命令

    mysql导出数据1.导出整个数据库mysqldump -u用户名 -p 数据库名 >备份文件2.导出一个表mysqldump -u用户名 -p databaseName tablename & ...

  10. 关于applicationx/www-form-urlencoded和multipart/form-data的描述

    在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...