codeforces1213G Path Queries 并查集

题意

给定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;
}

原文地址:https://www.cnblogs.com/xyq0220/p/11729564.html

时间: 2024-10-04 03:52:28

codeforces1213G Path Queries 并查集的相关文章

HDU5883 The Best Path(并查集+欧拉路)

题意: n个点m条边,问m条边构成的是否为欧拉路. 是的话输出路径上所有点的异或和,每个点经过几次异或几次. 思路: 先用并查集判断是否连通,然后如果是欧拉路的话有两种情况 如果奇数度节点有2个,就枚举这两个点做起点,选大的 如果都为偶数度节点,就枚举n个起点,选大的 /* *********************************************** Author :devil ************************************************ *

zoj3583Simple Path【并查集(思想很好)】

大意: 告诉你一个无向图 然后定义一个simple path是一条路径上面不包含重复的点 然后告诉你两个点s, t 问有多上个点是不在s到t的simple路径上 分析: 对于从s到t的simple path上  无论删除其他的任何一个 点   那么这个点一定是要么和s相连,要么和t相连 从这个角度出发的话 如果删除任何一点 如果有个点突然不和s,或t相连了 那么改点也就一定不是该simple path上的点 最后统计一下个数即可 代码: 1 #include <iostream> 2 #inc

1053 Path of Equal Weight (30分)(并查集)

Given a non-empty tree with root R, and with weight W?i?? assigned to each tree node T?i??. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L. Now given any weighted tre

hdu 5441 并查集

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2331    Accepted Submission(s): 804 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

【POJ 1984】Navigation Nightmare(带权并查集)

Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 100

poj1984 带权并查集(向量处理)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2102 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

POJ 1984 - Navigation Nightmare - [带权并查集]

题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000)

【杭电OJ3938】【离线+并查集】

http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1921    Accepted Submission(s): 955 Problem Description ZLGG found a magic theory that the b