HDU 4750 Count The Pairs(并查集)
没有发现那个点,无奈。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define LL __int64
int o[],rank[];
struct node
{
int u,v,w;
} edge[];
int n,m;
int que[];
LL ans[];
bool cmp(node a,node b)
{
return a.w < b.w;
}
int find(int x)
{
int r,t;
r = x;
while(x != o[x])
x = o[x];
while(r != x)
{
t = o[r];
o[r] = x;
r = t;
}
return x;
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
{
o[x] = y;
rank[y] += rank[x];
}
}
int bin(int x)
{
int str,end,mid;
str = ;
end = m-;
if(x > que[end])
return m;
while(str < end)
{
mid = (str+end)/;
if(que[mid] < x)
str = mid + ;
else
end = mid;
}
return str;
}
int main()
{
int i,u,v,t;
LL sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i = ;i < n;i ++)
{
rank[i] = ;
o[i] = i;
}
for(i = ; i < m; i ++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
que[i] = edge[i].w;
ans[i] = ;
}
sort(que,que+m);
sort(edge,edge+m,cmp);
sum = ;
for(i = ; i < m; i ++)
{
u = find(edge[i].u);
v = find(edge[i].v);
if(u != v)
{
ans[i] = (LL)rank[u] * rank[v]*;
sum += (LL)rank[u] * rank[v]*;
merge(u,v);
}
}
for(i = ;i < m;i ++)
ans[i] = ans[i] + ans[i-];
scanf("%d",&t);
for(i = ; i < t; i ++)
{
scanf("%d",&u);
v = bin(u);
if(v == )
printf("%I64d\n",sum);
else
printf("%I64d\n",sum-ans[v-]);
}
}
return ;
}
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(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 代码: #include<cstdio> #include<cstring&g ...
- 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, ...
随机推荐
- Python win32api提取exe图标icon
转载地址: http://blog.csdn.net/gumanren/article/details/6129416 代码如下: # -*- coding: utf-8 -*- import sys ...
- 发现一php木马代码
<?php ;//无需验证密码! $shellname='hello~地球~猴子星球欢迎你 '; define('myaddress',__FILE__); error_reporting(E_ ...
- uploadify文件批量上传
uploadify能够时间文件的批量上传,JS文件包下载地址,使用说明可以参考官网文档(http://www.uploadify.com/documentation/) 使用方法如下代码: $(&qu ...
- xml解析方法总结
==========================================xml文件<?xml version=”1.0″ encoding=”GB2312″?> <RES ...
- C++ 基础 const放在函数末尾的意思
- 计算第K个素数
暂时没有时间整理,先放在这里: http://www.quora.com/Prime-Numbers/What-are-good-ways-to-find-nth-prime-number-in-th ...
- Printf()输出格式控制(转)
int printf(const char *format,[argument]); format 参数输出的格式,定义格式为: %[flags][width][.perc] [F|N|h|l]typ ...
- Redis持久化实践及灾难恢复模拟
参考资料: Redis Persistence http://redis.io/topics/persistence Google Groups https://groups.google.com/f ...
- 【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
controller作为后台与前台的ajax进行交互,后台的方法处理完成返回一个boolean类型的值,想传给前台用来判断是否执行成功,BUT,问题来了: 严重: Servlet.service() ...
- bat
1.输出系统时间,利用系统时间做文件名 @echo offset filename=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%ti ...