按边从小到大排序。

对于每条边(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. 第30章 ADC—电压采集—零死角玩转STM32-F429系列

    第30章     ADC—电压采集 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...

  2. js循环读取json数据,将读取到的数据用js写成表格

    ①js循环读取json数据的方式: var data=[{"uid":"2688","uname":"*江苏省南菁高级中学 022 ...

  3. (排班表一)使用SQL语句使数据从坚向排列转化成横向排列

    知识重点: 1.extract(day from schedule01::timestamp)=13 Extract 属于 SQL 的 DML(即数据库管理语言)函数,同样,InterBase 也支持 ...

  4. rsync+lsyncd实现实时同步

    1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...

  5. Wordpress网站中添加百度统计代码

    百度统计是流量分析平台,帮助收集网站访问数据,提供流量趋势.来源分析.转化跟踪.页面热力图.访问流等多种统计分析服务,同时与百度搜索.百度推广.云服务无缝结合,为网站的精细化运营决策提供数据支持,进而 ...

  6. (转)数据库老兵:NewSQL才是未来

    编者按:在数据库技术领域,Michael Stonebraker几乎是无人不知无人不晓的人物.现年70岁的Stonebraker不仅是Ingres和PostgreSQL的创始人,同时在Informix ...

  7. C++基础 匿名对象

    以下几种情况又会匿名对象 (1)对象构造 与 匿名对象 Test t1 = Test(); 这时,Test()会构造匿名对象,并且是调用无参构造函数,然后 t1 将匿名对象扶正. (2)对象赋值 与 ...

  8. python-10多进程

    1-多进程(multiprocessing), 1个父进程可以有多少子进程 1.1下面的例子演示了启动一个子进程并等待其结束 from multiprocessing import Process i ...

  9. 笔记-pyrhon-lib-requests

    笔记-pyrhon-lib-requests 1.      简介 Requests is the only Non-GMO HTTP library for Python, safe for hum ...

  10. struct2 命名空间

    转自http://blog.csdn.net/carefree31441/article/details/4857546 使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s: ...