HDU 4750
解题方法,,,首先应该可以看出来是一颗 最小生成树,任意一条的边的价值是不同的;所以计算出最小生成树的每一条边有多少对顶点满足他的 f 值就是这条边的 权值,因此可以在生成最小生成树的时候,进行一下统计,每加入一条边,就统计一下,得到 f 值和这条边权值相同有多少对顶点;方法是 记录一个 rank 数组,记录每个分支里面有多少个顶点,合并的时候,以为 是按照权值从小大大放入的,所以结果是 rank[a]*ran[b]*2;
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; struct date{
int u,v,w;
bool operator < ( const date &a )const{
return a.w > w;
}
}edge[];
int N,M,Q;
int f[];__int64 rank[];
int find( int x ){
if( x != f[x] )return f[x] = find( f[x] );
return x;
}
int res[]; __int64 num[];
int search( int lt,int rt,int key )
{
if( rt - lt < )
{
for( int i = lt; i <= rt; i++ )
if( res[i] >= key )return i;
return rt+;
}
int mid = ( lt + rt )>>;
if( key > res[mid] )
return search( mid,rt,key );
else return search( lt,mid,key );
}
int main( )
{
int u,v,w;
while( scanf("%d%d",&N,&M) != EOF )
{
for( __int64 i =; i <= M; i++ ){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
} sort( &edge[],&edge[]+M );
for( int i =; i <= N; i++ )f[i] = i;
for( int i =; i <= N; i++ )rank[i] =;
int k =;
for( int i =; i <= M; i++ )
{
int u = edge[i].u; int v = edge[i].v;
int a = find( u ); int b = find( v );
if( a != b )
{
res[++k] = edge[i].w;
num[k] = rank[a]*rank[b]*;
rank[b] += rank[a];
f[a] = b;
}
}
for( int i = k-; i >=; i-- )
num[i] = num[i]+num[i+];
scanf("%d",&Q);
for( int i =; i <= Q; i++ )
{
int a; scanf("%d",&a);
int pos = search(,k,a );
if( pos > k )puts("0");
else printf("%I64d\n",num[pos]);
}
}
return;
}
HDU 4750的更多相关文章
- [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 ...
- 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(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- 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南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- 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, ...
- HDU 4750 Count The Pairs (离线并查集)
按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...
随机推荐
- 项目上线与LOG记录
如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...
- java如何追加写入txt文件
java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import ...
- [RM HA3] Zookeeper在RM HA的应用
[RM HA3] Zookeeper在RM HA的应用 RM HA(ResourceManager HighAvailability)中使用Zookeeper的地方在ZKRMStateStore和Z ...
- C#日期大全
DateTime dt = DateTime.Now; // Label1.Text = dt.ToString();//2005-11-5 13:21:25 // Label2.Text = dt. ...
- Junit单元测试学习笔记三
一. 高级 Fixture 上一篇文章中我们介绍了两个 Fixture 标注,分别是 @Before 和 @After ,我们来看看他们是否适合完成如下功能:有一个类是负责对大文件(超过 50 ...
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解
一.简介 1.You now have three methods for distinguishing references: Objects are identical if they occ ...
- wordpress学习二:源码目录结构和启动流程
wordpress安装后的文件目录如下: 其中的主要目录和文件用途介绍如下: wp-admin:用于进行博客后台设置的功能目录 wp-content: wordpress的 主题,插件和本地化的存储目 ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- Linux命令-free
显示系统内存使用情况 free [-m / -g / -h] [root@localhost test]# free -m total used free shared buff/cache ava ...