按边从小到大排序。

对于每条边(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. 矩阵——特征向量(Eigenvector)

    原文链接 矩阵的基础内容以前已经提到,今天我们来看看矩阵的重要特性——特征向量. 矩阵是个非常抽象的数学概念,很多人到了这里往往望而生畏.比如矩阵的乘法为什么有这样奇怪的定义?实际上是由工程实际需要定 ...

  2. HTML5 input date 移动端 IOS 不支持问题

    1.placeholder 问题解决方法 对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 date 不会显示 placehol ...

  3. Velocity中加载vm文件的三种方式

    Velocity中加载vm文件的三种方式: a.  加载classpath目录下的vm文件 /** * 初始化Velocity引擎 * --VelocityEngine是单例模式,线程安全 * @th ...

  4. System.Web

    如果 using System.Web:还是调用不出来其中的类,请在引用的位子添加 System.Web  引用,有的版本不自带这个命名空间. 类: HttpResponse类       用于绘画验 ...

  5. c#简单加密和对称加密

    转自:https://www.cnblogs.com/zhihai/archive/2012/05/03/2480856.html using System;using System.Collecti ...

  6. C#把日期转化成星期

    显示效果:       *****      前台页面代码: <TextBlock Grid.Row="/> <TextBlock Grid.Row="/> ...

  7. 堆(heap)和栈(stack)几点认识

    堆(heap)和栈(stack)主要的区别由以下几点:1.管理方式不同:2.空间大小不同:3.产生碎片不同:4.生长方向不同:5.分配归属不同:6.分配效率不同:7.存取效率不同:管理方式:对于栈来讲 ...

  8. BZOJ3170: [Tjoi2013]松鼠聚会(切比雪夫距离转曼哈顿距离)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1524  Solved: 803[Submit][Status][Discuss] Descripti ...

  9. linux:eth网卡对应的物理网口判断

    可以利用ethtool命令 #ethtool -p eth0 执行上述命令则相应的物理网口会闪烁,则可以判断对应的物理网口 注:应在不插网线的情况下测试

  10. python__系统 : 线程

    线程之间,全局变量可以共享,但是局部变量依然是不共享的,线程的创建方式: threading.Thread(),还可以定义一个类继承Thread,重写他的run方法,具体和进程的写法一样. 那么,线程 ...