题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w,

那么需要城市u到城市w的长度e1≤d,同时城市w到城市v的长度e2≤d)。

析:一开始的时候,题意都读错了,怎么看都不对,原来是只要最大的d小于等于x就可以,过了好几天才知道是这样。。。。。

这个题是并查集的应用,先从d小的开始遍历,然后去判断有几个连通块,在连通块中计数,用一个数组即可,运用排列组合的知识可以知道一个连通块中, 一共有

n * (n-1)对,然后不断更新连通块中结点的数量即可,先排序再输出。其实并不难。要注意加结点的时候,谁是谁是父结点,这个是很重要的。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. using namespace std ;
  15.  
  16. typedef long long LL;
  17. typedef pair<int, int> P;
  18. const int INF = 0x3f3f3f3f;
  19. const double inf = 0x3f3f3f3f3f3f3f;
  20. const double eps = 1e-8;
  21. const int maxn = 2e4 + 5;
  22. const int mod = 1e9 + 7;
  23. const int dr[] = {0, 0, -1, 1};
  24. const int dc[] = {-1, 1, 0, 0};
  25. int n, m;
  26. inline bool is_in(int r, int c){
  27. return r >= 0 && r < n && c >= 0 && c < m;
  28. }
  29. struct node{
  30. int u, v, w;
  31. bool operator < (const node &p) const{
  32. return w < p.w;
  33. }
  34. };
  35. node a[maxn*5];
  36. struct node1{
  37. int id, w;
  38. bool operator < (const node1 &p) const{
  39. return w < p.w;
  40. }
  41. };
  42. node1 x[5005];
  43. int p[maxn], w[maxn], ans[maxn];
  44.  
  45. int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
  46.  
  47. int main(){
  48. int T; cin >> T;
  49. while(T--){
  50. int q;
  51. scanf("%d %d %d", &n, &m, &q);
  52. for(int i = 1; i <= n; ++i) p[i] = i, w[i] = 1;
  53. for(int i = 0; i < m; ++i) scanf("%d %d %d", &a[i].u, &a[i].v, &a[i].w);
  54. for(int i = 0; i < q; ++i) scanf("%d", &x[i].w), x[i].id = i;
  55. sort(a, a+m);
  56. sort(x, x+q);
  57.  
  58. int j = 0, num = 0;
  59. for(int i = 0; i < q; ++i){
  60. while(j < m && a[j].w <= x[i].w){
  61. int xx = Find(a[j].u);
  62. int yy = Find(a[j].v);
  63. if(xx != yy){
  64. num -= w[xx] * (w[xx]-1) + w[yy] * (w[yy]-1);
  65. w[xx] += w[yy];
  66. w[yy] = 0;
  67. num += w[xx] * (w[xx]-1);
  68. p[yy] = xx;//注意父结点的选取
  69. }
  70. ++j;
  71. }
  72. ans[x[i].id] = num;
  73. }
  74.  
  75. for(int i = 0; i < q; ++i)
  76. printf("%d\n", ans[i]);
  77. }
  78. return 0;
  79. }

HDU 5441 Travel (并查集+数学+计数)的更多相关文章

  1. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  2. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  3. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  4. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  5. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  6. HDU 5441——Travel——————【并查集+二分查界限】

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  7. hdu 5441 Travel (2015长春网赛)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题目大意是给一个n个城市(点)m条路线(边)的双向的路线图,每条路线有时间值(带权图),然后q个询问,每个 ...

  8. HDU 2818 (矢量并查集)

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

  9. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

随机推荐

  1. uva1637Double Patience

    状态压缩,记忆化搜索. 用一个5进制数来表示每堆排到了哪一个位置.和2进制是一样的,不过不能用位运算. #include<cstdio> #include<algorithm> ...

  2. android webview js alert对话框 不能弹出 解决办法

    在配置了webview的 setting属性后,以前设置的都是可以直接弹出来的,今天写一个小demo时候莫名其妙的发现alert怎么也出来,即使设置了这么多也不行: webSettings.setJa ...

  3. 【转】APUE学习1:迈出第一步,编译myls.c

    原文网址:http://blog.csdn.net/sddzycnqjn/article/details/7252444 注:以下写作风格均学习自潘云登前辈 /******************** ...

  4. TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流

    目前建立在TCP协议上的网络协议特别多,有telnet,ssh,有ftp,有http等等.这些协议又可以根据数据吞吐量来大致分成两大类:(1)交互数据类型,例如telnet,ssh,这种类型的协议在大 ...

  5. 解决32位plsql客户端连接不64位Oracle11g上数据库

    一.解决方案 因为本人安装的是64位的Oracle,plsql 是32位的故连接不上.网上有方法能连接. 1. 文件下载 下载PLSQL_Developer地址 http://pan.baidu.co ...

  6. 开源GIS简介

    原文 开源GIS C++开源GIS中间件类库: GDAL(栅格)/OGR(矢量)提供了类型丰富的读写支持 GEOS(Geometry Engine Open Source)是基于C++的空间拓扑分析实 ...

  7. Unable to execute dex: method ID not in [0, 0xffff]: 65536

    http://ingramchen.io/blog/2014/09/prevention-of-android-dex-64k-method-size-limit.html

  8. CXF之七 传输文件

    CXF的文件传输通过MTOM实现.MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息传输优化机制,可以在SOAP消息中发送二进制数 ...

  9. ASP.NET 中JSON 的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  10. C++实现离散余弦变换(参数为二维指针)

    C++实现离散余弦变换(参数为二维指针) 写在前面 到目前为止已经阅读了相当一部分的网格水印等方面的论文了,但是论文的实现进度还没有更上,这个月准备挑选一些较为经典的论文,将其中的算法实现.在实现论文 ...