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. WPF中样式和行为和触发器

    原文:WPF中样式和行为和触发器 样式简介:样式(style)是组织和重用格式化选项的重要工具,不是使用重复的标记填充XAML,以便设置外边距.内边距.颜色以及字体等细节.而是创建一系列封装所有这些细 ...

  2. ASP UserInfoList 方法1

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserInfoList.a ...

  3. 关于阿里云centos7安装svn,客服端无法链接的问题

    阿里云的centos7的版本中,通过yum安装了subversion之后,svn客服端无法链接svn服务器. 首先确定服务器的安全组策略中的3690端口是否打开 然后确定svnserve配置是否正确, ...

  4. PostMessage和SendMessage有什么区别?(有EnumChildWindowsProc的例子)

    PostMessage只是把消息放入队列,不管其他程序是否处理都返回,然后继续执行;而SendMessage必须等待其他程序处理消息后才返回,继续执行.PostMessage的返回值表示PostMes ...

  5. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  6. winform实现DataGridView全选

    之前写过,一时想不起来就在网上找了一些.结果感觉好麻烦.于是就自己打开之前做过的功能.找到源码. private void CheckDatabasexuan() { DataGridViewChec ...

  7. HTML特殊编码转换

    var encoded = ""'&<>¡¢£¤" + "¥¦§¨©ª«¬­®" + "¯°±²³´µ¶·" ...

  8. 数据可视化图表ECharts

    介绍: ECharts是一个基于ZRender(轻量级Canvas类库)的纯javascript图表库,提供可交互.个性化的数据可视化图表. ECharts提供了折线图.柱状图.散点图.饼图.K线图, ...

  9. 网易Lofter

    作为老网虫,对网易的感情是很深的.当我知道Lofter这个东西后,曾经很兴奋要好好打理自己的博客,然而兴奋很快过了.因为Lofter无时无刻不在刷存在感.无限空间加无限图片流量,国内的服务无法做的更好 ...

  10. 从Java和JavaScript来学习Haskell和Groovy

    直击现场 记得刚接触计算机的时候,我就受到了两个非常巨大的错误观念的影响,这个观念最初是来自于老师的传授还是学长的教诲已经记不清了,但是直到我工作几年以后,才慢慢有了实际的体会: 学习和使用什么编程语 ...