zoukankan      html  css  js  c++  java
  • cf293E Close Vertices(树分治+BIT)

    E. Close Vertices

    You've got a weighted tree, consisting of n vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.

    Two vertices are close if there exists a path of length at most l between them and a path of weight at most w between them. Count the number of pairs of vertices v, u (v < u), such that vertices v and u are close.

    Input

    The first line contains three integers n, l and w (1 ≤ n ≤ 105, 1 ≤ l ≤ n, 0 ≤ w ≤ 109). The next n - 1 lines contain the descriptions of the tree edges. The i-th line contains two integers pi, wi (1 ≤ pi < (i + 1), 0 ≤ wi ≤ 104), that mean that the i-th edge connects vertex (i + 1) and pi and has weight wi.

    Consider the tree vertices indexed from 1 to n in some way.

    Output

    Print a single integer — the number of close pairs.

    Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Sample test(s)
    Input
    4 4 6
    1 3
    1 4
    1 3
    Output
    4
    Input
    6 2 17
    1 3
    2 5
    2 13
    1 6
    5 9
    Output
    9

    【思路】

           树分治。大体思路和这道题相似。

           不同的是有两个需要满足的条件,只需要把dis排序,扫描的同时用BIT维护dep的区间信息并统计答案即可。

    【代码】

      1 #include<map>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<vector>
      5 #include<iostream>
      6 #include<algorithm>
      7 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
      8 using namespace std;
      9 
     10 typedef long long LL;
     11 const int N = 1e5+10;
     12 const int INF = 1e9+10;
     13 struct Edge {
     14     int v,w; 
     15     Edge(int v=0,int w=0) :v(v),w(w){}
     16 };
     17 int n,W,L; LL  ans;
     18 int root,size,vis[N],siz[N],f[N],dis[N],dep[N],l1,l2;
     19 pair<int,int> list[N];
     20 vector<int> rec;
     21 vector<Edge> g[N];
     22 //BIT
     23 int C[N];
     24 void add(int x,int v) {
     25     while(x<=n) C[x]+=v,x+=x&(-x);
     26 }
     27 int query(int x) {
     28     int ans=0;
     29     while(x>0)  ans+=C[x],x-=x&(-x);
     30     return ans;
     31 }
     32 //fenzhi
     33 void getroot(int u,int fa) {
     34     siz[u]=1; f[u]=0;
     35     for(int i=0;i<g[u].size();i++) {
     36         int v=g[u][i].v;
     37         if(v!=fa && !vis[v]) {
     38             getroot(v,u);
     39             siz[u]+=siz[v];
     40             f[u]=max(f[u],siz[v]);
     41         }
     42     }
     43     f[u]=max(f[u],size-siz[u]);
     44     if(f[u]<f[root]) root=u;
     45 }
     46 void getdis(int u,int fa) {
     47     list[++l1]=make_pair(dis[u],dep[u]);
     48     for(int i=0;i<g[u].size();i++) {
     49         int v=g[u][i].v;
     50         if(v!=fa && !vis[v]) {
     51             dep[v]=dep[u]+1;
     52             dis[v]=dis[u]+g[u][i].w;
     53             getdis(v,u);
     54         }
     55     }
     56 }
     57 LL getans(int l,int r) {
     58     sort(list+l,list+r+1);
     59     LL res=0; int j=l;
     60     for(int i=r;i>=l;i--) {
     61         while(j<=r && list[i].first+list[j].first<=W) {
     62             add(list[j].second,1);
     63             rec.push_back(list[j].second);
     64             j++;
     65         }
     66         if(list[i].first*2<=W && list[i].second*2<=L) res--;
     67         res+=(LL)query(L-list[i].second);
     68     }
     69     return res/2;
     70 }
     71 void clear() {
     72     for(int i=0;i<rec.size();i++) add(rec[i],-1);
     73     rec.clear();
     74 }
     75 void solve(int u) {
     76     vis[u]=1; l1=l2=0;
     77     LL S1=0,S2=0;
     78     for(int i=(int)g[u].size()-1;i>=0;i--) {
     79         int v=g[u][i].v;
     80         if(!vis[v]) {
     81             l2=l1+1;
     82             dep[v]=1; dis[v]=g[u][i].w;
     83             getdis(v,u);
     84             clear();
     85             S1+=getans(l2,l1);
     86         }
     87     }
     88     FOR(i,1,l1)                                                //AT:根为终点
     89         if(list[i].first<=W && list[i].second<=L) S2++;
     90     clear();                                                //AT:clear
     91     S2+=getans(1,l1);
     92     ans=ans+S2-S1;
     93     for(int i=(int)g[u].size()-1;i>=0;i--) {
     94         int v=g[u][i].v;
     95         if(!vis[v]) {
     96             size=siz[v]; root=0;
     97             getroot(v,-1); solve(root);
     98         }
     99     }
    100 }
    101 
    102 void read(int& x) {
    103     char c=getchar(); int f=1; x=0;
    104     while(!isdigit(c)) {if(c=='-')f=-1;c=getchar();} 
    105     while(isdigit(c)) x=x*10+c-'0',c=getchar();
    106     x*=f; 
    107 }
    108 int main() {
    109     //freopen("in.in","r",stdin);
    110     //freopen("out.out","w",stdout);
    111     read(n),read(L),read(W);
    112     int u,v,w;
    113     FOR(i,2,n) {
    114         read(u),read(v),read(w);
    115         g[u].push_back(Edge(v,w));
    116         g[v].push_back(Edge(u,w));
    117     }
    118     root=0,f[0]=INF,size=n;
    119     getroot(1,-1); solve(root);
    120     cout<<ans;
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    WPF样式之画刷结合样式
    年度巨献-WPF项目开发过程中WPF小知识点汇总(原创+摘抄)
    30分钟学会XAML
    spring注解之@Import注解的三种使用方式
    大白话讲解Spring的@bean注解
    SpringBoot中的五种对静态资源的映射规则
    Spring注解之@Autowired、@Qualifier、@Resource、@Value
    Spring中如何使用工厂模式实现程序解耦?
    Spring注解之@Component、@Controller、@Service、@Repository
    Spring整合Redis
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5190448.html
Copyright © 2011-2022 走看看