hdu 4750 Count The Pairs(并查集)
题目链接: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(并查集)的更多相关文章
- hdu 4750 Count The Pairs(并查集+二分)
Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
Count The Pairs Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- HDU 4750 Count The Pairs ★(图+并查集+树状数组)
题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...
- 2013南京网赛1003 hdu 4750 Count The Pairs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...
- HDU 4750 Count The Pairs (离线并查集)
按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...
- [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 ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
随机推荐
- SQL分页语句总结
今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了. 使用top选项 selecttop10*from Orders a where a.orderid notin(s ...
- LIB库加载方法-引用百度百科
LIB库加载方法,有三种,如下: 1.LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中\"Add Files to Project\ ...
- Java文件File操作一:文件的创建和删除
一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...
- list集合中指定字段去重
在开发中,有时会需要指定字段去重,以下为实现方法: 假设有个房地产权的类,其中宗地代码ZDDM值重复,而我们在前端页面显示时,只需要一条数据,因为公共字段都一样: IEqualityComparer需 ...
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)解决方法
登陆mysql的时候,出现了这个问题: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' ( ...
- windows8一直更新不了的问题————解决方案
以下是微软官方工程师的详细解答: 尊敬的佐先生: 您好! 感谢您联系微软技术支持!我是微软技术支持工程师,我姓张.我将协助您解决有关问题.您的问题编号是SRX 1274238225 对于您当前的更新问 ...
- 重要业务MySQL冷备解决方案
1.概述 在公司业务里面,当对应的业务数据不是很重要的时候,我们一般会简单的写个脚本,每天半夜把数据库数据全量拉取下来,备份到本地磁盘.但当业务比较重要的时候,这样简单操作会存在许多问题,比如本地磁盘 ...
- [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD
一.回归函数及目标函数 以均方误差作为目标函数(损失函数),目的是使其值最小化,用于优化上式. 二.优化方式(Gradient Descent) 1.最速梯度下降法 也叫批量梯度下降法Batch Gr ...
- js 中的流程控制—while和do while
while语句: while(exp){ }如果为true ,执行代码块里的语句,如果为false,跳出循环 <script> var i =1 ; while (i<10){ / ...
- php 获取客户端IP地址
/** * 获取真实IP地址 */ /* 在PHP中getenv(参数)函数是一个用于获取环境变量的函数,根据提供不同的参数可以获取不同的环境变量, getenv("REMOTE_ADDR& ...