zoukankan      html  css  js  c++  java
  • Path Queries

    G. Path Queries
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a weighted tree consisting of nn vertices. Recall that a tree is a connected graph without cycles. Vertices uiui and vivi are connected by an edge with weight wiwi.

    You are given mm queries. The ii-th query is given as an integer qiqi. In this query you need to calculate the number of pairs of vertices (u,v)(u,v)(u<vu<v) such that the maximum weight of an edge on a simple path between uu and vv doesn't exceed qiqi.

    Input

    The first line of the input contains two integers nn and mm (1n,m21051≤n,m≤2⋅105) — the number of vertices in the tree and the number of queries.

    Each of the next n1n−1 lines describes an edge of the tree. Edge ii is denoted by three integers uiui, vivi and wiwi — the labels of vertices it connects (1ui,vin1≤ui,vi≤n, uiviui≠vi) and the weight of the edge (1wi21051≤wi≤2⋅105). It is guaranteed that the given edges form a tree.

    The last line of the input contains mm integers q1,q2,,qmq1,q2,…,qm (1qi21051≤qi≤2⋅105), where qiqi is the maximum weight of an edge in the ii-th query.

    Output

    Print mm integers — the answers to the queries. The ii-th value should be equal to the number of pairs of vertices (u,v)(u,v) (u<vu<v) such that the maximum weight of an edge on a simple path between uu and vv doesn't exceed qiqi.

    Queries are numbered from 11 to mm in the order of the input.

    Examples
    input
    Copy
    7 5
    1 2 1
    3 2 3
    2 4 1
    4 5 2
    5 7 4
    3 6 2
    5 2 3 4 1
    
    output
    Copy
    21 7 15 21 3 
    
    input
    Copy
    1 2
    1 2
    
    output
    Copy
    0 0 
    
    input
    Copy
    3 3
    1 2 1
    2 3 2
    1 3 2
    
    output
    Copy
    1 3 3 
    
    Note

    The picture shows the tree from the first example:

    #pragma GCC optimize(2)
    
    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn = 1e6 + 108;
    typedef long long ll;
    struct atom{
        int u,v,w;
        bool operator<(const atom& cur)const {
            return w<cur.w;
        }
    }o[maxn];
    int n,m,fa[maxn];
    ll num[maxn],res[maxn];
    
    inline int getfa(int cur){
        return cur==fa[cur]?cur:fa[cur]=getfa(fa[cur]);
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt", "r", stdin);
    #endif
        scanf("%d%d",&n,&m);
        for(register int i=1;i<=n;++i){
            fa[i]=i;
            num[i]=1;
        }
        for(register int i=1;i<n;++i){
            scanf("%d%d%d",&o[i].u,&o[i].v,&o[i].w);
        }
        sort(o+1,o+n);
        for(register int i=1;i<n;++i){
            int fu=getfa(o[i].u);
            int fv=getfa(o[i].v);
            res[o[i].w]+=num[fu]*num[fv];
            num[fu]+=num[fv];
            fa[fv]=fu;
        }
        for(register int i=1;i<=360000;++i){
            res[i]=res[i]+res[i-1];
            //printf("debug res[%d] = %d
    ",i,res[i]);
        }
        int query;
        while(m--){
            scanf("%d",&query);
            printf("%lld ",res[query]);
        }
        return 0;
    }
  • 相关阅读:
    FasDfs缩略图解决方案 -- Linux
    FastDFS 配置 Nginx 模块,并实现分布式同步-Linux
    Linux简单文本处理
    Linux命令执行顺序与管道命令
    建立Linux计划命令crontab
    Linux下的帮助命令
    Linux文件系统操作与磁盘管理
    Linux之文件的压缩与解压缩
    Linux环境变量与文件查找
    Linux目录结构及文件操作
  • 原文地址:https://www.cnblogs.com/czy-power/p/11455941.html
Copyright © 2011-2022 走看看