Codeforces Round #582 (Div. 3)-G. Path Queries-并查集


【Problem Description】

给你一棵树,求有多少条简单路径\((u,v)\),满足\(u\)到\(v\)这条路径上的最大值不超过\(k\)。\(q\)次查询。

【Solution】

并查集

将所有边按权值从小到大排序,查询值\(k_i\)也从小到大排序。对于每次查询的值\(k_i\),将边权小于等于\(k_i\)的边通过并查集合并在一起。对于合并后的联通块,每个联通块对答案的贡献为\(\frac{size\times(size-1)}{2}\),所有联通块的贡献直接求和即可。所以每次合并两个节点时,先将这两个节点所在的联通块的贡献减去,再加上合并后的贡献即可。


【Code】

  1. /*
  2. * @Author: Simon
  3. * @Date: 2019-09-07 10:00:30
  4. * @Last Modified by: Simon
  5. * @Last Modified time: 2019-09-07 10:29:11
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. typedef int Int;
  10. #define int long long
  11. #define INF 0x3f3f3f3f
  12. #define maxn 200005
  13. struct node{
  14. int u,v,w;
  15. node(){}
  16. node(int u,int v,int w):u(u),v(v),w(w){}
  17. bool operator <(const node&a)const{
  18. return w<a.w;
  19. }
  20. }g[maxn],q[maxn];
  21. int Set[maxn],Rank[maxn];
  22. void init(int n){ //初始化
  23. for(int i=0;i<=n;i++){
  24. Set[i]=i;Rank[i]=1;
  25. }
  26. }
  27. int cal(int x){ //计算联通块的贡献
  28. return x*(x-1)/2;
  29. }
  30. int Find(int u){ //并查集的查找根节点
  31. return Set[u]==u?u:Set[u]=Find(Set[u]);
  32. }
  33. int res=0;
  34. int ans[maxn];
  35. void merge(int u,int v){ //并查集的合并
  36. u=Find(u),v=Find(v);
  37. if(Rank[u]<Rank[v]) swap(u,v);
  38. res-=cal(Rank[u]),res-=cal(Rank[v]); //减去u,v所在联通块的贡献
  39. Set[v]=u;Rank[u]+=Rank[v]; //合并两个联通块
  40. res+=cal(Rank[u]); //再加上合并后的联通块的贡献
  41. }
  42. Int main(){
  43. #ifndef ONLINE_JUDGE
  44. //freopen("input.in","r",stdin);
  45. //freopen("output.out","w",stdout);
  46. #endif
  47. ios::sync_with_stdio(false);
  48. cin.tie(0);
  49. int n,m;cin>>n>>m;init(n);
  50. for(int i=1;i<n;i++){
  51. cin>>g[i].u>>g[i].v>>g[i].w;
  52. }
  53. sort(g+1,g+n);
  54. for(int i=1;i<=m;i++){
  55. cin>>q[i].w;q[i].u=i;
  56. }
  57. sort(q+1,q+m+1);
  58. int pos=1;
  59. for(int i=1;i<=m;i++){
  60. while(pos<n&&g[pos].w<=q[i].w){
  61. merge(g[pos].u,g[pos].v);
  62. pos++;
  63. }
  64. ans[q[i].u]=res;
  65. }
  66. for(int i=1;i<=m;i++) cout<<ans[i]<<" \n"[i==m];
  67. #ifndef ONLINE_JUDGE
  68. cout<<endl;system("pause");
  69. #endif
  70. return 0;
  71. }

Codeforces Round #582 (Div. 3)-G. Path Queries-并查集的更多相关文章

  1. Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)

    题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...

  2. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  3. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  6. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  7. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  9. Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...

随机推荐

  1. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

  2. C# .NET 杀进程

    procName 是进程名,不带.exe . private bool IsAppKill(String procName) { try { ; System.Diagnostics.Process[ ...

  3. JMeter 脚本请求错误 HTTP Status 415 的解决

    然后在http请求上点击右键,添加配置元件-http信息头管理器,在信息头管理器上,添加一个参数,名称:Content-Type,值:application/json.然后在http请求上,conte ...

  4. java properties文件转义字符和中文乱码解决

    properties文件的分隔符是   =或者 : 第一次出现的就是分割符,第二次出现的也不需要转义,也即是(忽略掉[],只是着重描述字符) [\=]     [\:]   或者  [=]  [:] ...

  5. IIS清理缓存

    服务器突然断电经常会导致IIS中web项目运行不起来问题,各种报XXXX.dll加载失败,解决方法重新发布,如果重新发布也不行可能就是IIS缓存的问题了. 清理IIS缓存方法: 进入以下文件夹吧对应的 ...

  6. Word 带圈字符 1~20 快捷键的输入的技巧

    1. 前言 如何在Word中输入带圈数字?为大家分享一种快速录入带圈字符的方法,就是使用快捷键. 2. 输入带圈字符 1.在Word中输入2465,然后使用快捷键「ALT + X」就能变成⑥:输入24 ...

  7. DjangoRestful 递归嵌套序列化器实现

    **** 由于博客园不支持markdown语法,所以推荐以下链接阅读: 原创 https://blog.csdn.net/weixin_42495873/article/details/8943354 ...

  8. Django使用指南

    一.安装Django 1.命令行安装 pip3 install django(默认安装最新稳定版本) pip3 install django==版本号(指定版本安装) 2.Pycharm安装 在Pyc ...

  9. springboot2.0+mybatis多数据源集成

    最近在学springboot,把学的记录下来.主要有springboot2.0+mybatis多数据源集成,logback日志集成,springboot单元测试. 一.代码结构如下 二.pom.xml ...

  10. SQL Server2008导入导出数据库

    一.导出数据库 1.新建一个.bak的文本 右击数据库-->Tasks-->BackUp-->Remove原来的数据库-->Add后选择之前建立的.bak档 二.导入数据库 1 ...