Count The Pairs
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的更多相关文章
- HDOJ 4750 Count The Pairs
按边长从小到大排序...再逐个加入(就像MST一样)最先联通的点之间最长路径中的最小值就是新加入的边的长.... Count The Pairs Time Limit: 20000/10000 MS ...
- 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 ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- HDU-4750 Count The Pairs 最小生成树,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:Q个询问t,求在一个无向图上有多少对点(i,j)满足 i 到 j 的所有路径上的最长边的最 ...
- [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(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 代码: #include<cstdio> #include<cstring&g ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- 2013南京网赛1003 hdu 4750 Count The Pairs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...
随机推荐
- DTrace Probes in HotSpot VM----java
http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html http://docs.oracle.com/javase/7 ...
- 安装Oracle数据库和PLSQL连接数据库
首先在Oracle官网上下载: 安装前要注意:将win64_11gR2_database_2of2中的\win64_11gR2_database_2of2\database\stage\Compone ...
- 一次向svn中增加所有新增文件 svn add all new files
svn st | grep '^\?' | tr '^\?' ' ' | sed 's/[ ]*//' | sed 's/[ ]/\\ /g' | xargs svn add
- centos 安装mysqldb 记录
vim setup_pofix.py #修改mysql_config路径 <pre> ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /us ...
- FreeBSD系统更新与软件安装方法
一.系统更新 freebsd-update fetch freebsd-update install 二.软件源更新(类似yum update.apt-get update) 1.取回源 portsn ...
- jquery easyui easyui-treegrid 使用异步加载数据
jquery easyui easyui-treegrid 使用异步加载数据 jquery easyui easyui-treegrid 异步请求 >>>>>>&g ...
- ueditor 添加微软雅黑字体 异常“从客户端中检测到有潜在危险的 request.form值”,解决
使用ueditor往数据库添加文本内容时,如果字体有css样式, <,>," 这些字符会导致报出异常信息:从客户端中检测到有潜在危险的 request.form值 因为这些字符有 ...
- Promise的用简要使用方式
Promise用法 在项目中用到异步请求ajax,想到用promise来解决,之前用过但是已经很久了,还是忘了一些,重新熟悉了一下整理一份简要文档. Promise,就是一个对象,用来传递异步操作的消 ...
- C#设置与获取目录权限(.net控制ACL)
找到两种方式可以修改文件夹的权限 第一种: 想用c#来设置和读取ntfs分区上的目录权限,找了很多资料,未果.终于发现了一段vb.net的代码,做了修改,以C#展示给大家. using System; ...
- 消息处理之EventBus ——使用篇
以前的几篇文章简单的介绍了一下UI线程和子线程之间的线程通信利器Handler,以及顺便介绍了一下SyncTask和HeadlerThread.这里介绍另一线程通信利器EventBus. EventB ...