按边从小到大排序。

对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to。

如果from和to不属于同一个集合,那么增加这条边后增加的点对数目是cnt[from]*cnt[to]*2( 因为(u, v)和(v, u)不算同一点对,所以*2 )

统计出所有点对数total。

对于查询,按t值从小到大排序,边从小到大一条一条往里加。

tmpSum为f值小于t的点对总数。

当边权大于等于t值时:ans[i] = total - tmpSum。

当边权小于t值时,更新tmpSum。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int using namespace std; const int MAXN = ; struct node
{
int from, to, dist;
bool friend operator<( node lhs, node rhs )
{
return lhs.dist < rhs.dist;
}
}; struct Query
{
int id;
LL t;
bool friend operator<( Query lhs, Query rhs )
{
return lhs.t < rhs.t;
}
}; node D[MAXN*];
int N, M;
LL ans[MAXN*];
Query qry[MAXN*]; int p[MAXN];
LL cnt[MAXN]; int find( int x )
{
return p[x] == x ? x : p[x] = find(p[x]);
} void init()
{
for ( int i = ; i <= N; ++i )
{
p[i] = i;
cnt[i] = ;
}
return;
} int main()
{
while ( scanf( "%d%d", &N, &M ) == )
{
init();
for ( int i = ; i < M; ++i )
{
scanf("%d%d%d", &D[i].from, &D[i].to, &D[i].dist );
int x = find( D[i].from );
int y = find( D[i].to );
if ( x != y )
{
p[y] = x;
cnt[x] += cnt[y];
}
} LL total = ;//统计所有点对
for ( int i = ; i < N; ++i )
{
if ( p[i] == i )
total += ( cnt[i]*( cnt[i] - ) );
} sort( D, D + M );
int Q;
scanf( "%d", &Q );
for ( int i = ; i < Q; ++i )
{
scanf( "%I64d", &qry[i].t );
qry[i].id = i;
}
sort( qry, qry + Q ); init();
int i = , j = ;
LL tmpSum = ;
while ( j < Q )
{
//printf( "tot=%I64d tmp=%I64d\n", total, tmpSum );
if ( i < M && qry[j].t <= D[i].dist )
{
int id = qry[j].id;
ans[id] = total - tmpSum;
++j;
}
else if ( i < M )
{
int x = find( D[i].from );
int y = find( D[i].to );
if ( x != y )
{
p[y] = x;
tmpSum += cnt[x]*cnt[y]*;
cnt[x] += cnt[y];
}
++i;
}
else if ( i >= M )
{
ans[ qry[j].id ] = total - tmpSum;
++j;
}
} for ( int i = ; i < Q; ++i )
printf( "%I64d\n", ans[i] );
}
return ;
}

HDU 4750 Count The Pairs (离线并查集)的更多相关文章

  1. HDU 4750 Count The Pairs ★(图+并查集+树状数组)

    题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...

  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. 2013南京网赛1003 hdu 4750 Count The Pairs

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 系统运行时间悬浮框(demo)

    此项目基于.net framework 4.0 思路: 拖一个定时器控件,每秒执行一次,调用函数查询当前运行时间并更新到label控件. private void Form1_Load(object ...

  2. mac jdk profile 永久的配置

    配置java_home 在MAC中设置JAVA_HOME环境变量 环境变量要再etc目录下的profile文件中配置,这样才是永久的配置. cd /etc vi profile 按 i 键进入写模式 ...

  3. Java中阻塞队列的使用

    http://blog.csdn.net/qq_35101189/article/details/56008342 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如 ...

  4. linux awk 内置函数详细介绍(实例)

    这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...

  5. AngularJS 重复HTML元素

    data-ng-repeat指令会重复一个HTML元素 <!DOCTYPE html><html><head><meta http-equiv="C ...

  6. C# is运算符

    一.C# is运算符 is运算符用于检查对象是否与给定类型兼容(兼容表示对象是该类型,或者派生于该类型). is运算符用来检查对象(变量)是否属于某种数据类型,如int.string.bool.dou ...

  7. Spring 学习之依赖注入

    什么是依赖关系? 纵观所有的Java 应用,从基于Applet的小应用到多层次结构的企业级别的应用,他们都是一种典型的依赖性应用,也就是由一些互相协作的对象构成的,Spring把这种互相协作的关系称之 ...

  8. ios xmppFramework框架的导入步骤和介绍

    一个将要开发xmpp的项目,建议在项目刚创建就导入框架,这样可以避免一些自己操作失误造成不必要的损失. xmpp中最常用的框架就是 xmppFrameWork 第一种方法直接拖 1> 拖入文件夹 ...

  9. 在centos7云服务器上搭建Apache服务器并访问到你的网站

    使用X-shell ssh安全连接到云服务器 https://mail.qq.com/cgi-bin/mail_spam?action=check_link&url=https://www.n ...

  10. 【赛时总结】◇赛时·VI◇ Atcoder ABC-104

    ◇赛时·VI◇ ABC-104 ◆??? 莫名爆炸……ABC都AK不了 QwQ C题竟然沦落到卡数据的地步:D题没有思路,直接放弃 ⋋( ◕ ∧ ◕ )⋌ ◆ 题目&解析 ◇A题◇ Rated ...