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. Linking different libraries for Debug and Release builds in Cmake on windows?

    问题叙述性说明: So I've got a library I'm compiling and I need to link different third party things in depe ...

  2. 关于fastjson用法

    fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发. public static final String toJSONString(Obje ...

  3. sql 多列求和

    列相加即可注意Null不可加,先用ISNULL方法验证,设置默认值 SELECT ID, Name, Province, City, District, ISNULL(row1, 0), ISNULL ...

  4. WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序

    原文:WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序 前面我讲了在win7下使用模拟器,进行调试模拟多点触摸,其实际开发中这样也比较麻烦.. 要拿几个鼠标. 所以更多的人会 买个触摸套 套 ...

  5. Visual C++ 编译器自动假定带 .C 扩展名的文件是 C 文件而不是 C++ 文件,并且拒绝 C++ 语法和关键字(c语言只能在大括号最前面申明变量)

    今天在编译OpenGL红宝书附带源码中的light.c文件时遇到一个诡异的问题: 如图light .c,在不做任何修改的情况编译OK.然而只要在某些地方写了可执行代码,则会无法通过编译器编译! (这几 ...

  6. Spring 核心技术(1)

    接上篇:Spring 框架概述 version 5.1.8.RELEASE 这部分参考文档涵盖了 Spring Framework 所有绝对不可或缺的技术. 其中最重要的是 Spring Framew ...

  7. C# XML 去xmlns:xsd和xmlns:xsi属性

    public static XElement WithoutNamespaces(this XElement element) { if (element == null) return null; ...

  8. Xdite:永葆热情的上瘾式学习法(套路王:每天总结自己,反省自己的作息规律,找到自己的幸运时间、幸运方法,倒霉时间、倒霉方法。幸运是与注意力挂钩的。重复才能让自己登峰造极,主动去掉运气部分来训练自己。游戏吸引自己的几个原因非常适合训练自己)good

    版权声明 本文首发自微信公共帐号: 学习学习再学习(xiaolai-xuexi) 无需授权即可转载, 甚至无需保留以上版权声明: 转载时请务必注明作者. 以下是<共同成长社区>第 58 次 ...

  9. .Net DateTime跨时区相关问题

    项目:.Net CS结构,WCF通信,MySql存储. 场景:客户端(UTC+07:00)获取本地时间(DateTime对象)2017-01-17 15:20:12,通过WCF(http)传输至服务端 ...

  10. Android零基础入门第20节:CheckBox和RadioButton使用大全

    原文:Android零基础入门第20节:CheckBox和RadioButton使用大全 本期先来学习Button的两个子控件,无论是单选还是复选,在实际开发中都是使用的较多的控件,相信通过本期的学习 ...