题目链接: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. winform中的chat

    百度一下 源代码下载:百度一下

  2. JavaScript Invalid Date Verify

    if ( Object.prototype.toString.call(d) === "[object Date]" ) { // it is a date if ( isNaN( ...

  3. Zend Studio 11.0.2 破解和汉化

    本方法适用于Zend Studio 11.0.2,亲测,其他版本未知. 破解方法:覆盖安装目录 plugins 里同名文件,启动任意输入即可注册. Windows版下载地址:http://downlo ...

  4. markdown2 在win10下无法预览解决方案

    今天升级完Win10发现心爱的markdownPad 2无法预览,显示the view has crashed! 按照官网的Q&A http://markdownpad.com/faq.htm ...

  5. Mysql JDBC Url参数说明useUnicode=true&characterEncoding=UTF-8

    MySQL的 JDBC URL 格式 for  Connector/J 如下例: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [ ...

  6. 获取Ip 的地域等信息接口-实例

    今天项目要用到 查询访问网站用户的IP 然后网上查询了 资料还很多 不过有些已经失效了 在这总结下 腾讯,pconline 的API已经失效 不能使用 淘宝的IP接口地址: http://ip.tao ...

  7. Mysql主从复制,读写分离

    一个简单完整的 Mysql 主从复制,读写分离的示意图. 1. 首先搭建 Mysql 主从架构,实现 将 mater 数据自动复制到 slave MySQL 复制的工作方式很简单,一台服务器作为主机, ...

  8. 【python之旅】python简介和入门

    python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...

  9. Sevlet局部变量初始化

    //java 代码部分package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java. ...

  10. 将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)

    安装官方的小度WiFi的驱动器,只能让它当做无线信号的发射装置,但是我想通过小度WiFi让我的台式电脑能都接收无线信号,于是经过一番折腾终于成功了.我的是win7. 小度WiFi无法接受无线信号,不能 ...