按边从小到大排序。

对于每条边(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. logistic regression (逻辑回归) 概述

    :http://hi.baidu.com/hehehehello/blog/item/0b59cd803bf15ece9023d96e.html#send http://en.wikipedia.or ...

  2. 基于建模的视觉定位(SFM-Based Positioning)

    具体方法来自我参与的这篇journal: Vision-Based Positioning for Internet-of-Vehicles, IEEE Transactions on Intelli ...

  3. apache配置局域网访问

    1.配置vhost.conf NameVirtualHost 192.168.2.74:80 <VirtualHost 192.168.2.74:80> DocumentRoot /var ...

  4. 2.初识CronTrigger

    开发工具:Eclipse 代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git 前言: 在1.初始Quartz里面,我们介绍了quartz的一 ...

  5. python生成xml文件

    先上代码: #!/usr/bin/env python3 # _*_ coding: utf-8 _*_ from xml.dom.minidom import Document def readFi ...

  6. SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别。

    DROP: DROP TABLE test; 删除表test,并释放空间,将test删除的一干二净. TRUNCATE: TRUNCATE test; 删除表test里的内容,并释放空间,但不删除表的 ...

  7. vue学习--父子组件通讯

    this.$parent        // 子组件访问父组件 this.$root            // 访问根实例 this.$children        // 父组件的所有子元素 *一 ...

  8. pywinauto 的使用

    要用python实现Windows窗口程序的自动化操作,可以用ctypes调用windowsapi来实现,还可以用pywin32+pywinauto来实现,后者是别人造的轮子. pywinauto首页 ...

  9. 使用cookie登陆知乎

    只是想说明一个问题,Cookie可以维持登录状态,有些网页当中,访问之后的cookie里面带有登陆账号,和登陆密码,这样可以使用cookie直接访问网页,如知乎,首先登录知乎,将Headers中的Co ...

  10. 《Redis设计与实现》- RDB持久化

    Redis RDB持久化功能可以将Redis内存中的数据库状态保存到磁盘里面,避免数据意外丢失. 1. 手动生成 RDB 文件 有两个Redis命令可以用于生成RDB文件: SAVE,该命令会阻塞Re ...