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
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. #define N 10005
  8. #define M 50005
  9. /*
  10. 因为询问比较多,所以需要离线
  11. 在线的意思就是每一个询问单独处理复杂度O(多少多少),
  12. 离线是指将所有的可能的询问先一次都处理出来,
  13. 最后对于每个询问O(1)回答
  14.  
  15. 对于每个询问有一个长度L,问有多少条路径的长度<=L
  16. 而且该路径的长度是T,T是从u到v上最长的边
  17. 只要求得有多少个点对使得点对之间的最大的边小于L即可。
  18.  
  19. 先从小到大排序一遍询问的边的长度L,从小到大枚举u->v之间的边的长度
  20. 如果两个集合没有联通,那么联通之后路径的条数为sum[x]*sum[y]
  21. 因为长的L必定包含了短的L的答案,所以要累加起来
  22. */
  23. int fa[N],sum[N];
  24.  
  25. struct node1
  26. {
  27. int id,ans,l;
  28. }query[N];
  29.  
  30. struct node2
  31. {
  32. int u,v,len;
  33. }edge[M];
  34.  
  35. bool cmp1(node2 a,node2 b)
  36. {
  37. return a.len<b.len;
  38. }
  39.  
  40. bool cmp2(node1 a,node1 b)
  41. {
  42. return a.id<b.id;
  43. }
  44.  
  45. bool cmp3(node1 a,node1 b)
  46. {
  47. return a.l<b.l;
  48. }
  49.  
  50. int Find(int x)
  51. {
  52. if(x==fa[x]) return x;
  53. return fa[x]=Find(fa[x]);
  54. }
  55.  
  56. int Merge(int x,int y)
  57. {
  58. int fx=Find(x),fy=Find(y);
  59. if(fx==fy) return ;
  60. int tmp;
  61. if(fx<fy){
  62. fa[fy]=fx;
  63. tmp=sum[fx]*sum[fy];
  64. sum[fx]+=sum[fy];
  65. }
  66. else{
  67. fa[fx]=fy;
  68. tmp=sum[fx]*sum[fy];
  69. sum[fy]+=sum[fx];
  70. }
  71. return tmp;
  72. }
  73.  
  74. int main()
  75. {
  76. int n,m,q;
  77. while(~scanf("%d%d%d",&n,&m,&q)){
  78. for(int i=;i<=n;i++){
  79. fa[i]=i;
  80. sum[i]=;
  81. }
  82. for(int i=;i<m;i++){
  83. scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].len);
  84. }
  85. for(int i=;i<q;i++){
  86. scanf("%d",&query[i].l);
  87. query[i].id=i;
  88. query[i].ans=;
  89. }
  90. sort(edge,edge+m,cmp1);
  91. sort(query,query+q,cmp3);
  92. int cnt=;
  93. for(int i=;i<q;i++){
  94. while(edge[cnt].len<=query[i].l&&cnt<m){
  95. int x=edge[cnt].u;
  96. int y=edge[cnt].v;
  97. int fx=Find(x);
  98. int fy=Find(y);
  99. if(fx==fy) cnt++;
  100. else{
  101. query[i].ans+=Merge(x,y);
  102. cnt++;
  103. }
  104. }
  105. if(i>) query[i].ans+=query[i-].ans;
  106. }
  107. sort(query,query+q,cmp2);
  108. for(int i=;i<q;i++){
  109. printf("%d\n",query[i].ans);
  110. }
  111. }
  112. return ;
  113. }

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. 最通俗易懂的方式让你理解 Swift 的函数式编程

    函数式编程(Functional Programming)是相对于我们常用的面向对象和面向过程编程的另外一种开发思维方式,它更加强调以函数为中心.善用函数式编程思路,可以对我们的开发工作有很大的帮助和 ...

  2. apt-spy 软件源更新

    ebian上的apt-get是最快的软件安装方式,不过要用好apt-get,首先得需要找到最快的源,这样安装软件的时候才能获得好的速度,用起来才能得心应手. 有的源在用了一段以后,就会失效,这个时候, ...

  3. DELPHI7中 TObjectList sort排序问题

    网上收集了一点东西 TOBJECTLIST里,有自带的排序功能 TLIST,TSTRINGLIST也有,MS是一样的 SORT里有一个参数: Compare:TListSortCompare 那我们先 ...

  4. 毕业设计之感悟 —— UML 与 ER 图

    今天毕业设计答辩,虽然我第一个上场,但是不是特别紧张,因为整个系统都是我写的.我以为自己天衣无缝,能应付所有老师的所有问题.事实上,我被老师教育了一番. 老师说我,毕业论文中没有一个类.我一开始比较懵 ...

  5. WPF媒体资源和图片资源寻址方式的杂谈

    WPF提供一个封装和存取资源(resource)的机制,我们可将资源建立在应用程序的不同范围上.WPF中,资源定义的位置决定了该资源的可用范围.资源可以定义在如下范围中: (1)控件级:此时,资源只能 ...

  6. WPF TreeView HierarchicalDataTemplate

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

  7. RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密

    原文:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密 C#在using System.Security.Cryptograph ...

  8. Android 调试桥(adb)是多种用途的工具

    Android 调试桥 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. 可以通过下列几种方法加入adb: 在设备上运行shell命令 通过端口转发来管理 ...

  9. Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案

    原文:Windows10 使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案 一打开虚拟机电脑就立马蓝屏重启,新建虚拟机也没用,然后就开始 ...

  10. Collection was modified; enumeration operation may not execute.的异常处理

    Collection was modified; enumeration operation may not execute.的异常处理 在运行程序时遇到这样一段异常,仔细检查后发现是使用Foreac ...