hdu4750:http://acm.hdu.edu.cn/showproblem.php?pid=4750

题意:给你一个带权无向图,然后让你求这样的点对s,t,使得s--t的所有路径上的最大的边的最小值>=d,输出这样的点数有多少条。

题解:这一题的解法实在是太妙了。利用克努斯卡尔的生成树的额思想,先对所有的边排序,然后不断的加边,加边的同时处理出相应的方案数,把所有额情况都处理出来,查询的时候只要O(1)的输出就可以了。具体的看代码。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e5+;
const int M=5e5+;
int n,m,q,u,v,top,temp;
int fa[N],sum[N],ans[N];
struct Node{
int u,v;
int w;
bool operator<(const Node a)const {
if(w!=a.w)
return w<a.w;
else
return u<a.u;
}
}edge[M];
void init(){
for(int i=;i<=n;i++){
fa[i]=i;
sum[i]=;
}
top=;
memset(ans,,sizeof(ans));
}
int Find(int x){
int s;
for(s=x;s!=fa[s];s=fa[s]);
while(s!=x){
int temp=fa[x];
fa[x]=s;
x=temp;
}
return s;
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<=m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
edge[i].v++;edge[i].u++;
}
sort(edge+,edge+m+);
vector<int>Q;
for(int i=;i<=m;i++){
int x=Find(edge[i].u);
int y=Find(edge[i].v);
if(x!=y){
ans[++top]=sum[x]*sum[y]*;
fa[x]=y;
sum[y]+=sum[x];
Q.push_back(edge[i].w);
}
}
for(int i=;i<=top;i++)
ans[i]+=ans[i-];
scanf("%d",&q);
for(int i=;i<=q;i++){
scanf("%d",&temp);
int id=lower_bound(Q.begin(),Q.end(),temp)-Q.begin();
printf("%d\n",ans[top]-ans[id]);
}
}
}

Count The Pairs的更多相关文章

  1. HDOJ 4750 Count The Pairs

    按边长从小到大排序...再逐个加入(就像MST一样)最先联通的点之间最长路径中的最小值就是新加入的边的长.... Count The Pairs Time Limit: 20000/10000 MS ...

  2. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  3. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  4. HDU 4750 Count The Pairs(并查集)

    题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  5. HDU-4750 Count The Pairs 最小生成树,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:Q个询问t,求在一个无向图上有多少对点(i,j)满足 i 到 j 的所有路径上的最长边的最 ...

  6. [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 ...

  7. hdu 4750 Count The Pairs(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 代码: #include<cstdio> #include<cstring&g ...

  8. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

  9. 2013南京网赛1003 hdu 4750 Count The Pairs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...

随机推荐

  1. SOAP消息的传递

    上一篇说了SOAP消息的创建,那么创建好了的SOAP消息要怎么发送给服务端呢? public class SoapTest { private String wsdlUri = "http: ...

  2. linux64下安装swftools

    在文档转换器中,需要在linux上安装swftools,经历了一番曲折过程终于安装成功.swftools安装包从http://www.swftools.org/download.html上面下载. 在 ...

  3. css:nth-of-type()选择器用法

    今天做一个页面,无意中看到这个nth-of-type感觉挺方便的,之前单双行有的有横线,有的无横线一般在html中单独再写border-right:none等之类的.现在发现这个好东西赶紧记录下来. ...

  4. ASP.NET 相关小知识

    后台修改前台html控件属性 添加 runat=server ,后台获取// 客户端隐藏 a.Attributes[ "style "] = "display:none ...

  5. 一个library,相当于一个rootfolder

    picLib.RootFolder.SubFolders 操作library的方式:                         SPList oList = web.Lists[ListName ...

  6. Play - js/css concatenation & minify

    1. Css We’ll use LESS CSS, all less sources are defined in the app/assets, and they will be compiled ...

  7. linux修改时区,时间格式

    修改为上海的时区: 查看当前时区 date cp -vf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime vim /etc/sysconfig/cl ...

  8. WebService学习笔记

    WebService有什么用? 入门之前先简单介绍下WCF.在用WebService做开发时,很多人都不知道WCF和WebService之间的关系.实际上WCF包含了WebService,这是一个很强 ...

  9. winfrom面向对象1

    1:面向对象的技术概论 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 对象的定义是人们要进行研究的任何事物,从最简单的整数到复杂的飞机 ...

  10. ie8中parseInt字符型数值转换数值型问题

    今天在ie8中测试项目发现一个奇怪的问题,"08" "09" 强转竟然变成了: 后来发现ie8把"08" "09" 默认 ...