题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn = ;
  8. const int maxm = ;
  9.  
  10. struct Edge
  11. {
  12. int u,v,w;
  13. Edge(int u=,int v=,int w=): u(u), v(v), w(w) {}
  14. bool operator < (const Edge& rhs) const
  15. {
  16. return w < rhs.w;
  17. }
  18. }edges[maxm];
  19. struct Query
  20. {
  21. int id;
  22. int num;
  23. bool operator < (const Query& rhs) const
  24. {
  25. return num < rhs.num;
  26. }
  27. }Q[maxn*];
  28.  
  29. int pa[maxn];
  30. int counts[maxn];
  31. int ans[maxn*];
  32.  
  33. int find(int x)
  34. {
  35. return x == pa[x] ? x : pa[x] = find(pa[x]);
  36. }
  37.  
  38. int main()
  39. {
  40. //freopen("E:\\acm\\input.txt","r",stdin);
  41. int n,m,p;
  42. while(cin>>n>>m)
  43. {
  44. for(int i=; i<n; i++)
  45. {
  46. pa[i] = i;
  47. counts[i] = ;
  48. }
  49. for(int i=; i<m; i++)
  50. {
  51. int a,b,c;
  52. scanf("%d %d %d",&a,&b,&c);
  53. edges[i] = Edge(a,b,c);
  54. }
  55. sort(edges,edges+m);
  56.  
  57. scanf("%d",&p);
  58. for(int i=; i<p; i++)
  59. {
  60. scanf("%d",&Q[i].num);
  61. Q[i].id = i;
  62. }
  63. sort(Q,Q+p);
  64.  
  65. int cnt = ;
  66. int sum = ;
  67. int tot = n*(n-); //这么做前提是图是连通的,只能样例给的是连通的
  68.  
  69. for(int i=; i<p; i++)
  70. {
  71. while(cnt<m && edges[cnt].w < Q[i].num)
  72. {
  73. int u_fa = find(edges[cnt].u);
  74. int v_fa = find(edges[cnt].v);
  75. if(u_fa != v_fa)
  76. {
  77. sum = sum + counts[u_fa]*counts[v_fa]*;
  78. pa[v_fa] = u_fa;
  79. counts[u_fa] += counts[v_fa];
  80. }
  81. cnt++;
  82. }
  83.  
  84. ans[Q[i].id] = tot - sum;
  85. }
  86. for(int i=; i<p; i++)
  87. printf("%d\n",ans[i]);
  88. }
  89. }

hdu 4750 Count The Pairs(并查集)的更多相关文章

  1. hdu 4750 Count The Pairs(并查集+二分)

    Problem Description With the 60th anniversary celebration of Nanjing University of Science and Techn ...

  2. HDU 4750 Count The Pairs(并查集)

    题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...

  3. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  4. HDU 4750 Count The Pairs ★(图+并查集+树状数组)

    题意 给定一个无向图(N<=10000, E<=500000),定义f[s,t]表示从s到t经过的每条路径中最长的边的最小值.Q个询问,每个询问一个t,问有多少对(s, t)使得f[s, ...

  5. 2013南京网赛1003 hdu 4750 Count The Pairs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给出一个无向图,f(a,b)表示从点a到点b的所有路径中的每条路径的最长边中的最小值,给出 ...

  6. HDU 4750 Count The Pairs (离线并查集)

    按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...

  7. [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...

  8. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. oracle插入数据报错ORA-26026

    今天进行数据清理时发现报错ORA-26026,主要是把从交易库提取数据并插入到归档库中. 检查一下发现是归档库的索引问题. 当时为了提高插入速度,所以删除了归档库的索引,可能对主键索引产生了影响. 解 ...

  2. SQL Server T-SQL高级查询【转】

    高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student;   --all 查询所有 select all sex from ...

  3. vector预分配空间溢出

    vector 当一个vector预分配的存储空间用完之后,为维护其连续的对象数组,它必须在另外一个地方重新分配大块新的(更大的)存储空间,并把以前已有的对象拷贝到新的存储空间中去. // A clas ...

  4. 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)

    [热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...

  5. jquery中mouseout和mouseleave 事件的区别

    今天用jQuery写了一个选项卡的效果,用mouseout事件控制了鼠标的移出,结果发现在移出时div会发生闪动,于是网上各种查资料觉得用mouseleave更合适一些,

  6. Android 中 View移动总结:ViewDragHelper学习及用法详解

    如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...

  7. Object之魔术函数__toString() 直接输出对象引用时自动调用

    __toString()是快速获取对象的字符串信息的便捷方式 在直接输出对象引用时自动调用的方法. __toString()的作用 当我们调试程序时,需要知道是否得出正确的数据.比如打印一个对象时,看 ...

  8. WorkerScript QML Type

    官方描述:在一个Qt Quick应用程序中可以使用线程了. Import Statement:     import QtQuick .属性:source : url信号:message(jsobje ...

  9. Arduino从基础到实践第三章练习题

    先写在这里,还没经过测试. 1. LED两端往中间移动,到中间后向两边返回. // adr301.ino , , , , , , , , , }; ); ; ; unsigned long chang ...

  10. bzoj 2818: Gcd 歐拉函數

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1633  Solved: 724[Submit][Status] Descript ...