HDU-4750 Count The Pairs 最小生成树,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750
题意:Q个询问t,求在一个无向图上有多少对点(i,j)满足 i 到 j 的所有路径上的最长边的最小值大于等于t。
(i,j)所有路径上的最长边的最小值,容易想到就是 i, j之间的瓶颈路,瓶颈路也就是最小生成树上的边了。注意到每条边的权值都是不相等的,那么MST就是确定的。假设当前MST的边的权值是f[i],Kruskal的并查集中维护一个cnt[i],表示以节点 i 为根的集合的节点个数,那么两个集合x和y合并,点对数就增加cnt[fa[x]]*cnt[fa[y]]*2,sum为点对数的累计和。那么询问t小于等于下一个MST中的边f[i+1]的值就是n*(n-1)*2-sum,把询问排序就可以了。。
//STATUS:C++_AC_2593MS_7388KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,w;
bool operator < (const Edge& a)const {
return w<a.w;
}
}e[N*];
struct Qu{
int t,id;
bool operator < (const Qu& a)const {
return t<a.t;
}
}q[N]; int fa[N],cnt[N],ans[N];
int n,m,Q; int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} int main(){
// freopen("in.txt","r",stdin);
int i,j,k,x,y,t;
while(~scanf("%d%d",&n,&m))
{
for(i=;i<m;i++){
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
scanf("%d",&Q);
for(i=;i<Q;i++){
scanf("%d",&q[i].t);
q[i].id=i;
} sort(e,e+m);
sort(q,q+Q);
for(i=;i<n;i++)fa[i]=i,cnt[i]=;
for(i=t=j=,k=;i<m && k<n;i++){
x=find(e[i].u),y=find(e[i].v);
if(x==y)continue;
for(;j<Q && q[j].t<=e[i].w;j++)ans[q[j].id]=n*(n-)-t;
t+=cnt[x]*cnt[y]*;
fa[x]=y;
cnt[y]+=cnt[x];
k++;
}
for(;j<Q;j++)ans[q[j].id]=n*(n-)-t; for(i=;i<Q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
HDU-4750 Count The Pairs 最小生成树,并查集的更多相关文章
- HDU 4750 Count The Pairs ★(图+并查集+树状数组)
题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...
- HDU 4750 Count The Pairs (离线并查集)
按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...
- 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(并查集+二分)
Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...
- 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(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 代码: #include<cstdio> #include<cstring&g ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- [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]肯定唯一了. 拿 ...
随机推荐
- 转:socket编程在windows和linux下的区别
如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境. 下面大概分几个方面进行罗 ...
- php整理(三): 面向对象
PHP学习(三)----面向对象 首先,还是建立一个好的理解模型: 1.什么是面向对象? 面向对象分为两个部分,那就是:什么是对象和什么是面向? 什么是对象: 对象的出现就是为了用代码更好的绘制我 ...
- 1287. Mars Canals(DP)
1287 水DP #include <iostream> #include<cstdio> #include<cstring> #include<algori ...
- 为什么多数游戏服务端是用 C++ 来写
早年开发游戏必须用C++,这没得说,2000-2004年,java还没有nio,其他动态语言不抗重负,只能C/C++能开发出完整可用的游戏服务端.直到2005年,韩国的游戏很多都还是纯C++写服务端, ...
- Linux Watchdog Test Program
/*********************************************************************** * Linux Watchdog Test Progr ...
- liunx下mysql数据库使用之三范式,关系模型设计注意项,安装目录结构
数据库的三范式第一范式===>每行记录的属性,是原子的,拆到不可拆为止.===>例如:一个人的籍贯,可以拆分为,省,市,县,乡,村 第二范式===>每行记录的非主属性(非主键属性), ...
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
转:http://boke.25k5.com/kan141919.html 通过前面几篇,我们已经完成了内容类型,列表定义,列表实例g 8h"@的开发.本篇继续讲解列表中的一个重要环节- ...
- linux 下 NetBeans 字体大小设置
在linux mint 12下安装了 NetBeans7.1.2使用之后,觉得字体不好看,字体普遍特别大,分三个方面改NetBeans的字体. 1. 代码字体大小 点击NetBeans菜单,工具--& ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- memcache分布式部署的原理分析
下面本文章来给各位同学介绍memcache分布式部署的原理分析,希望此文章对你理解memcache分布式部署会有所帮助哦. 今天在封装memcache操作类库过程中,意识到一直以来对memcach ...