zoukankan      html  css  js  c++  java
  • Path Queries CF

    You are given a weighted tree consisting of n vertices. Recall that a tree is a connected graph without cycles. Vertices ui and vi are connected by an edge with weight wi.

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

    题意: 给出一个数问都多少个二元组满足 之间的最大边不超过这个数的


    这个显然满足单调性
    所以我们考虑离线 由小到大 加边
    答案的贡献为 每个联通 块 size size*(size-1)/2;
    并查集维护联通块的点的个数

    //
    #include<bits/stdc++.h>
    using namespace std;
    #define maxnn 300000
    #define ll long long
    ll n,m;
    struct node 
    {
     
        ll st,en;
        ll val;
    }edge[maxnn];
    bool cmp(node a,node b)
    {
        return a.val<b.val;
    }
    ll tot=0;
     
    struct gr{
     
        ll id,va;
     
    }gra[maxnn];
    bool cmp1(gr a,gr b)
    {
        return a.va<b.va;
    }
    ll f[maxnn];
    ll ask[maxnn];
    ll siz[maxnn];
    ll gf(ll v)
    {
        return f[v]==v?v:f[v]=gf(f[v]);
    }
    int main()
    {
        cin>>n>>m;
        ll x,y,z;
        for(int i=1;i<n;i++)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            edge[++tot].st=x;
            edge[tot].en=y;
            edge[tot].val=z;
        }
        sort(edge+1,edge+1+tot,cmp);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&gra[i].va);
            gra[i].id=i;
        }
        sort(gra+1,gra+1+m,cmp1);
        ll sec=0;
    	ll j=1;
    	ll tmp=0; 
        for(int i=1;i<=n;i++) f[i]=i,siz[i]=1;
        for(int i=1;i<=m;i++)
        {
            int tr=gra[i].va;
            for(;j<=tot;j++)
            {
                if(edge[j].val<=tr)
                {
                    if(gf(edge[j].st)!=gf(edge[j].en))
                    {		
    						tmp-=(siz[gf(edge[j].en)]*(siz[gf(edge[j].en)]-1)/2);
    						tmp-=(siz[gf(edge[j].st)]*(siz[gf(edge[j].st)]-1)/2);
                            siz[gf(edge[j].en)]+=siz[gf(edge[j].st)];
    						tmp+=(siz[gf(edge[j].en)]*(siz[gf(edge[j].en)]-1)/2);
                            f[gf(edge[j].st)]=gf(edge[j].en);
                    }
                }
    			else break;
            }
            j--;
     
                    ask[gra[i].id]=tmp;
      
        }
         for(int i=1;i<=m;i++)
         {  
            cout<<ask[i]<<" ";
         }
    }
    
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    C. Dima and Salad 背包好题
    centos7下查看cup核数
    code码说明
    数据库慢查询
    centos7重启Mysql命令
    many connection errors,更改max_connection_errors的值
    CentOS7 linux下yum安装redis以及使用
    django Warning: (3135, "'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes
    linux命令
    linux命令解压压缩rar文件的详细步骤
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11446212.html
Copyright © 2011-2022 走看看