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】

/*
* @Author: Simon
* @Date: 2019-09-07 10:00:30
* @Last Modified by: Simon
* @Last Modified time: 2019-09-07 10:29:11
*/
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 200005
struct node{
int u,v,w;
node(){}
node(int u,int v,int w):u(u),v(v),w(w){}
bool operator <(const node&a)const{
return w<a.w;
}
}g[maxn],q[maxn];
int Set[maxn],Rank[maxn];
void init(int n){ //初始化
for(int i=0;i<=n;i++){
Set[i]=i;Rank[i]=1;
}
}
int cal(int x){ //计算联通块的贡献
return x*(x-1)/2;
}
int Find(int u){ //并查集的查找根节点
return Set[u]==u?u:Set[u]=Find(Set[u]);
}
int res=0;
int ans[maxn];
void merge(int u,int v){ //并查集的合并
u=Find(u),v=Find(v);
if(Rank[u]<Rank[v]) swap(u,v);
res-=cal(Rank[u]),res-=cal(Rank[v]); //减去u,v所在联通块的贡献
Set[v]=u;Rank[u]+=Rank[v]; //合并两个联通块
res+=cal(Rank[u]); //再加上合并后的联通块的贡献
}
Int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0); int n,m;cin>>n>>m;init(n);
for(int i=1;i<n;i++){
cin>>g[i].u>>g[i].v>>g[i].w;
}
sort(g+1,g+n);
for(int i=1;i<=m;i++){
cin>>q[i].w;q[i].u=i;
}
sort(q+1,q+m+1);
int pos=1;
for(int i=1;i<=m;i++){
while(pos<n&&g[pos].w<=q[i].w){
merge(g[pos].u,g[pos].v);
pos++;
}
ans[q[i].u]=res;
}
for(int i=1;i<=m;i++) cout<<ans[i]<<" \n"[i==m];
#ifndef ONLINE_JUDGE
cout<<endl;system("pause");
#endif
return 0;
}

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. ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    通过service mysql status 命令来查看mysql 的启动状态 报错如下: ERROR! MySQL is not running, but lock file (/var/lock/ ...

  2. Qt563x86vs2015.编译错误(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension(0x????????) is not a function)

    1.在 编译或打开 pro时 有时会有这个错误 1.1.参考网址:Qt 编译错误 提示TypeError_ Property 'asciify' of object Core__Internal__U ...

  3. English Learning -- 0611--When Burnout Is a Sign You Should Leave Your Job

    I like the following article from Harvard Business Review, as I ever experienced burnout at work. Ve ...

  4. Ubuntu 18.04 LTS 设置代理(系统代理;http 代理;sock5 代理;apt 代理 ...)

    1. 设置系统代理 1.1 设置 http 代理 1.1.1 只在当前 shell 生效 export http_proxy="http://<user>:<passwor ...

  5. php生成动态验证码 加减算法验证码 简单验证码

    预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...

  6. Linux基础-06-vi编辑器

    1. vi编辑器简介 1) vi的定义:vi是一个UNIX和Linux系统内嵌的标准正文(文字)编辑器,它是一种交互类型的正文编辑器,它可以用来创建和修改正文文件. 2. vi编辑器的操作模式 vi编 ...

  7. 作业练习P194,jieba应用,读取,分词,存储,生成词云,排序,保存

    import jieba #第一题 txt='Python是最有意思的编程语言' words=jieba.lcut(txt) #精确分词 words_all=jieba.lcut(txt,cut_al ...

  8. vue 等比例截图组件,支持缩放和旋转

    <template> <div class="crop-image" :style="wrapStyle"> <img :src= ...

  9. 利用 nodejs 解析 m3u8 格式文件,并下 ts 合并为 mp4

    利用 nodejs 解析 m3u8 格式文件,并下 ts 合并为 mp4 以前看视频的时候,直接找到 video标签,查看视频地址,然后下载下来.. 后来发现,好多 video 标签打开元素审查,如下 ...

  10. js复制内容到粘贴板

    点击右边内容:<span onclick="copyContent(this);" title="点击复制">啊,我被复制了</span> ...