http://acm.hdu.edu.cn/showproblem.php?pid=3938

Portal

Problem Description
 
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
 
Input
 
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
 
Output
 
Output the answer to each query on a separate line.
 
Sample Input
 
10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6
 
Sample Output
 
36
13
1
13
36
1
3
6
2
16
13
 
 #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 10005
#define M 50005
/*
因为询问比较多,所以需要离线
在线的意思就是每一个询问单独处理复杂度O(多少多少),
离线是指将所有的可能的询问先一次都处理出来,
最后对于每个询问O(1)回答 对于每个询问有一个长度L,问有多少条路径的长度<=L
而且该路径的长度是T,T是从u到v上最长的边
只要求得有多少个点对使得点对之间的最大的边小于L即可。 先从小到大排序一遍询问的边的长度L,从小到大枚举u->v之间的边的长度
如果两个集合没有联通,那么联通之后路径的条数为sum[x]*sum[y]
因为长的L必定包含了短的L的答案,所以要累加起来
*/
int fa[N],sum[N]; struct node1
{
int id,ans,l;
}query[N]; struct node2
{
int u,v,len;
}edge[M]; bool cmp1(node2 a,node2 b)
{
return a.len<b.len;
} bool cmp2(node1 a,node1 b)
{
return a.id<b.id;
} bool cmp3(node1 a,node1 b)
{
return a.l<b.l;
} int Find(int x)
{
if(x==fa[x]) return x;
return fa[x]=Find(fa[x]);
} int Merge(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx==fy) return ;
int tmp;
if(fx<fy){
fa[fy]=fx;
tmp=sum[fx]*sum[fy];
sum[fx]+=sum[fy];
}
else{
fa[fx]=fy;
tmp=sum[fx]*sum[fy];
sum[fy]+=sum[fx];
}
return tmp;
} int main()
{
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q)){
for(int i=;i<=n;i++){
fa[i]=i;
sum[i]=;
}
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].len);
}
for(int i=;i<q;i++){
scanf("%d",&query[i].l);
query[i].id=i;
query[i].ans=;
}
sort(edge,edge+m,cmp1);
sort(query,query+q,cmp3);
int cnt=;
for(int i=;i<q;i++){
while(edge[cnt].len<=query[i].l&&cnt<m){
int x=edge[cnt].u;
int y=edge[cnt].v;
int fx=Find(x);
int fy=Find(y);
if(fx==fy) cnt++;
else{
query[i].ans+=Merge(x,y);
cnt++;
}
}
if(i>) query[i].ans+=query[i-].ans;
}
sort(query,query+q,cmp2);
for(int i=;i<q;i++){
printf("%d\n",query[i].ans);
}
}
return ;
}

HDU 3938:Portal(并查集+离线处理)的更多相关文章

  1. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. BZOJ5188: [Usaco2018 Jan]MooTube 并查集+离线处理

    BZOJ又不给题面... Luogu的翻译看不下去... 题意简述 有一个$n$个节点的树,边有权值,定义两个节点之间的距离为两点之间的路径上的最小边权 给你$Q$个询问,问你与点$v$的距离超过$k ...

  3. poj 2528 Mayor's posters 线段树 || 并查集 离线处理

    题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...

  4. ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)

    Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...

  5. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...

  6. zoj3261 并查集离线处理

    Connections in Galaxy War Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  7. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  8. BZOJ-1015 StarWar星球大战 并查集+离线处理

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Submit: 4105 Solved: 1826 [Submit ...

  9. 【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

随机推荐

  1. C#使用Selenium实现QQ空间数据抓取 登录QQ空间

    经@吃西瓜的星星提醒 首先我们介绍下Selenium Selenium也是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mo ...

  2. 【转】postgresql 9.4 在linux环境的安装步骤详解

    本文章来为各位介绍一篇关于postgresql 9.4 在linux环境的安装步骤详解,希望文章能够对各位新手朋友带来帮助的哦. 环境说明系统:centos 6.4 64位软件:postgresql ...

  3. 投资人的能量往往大多远远不仅于此,他能站在不同的角度和高度看问题(要早点拿投资,要舍得让出股份)——最好不要让 Leader 一边做技术、一边做管理,人的能力是有限的,精力也是有限的

      摘要:在创业三年时间里作为联合创始人,虽然拿着大家均等的股份,我始终是没有什么话语权的,但是,这也给了我从旁观者的角度看清整个局面的机会.创业公司的成败绝大程度取决于技术大牛和公司 Leader, ...

  4. 【全面解禁!真正的Expression Blend实战开发技巧】第二章 你好,UI设计师

    原文:[全面解禁!真正的Expression Blend实战开发技巧]第二章 你好,UI设计师 你好,UI设计师 曾几何时我从没想过要与艺术家打交道,但是Silverlight改变了这一切.UI设计师 ...

  5. Win10《芒果TV - Preview》更新v3.1.31.0,全新播放页蜕变,预加载提速技术

    Win10<芒果TV - Preview>(商店内测版) v3.1.31.0 于2016年11月21日星期一晚上九点半登陆商店 主要是全面升级改造桌面播放页,新增观看互动评论.猜你喜欢功能 ...

  6. Android零基础入门第79节:Intent 属性详解(上)

    Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,则取决于Intent的各属性.本期将详细介绍Intent的各属性值,以及 Android如何根据不同属性值来启动相应的组件. ...

  7. 关于 win32 下磁盘的遍历方法

    最近要写个在线专杀的东东,虽然是专杀(本来只要清除几个特定的文件和杀几个特定的进程,然后把用户的注册表恢复正常,很多病毒木马最喜欢干的一件事情就是写 映像劫持 然后机器一重启,安全相关的软件全部玩完了 ...

  8. 使用熔断器仪表盘监控(hystrix)

    概述 在 Ribbon 和 Feign 项目增加 Hystrix 仪表盘功能,两个项目的改造方式相同. 在 pom.xml 中增加依赖 <dependency> <groupId&g ...

  9. TCP使用注意事项总结

    目录 发送或者接受数据过程中对端可能发生的情况汇总 本端TCP发送数据时对端进程已经崩溃 本端TCP发送数据时对端主机已经崩溃 本端TCP发送数据时对端主机已经关机 某个连接长时间没有数据流动 TCP ...

  10. nginx搭建静态网站

    Nginx (engine x) 是一个高性能的HTTP和反向代理web服务,常用于负载均衡构架,以提高网站的并发量,概念不过多介绍,更多细节请自行百度, 本文是纯操作案例,假设你已经知道什么是ngi ...