并查集+二分-hdu-4750-Count The Pairs
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4750
题目大意:
给一无向图,n个点,m条边,每条边有个长度,且不一样。定义f(i,j)表示从节点i到节点j的所有路径中的最大边权值的最小值。有q个询问,每个询问有个t,求f(i,j)>=t的种数。
解题思路:
并查集+简单dp+二分。
比赛的时候各种TLE和MLE。只是查找方式不对。
队友思路,先按边从小到大排序考虑,对于每条边E该边两个节点为a、b,如果a、b不在同一个联通块,则a联通块中点集A和b联通块中点集B的f值一定为E(因为E升序)。恰好能使其通路。
map[i]表示以权值为i的边作为f值的点对个数。
sum[i]表示以大于等于第i大边权值的权值作为f值得点对总的个数。
对于每一个t,在排序了的sig[i](能取的边权值)中二分找到大于等于它的最小的小标j。输出sum[j]即可。
注意:
求点对个数时要乘以2.
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#define eps 1e-6
#define INF 0x3fffffff
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 11000
#define Maxm 510000
struct Edge
{
int a,b,c;
}edge[Maxm];
map<int,int>myp; short int fa[Maxn],num[Maxn];
int n,m;
int sum[Maxm],sig[Maxm]; bool cmp(struct Edge aa,struct Edge bb)
{
return aa.c<bb.c; //对边排序
} int find(int x) //并查集 路径压缩
{
int tmp=x; while(x!=fa[x])
x=fa[x]; while(fa[tmp]!=x)
{
int tt=fa[tmp];
fa[tmp]=x;
tmp=tt;
}
return x;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
int a,b,c;
myp.clear(); for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[i].a=a,edge[i].b=b,edge[i].c=c;
sig[i]=c;
}
sort(edge+1,edge+m+1,cmp);
for(int i=0;i<=n;i++)
{
fa[i]=i;
num[i]=1; //以i为根的联通块点的个数
}
for(int i=1;i<=m;i++)
{
int ra=find(edge[i].a);
int rb=find(edge[i].b);
if(ra!=rb) //不在一个联通块内,两个块内的点通过该边连接,f值为该边
{
if(myp.find(edge[i].c)!=myp.end())
myp[edge[i].c]=myp[edge[i].c]+num[ra]*num[rb]*2;
else
myp[edge[i].c]=num[ra]*num[rb]*2;
fa[rb]=ra; //合并
num[ra]+=num[rb];
}
}
int q;
scanf("%d",&q);
sort(sig+1,sig+m+1); //对边权值排序
memset(sum,0,sizeof(sum));//sum[i]表示以大于等于第i大边权值的权值作为f值得点对总的个数。
sum[m]=myp[sig[m]];
for(int i=m-1;i>=1;i--)
sum[i]+=sum[i+1]+myp[sig[i]];
for(int i=1;i<=q;i++)
{
int tt;
scanf("%d",&tt);
int pos=lower_bound(sig+1,sig+m+1,tt)-sig;//二分查找位置
printf("%d\n",sum[pos]); } }
return 0;
}
并查集+二分-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 (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(并查集)
题目链接: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 ...
- 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 5441——Travel——————【并查集+二分查界限】
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
随机推荐
- [转]Laravel 4之表单
Laravel 4之表单 http://dingjiannan.com/2013/laravel-forms/ 创建表单 除了原有的方式创建表单,Laravel提供了一种便捷的方式 <!-- a ...
- [Spring入门学习笔记][创建网站URL]
设计网站的URL 现代的Web站点都会设计一套拥有明确意义,方便用户记忆的URL,不论是域名还是路径,以天码营为例: http://tianmaying.com/courses表示网站下所有的课程列表 ...
- BOM 窗体相关属性以及页面可见区域的获取方式
1 在IE Safari Oper Chrome 都提供了screenLeft和screenTop属性: screenLeft : 相对于屏幕左边的距离 screenTop : 相对于屏幕上边的距离 ...
- Java学习——多态
多态:可以理解为事物存在的多种体现形态. 人:男人,女人 动物:猫,狗 猫 x = new 猫(); 动物 x = new 猫(); 1,多态的体现 父类的引用指向了自己的子类对象. 父类的引用也可以 ...
- “有箭头的视图”,即程序的Storyboard Entry Point。
设置方法很简单:打开StoryBoard文件,选中要设置为第一视图的ViewController,在右边工具栏勾选Is Initial View Controller就好了,此时你会看到ViewCon ...
- uva 1597 Searching the Web
The word "search engine" may not be strange to you. Generally speaking, a search engine se ...
- 关于LayoutParams
每一个布局均有一个叫LayoutParams的内部类,如: LinearLayout.LayoutParams RelativeLayout.LayoutParams AbsoluteLayout ...
- Struts2 - 常用的constant标签[转]
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- Web程序工作原理
1.Web程序工作原理 (1)Web一词的含义 Network:[计算机]电脑网络,网 Web:[计算机]万维网(World Wide Web),互联网(Internet) Web程序,顾名思义,即工 ...
- [原]C语言实现的快速排序,采用分治策略,递归实现
#include<stdio.h> #define LEN 8 int a[LEN] = { 5, 2, 4, 7, 1, 3, 2, 6 }; int Partition(int a[] ...