• 题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\).

  • 题解:真的想不到用最小生成树来写啊....

    我们对边权排序,然后再对询问的\(q_i\)排序,我们可以枚举\(q_i\),然后从last开始遍历边权,如果边权不大于\(q_i\),那么就可以用并查集将两个连通块合并且计数(因为我们是从小到大枚举的,所以将它们合并并不会对后面有影响,反而还会方便我们计数),\(cnt\)表示连通块的节点数,合并时贡献为\(res=cnt[fu]*cnt[fv]\).

  • 代码:

    #define int long long
    
    struct misaka{
    int u,v,w;
    bool operator < (const misaka & mikoto) const{
    return w<mikoto.w;
    }
    }e[N]; struct query{
    int w;
    int id;
    bool operator < (const query & mikoto) const {
    return w<mikoto.w;
    }
    }q[N]; int n,m;
    int p[N];
    int cnt[N];
    int ans[N];
    int res; int find(int x){
    if(p[x]!=x) p[x]=find(p[x]);
    return p[x];
    } signed main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m; rep(i,1,n-1){
    cin>>e[i].u>>e[i].v>>e[i].w;
    } rep(i,1,n){
    p[i]=i;
    cnt[i]=1;
    } rep(i,1,m){
    cin>>q[i].w;
    q[i].id=i;
    } sort(e+1,e+n);
    sort(q+1,q+1+m); int last=1; rep(i,1,m){
    rep(j,last,n-1){
    if(e[j].w<=q[i].w){
    int u=e[j].u;
    int v=e[j].v;
    int fu=find(u);
    int fv=find(v);
    if(fu==fv) continue;
    res+=cnt[fv]*cnt[fu];
    cnt[fv]+=cnt[fu];
    cnt[fu]=0;
    p[fu]=fv;
    last++;
    }
    else break;
    }
    ans[q[i].id]=res;
    } rep(i,1,m) cout<<ans[i]<<' '; return 0;
    }

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

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

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

  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. 【Flutter】功能型组件之数据共享

    前言   InheritedWidget提供了一种数据在widget树中从上到下传递.共享的方式,例如在应用的根widget中通过InheritedWidget共享了一个数据,那么便可以在子widge ...

  2. 关联实现上-jsonpath取值

    举例子: demo01.py import jsonimport requestsimport jsonpathsession = requests.session()get_param_dict={ ...

  3. 【Oracle】迁移表到其他的表空间

    有些时候需要将表迁移到其他的表空间,在将表空间做相关的操作 下面是命令如何迁移表空间 SQL> alter table 表名 move tablespace 表空间名; 如果有很多的表想要迁移的 ...

  4. 转发:[服务器]SSL安装证书教程

    [服务器]SSL安装证书教程   来自阿里云教程 Tomcat服务器安装SSL证书 安装PFX格式证书 https://help.aliyun.com/document_detail/98576.ht ...

  5. 响应式编程库RxJava初探

    引子 在读 Hystrix 源码时,发现一些奇特的写法.稍作搜索,知道使用了最新流行的响应式编程库RxJava.那么响应式编程究竟是怎样的呢? 本文对响应式编程及 RxJava 库作一个初步的探索. ...

  6. 不用git 手动对比文件差异

    使用python脚本比较两个文件的差异内容并输出到html文档中,可以通过浏览器打开查看. 一.脚本使用 对比文件的差异 python python_diff_file.py -f1 web26.co ...

  7. python中json模块的使用

    Python自带json模块,它有loads.dumps.load和dump这4个功能,用于Json格式字符串和Python数据类型间进行转换. 一.json.loads() 把Json格式字符串解码 ...

  8. (12)-Python3之--openpyxl模块

    1.安装 pip install openpyxl 2.Excel操作的流程 1.打开excel,进入工作薄 workbook2.选择表单 Sheet 3.单元格  Cell4.读写操作 3.Exce ...

  9. HTTPS学习(二):原理与实践

    div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...

  10. C++学习之STL(二)String

    1.assign assign方法可以理解为先将原字符串清空,然后赋予新的值作替换. 返回类型为 string类型的引用.其常用的重载也有下列几种: a. string& assign ( c ...