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, ...
随机推荐
- Spring中的jar包详解
下面给大家说说spring众多jar包的特点吧,无论对于初学spring的新手,还是spring高手,这篇文章都会给大家带来知识上的收获,如果你已经十分熟悉本文内容就当做一次温故知新吧.spring. ...
- 无题- Anyway,Object-C
Json String Body see here: working-with-json-in-ios-5also see here: serialize-custom-object-to-json- ...
- 关于phpcms v9投票模块选项排序listorder设定问题
关于phpcms v9投票模块选项排序listorder设定问题修改,主要修改了三个文件三处地方. 主要修改三个文件: .phpcms\modules\vote\templates\vote_edit ...
- 使用html5的离线缓存技术
突然想用html5的离线缓存,但是一直没有成功,在各种群里问发现很多人都没什么经验,最终终于在各种论坛找到解决方案了.下面就简单记录一下相关情况. 一.离线缓存的优点 我们都知道离线缓存主要是用来减少 ...
- hdu 4741 2013杭州赛区网络赛 dfs ***
起点忘记录了,一直wa 代码写的很整齐,看着很爽 #include<cstdio> #include<iostream> #include<algorithm> # ...
- excel、csv、txt文件数据读取
/// <summary> /// 读取Excel表每一行第一列的字符串集合 /// </summary> /// <param name="filePath& ...
- commonlisp教程以及学习笔记-01
手上有两个教程,但是感觉这个教程可能更适合自己
- Arduino101学习笔记(十二)—— 101定时器中断
一.API 1.开定时器中断 //*********************************************************************************** ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- vim 清空
插入模式 首先执行gg 跳至文件首行 然后执行dG就清空了整个文件 还有一种方法就要退出VIM,然后使用echo > file ,这样也能快速清空文件内容