题意

给定n个结点的树,每条边有边权,有m个询问,每个询问给一个\(q_i\)输出树上有多少点对的简单路径上最大的边权不超过\(q_i\)。

分析

用并查集维护点集,同时维护大小。

将所有边按边权排序,考虑每次从小到大加边,图中经过当前边的所有路径一定是以当前边的边权为最大值的,用并查集维护下图中每个联通块的大小,经过当前边的路径数即为\(sz[find(u)]*sz[find(v)]\)。然后前缀和一下就可以\(O(1)\)询问了。

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e5+10;
int n,m;
int f[maxn];
ll ans[maxn],sz[maxn];
int find(int k){
if(k==f[k]) return k;
else return f[k]=find(f[k]);
}
struct ppo{
int u,v,w;
bool operator<(const ppo &r) const{
return w<r.w;
}
}a[maxn];
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) f[i]=i,sz[i]=1;
for(int i=1;i<n;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
sort(a+1,a+n);
for(int i=1;i<n;i++){
int rx=find(a[i].u),ry=find(a[i].v);
ans[a[i].w]+=sz[rx]*sz[ry];
f[rx]=ry;
sz[ry]+=sz[rx];
}
for(int i=1;i<=2e5;i++) ans[i]+=ans[i-1];
while(m--){
int x;scanf("%d",&x);
printf("%lld ",ans[x]);
}
return 0;
}

codeforces1213G Path Queries 并查集的更多相关文章

  1. CodeForces - Path Queries (并查集+离线查询)

    题目:https://vjudge.net/contest/323699#problem/A 题意:给你一棵树,然后有m个查询,每次查询问一条路径最大边小于给定查询的数量 思路:首先我们看到,我们其实 ...

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

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

  3. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

    传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...

  4. UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)

    给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...

  5. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  6. Codeforces 938G Shortest Path Queries [分治,线性基,并查集]

    洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再 ...

  7. HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. 关于并查集的路径压缩(Path Compress)优化

    之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...

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

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

随机推荐

  1. 工欲善其事必先利其器之Vagrant + VirtualBox

    Vagrant 可以管理 VirtualBox,实现快速配置创建虚拟机 1.下载软件 VirtualBox虚拟机下载地址:https://www.virtualbox.org/wiki/Downloa ...

  2. XML-RPC-1概述

    XML-RPC是一个远程过程调用(远端程序呼叫)(remote procedure call,RPC)的分布式计算协议,通过XML将调用函数封装,并使用HTTP协议作为传送机制.   中文名 XML- ...

  3. JS基础 sessionStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  4. leetcode-62. Unique Paths · DP + vector

    题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  5. 【微信网页直接下载app】微信跳转-微信浏览器中直接唤起本地浏览器和App

    文档传送门:https://github.com/EthanOrange/wechat-redirect demo: http://wxredirect.jslab.fun/call-app

  6. 我用Python爬虫挣钱的那点事

    在下写了10年Python,期间写了各种奇葩爬虫,挣各种奇葩的钱,写这篇文章总结下几种爬虫挣钱的方式. 1.最典型的就是找爬虫外包活儿.这个真是体力活,最早是在国外各个freelancer网站上找适合 ...

  7. opencv+python 添加文字 cv2.putText

    import cv2 img = cv2.imread('E:\\usb_test\\example\\yolov3\\rknn_emotion\\test_images\\llj5.jpg') fo ...

  8. FormCollection使用

    FormCollection用来在controller中获取页面表单元素的数据.它是表单元素的集合,包括<input type="submit" />元素. 用法举例: ...

  9. Golang 本身是用什么语言写的?

    原文:https://www.zhihu.com/question/66944175 ------------------------------- 首先,问题的说法是有问题的. golang本身是用 ...

  10. vscode远程修改文件('file': A system error occured )

    The command you want is :e (short for :edit). If you use :edit! it will discard local changes and re ...