P1600 every day love running (lca + tree + trees difference barrel is open)

P1600 love running every day

analysis:

First, if you want to contribute statistics for each individual route to his observation point, apparently difficult to optimize to log in.

Consideration of each observation point statistical answer.

Such observation point is through two routes:

1. The starting point below it, from the beginning to go up through it.

2. end below it, from the beginning to go down somewhere through it.

In the first case, we want to satisfy: DEP [s] == DEP [x] + W [x] (x is an observation point, that is to say below the point x s, over W [x] x reaches exactly seconds point)

For the second case, we have to satisfy: Tim = DEP [s] + DEP [t] - DEP * 2 [LC] (from s to t elapsed time) Tim - (DEP [t] - DEP [X]) W == [x] (the overall time spent - x down from time t to spend, x is the time it takes to go from s)

The second simplification click: DEP [S] - DEP * 2 [LC] == W [X] - DEP [X]

In both cases only the latter part of the observation point related to a subtree when the observation point x, there are two equations will satisfy the above ans [x] ++.

So we just need to open a barrel record equation top left click.

But in the end there will be the starting point at x, x, from start to finish did not go through, how to do it?

A tree difference!

For the first bucket in the fa [lc] minus the contribution can be.

For the second, minus the bucket on lc contribution. (Why not fa [lc]: lc has been attributed in the first case inside, in the second half of the path is not included in the lc !!)

But there is a problem: when statistics sub-tree of them share a single bucket, duplication of information how to do?

Let u of two child nodes v1 and v2.

After traversing when v1, v1 bucket retain the information and all of its child nodes.

Then we traverse v2, v2 subtree information is also added to the bucket, then when the statistics v2 will contribute significantly to the statistics v1 sub-tree, how to do it?

Join only in the sub-tree v2 before recording the original look, then added two after the subtraction can be.

 

#include<bits/stdc++.h>
using namespace std;
#define N 300005
#define ri register int
vector<int> a1[N],a2[N],b1[N*3],b2[N*3],e[N];
int sum1[N<<1],sum2[N<<1],fa[N][22],w[N],n,m,ans[N],dep[N];
void dfs1(int u,int ff)
{
    for(ri i=0;i<e[u].size();++i){
        int v=e[u][i];
        if(v==ff) continue;
        fa[v][0]=u; dep[v]=dep[u]+1;
        for(ri j=1;j<=20;++j) fa[v][j]=fa[fa[v][j-1]][j-1];
        dfs1(v,u);
    }
}
int lca(int a,int b)
{
    if(dep[a]<dep[b]) swap(a,b);
    for(ri i=20;i>=0;--i) if(dep[fa[a][i]]>=dep[b]) a=fa[a][i];
    if(a==b) return a;
    for(ri i=20;i>=0;--i) if(fa[a][i]!=fa[b][i]) a=fa[a][i],b=fa[b][i];
    return fa[a][0];
}
void dfs2(int u,int ff)
{
    int tmp1=sum1[dep[u]+w[u]],tmp2=sum2[w[u]-dep[u] +n];
    for(ri i=0;i<e[u].size();++i) if(e[u][i]!=ff) dfs2(e[u][i],u);
    for(ri i=0;i<a1[u].size();++i) sum1[a1[u][i]]++;
    for(ri i=0;i<a2[u].size();++i) sum1[a2[u][i]]--;
    for(ri i=0;i<b1[u].size();++i) sum2[b1[u][i]]++;
    for(ri i=0;i<b2[u].size();++i) sum2[b2[u][i]]--;
    ans[u]=sum1[dep[u]+w[u]]-tmp1 + sum2[w[u]-dep[u] +n]-tmp2;
}
int main()
{
    scanf("%d%d",&n,&m);
    int a,b;
    for(ri i=1;i<=n-1;++i) scanf("%d%d",&a,&b),e[a].push_back(b),e[b].push_back(a);
    dep[0]=-1; dep[1] = 0 ; DFS1 ( . 1 , 0 );
     for (I = RI . 1 ; I <= n-; I ++) Scanf ( " % D " , & W [I]);
     for (I = RI . 1 ; I < m =; ++ I) {
         int S, T; 
        Scanf ( " % D% D " , & S, & T);
         int LC = LCA (S, T); 
        A1 [S] .push_back (DEP [S]) ; // A1 is added to the tub, when the recording mark, and then release the DFS 
        A2 [FA [LC] [ 0 ]] push_back (DEP [S]);. // A2 is subtracted bucket 
        b1 [t] .push_back ( DEP [S] - 2*dep[lc] +n);//防止越界 
        b2[lc].push_back(dep[s]-2*dep[lc] +n);
    }
    dfs2(1,0);
    for(ri i=1;i<=n;++i) printf("%d ",ans[i]);
}
/*
6 3
2 3
1 2 
1 4 
4 5 
4 6 
0 2 5 1 2 3 
1 5 
1 3 
2 6 

5 3
1 2 
2 3 
2 4 
1 5 
0 1 0 3 0 
1 4
3 1 
5 5 
*/
View Code

 

 

 

 

Guess you like

Origin www.cnblogs.com/mowanying/p/11808536.html
Recommended