CF 191C Fools and Roads lca 或者 树链剖分

树形结构与路径加权操作算法
本文介绍了一种解决树形结构中路径加权操作的算法,通过树链剖分和LCA(最低公共祖先)技术,实现对特定路径上边权的更新,并最终输出每条边的权重值。此算法适用于处理复杂树形结构中的路径更新问题。

They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost).

But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths.

A simple path is the path which goes through every Berland city not more than once.

The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it.

Note how the fools' paths are given in the input.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of cities.

Each of the next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ nui ≠ vi), that means that there is a road connecting cities ui and vi.

The next line contains integer k (0 ≤ k ≤ 105) — the number of pairs of fools who visit each other.

Next k lines contain two space-separated numbers. The i-th line (i > 0) contains numbers ai, bi (1 ≤ ai, bi ≤ n). That means that the fool number 2i - 1 lives in city ai and visits the fool number 2i, who lives in city bi. The given pairs describe simple paths, because between every pair of cities there is only one simple path.

Output

Print n - 1 integer. The integers should be separated by spaces. The i-th number should equal the number of fools who can go on the i-th road. The roads are numbered starting from one in the order, in which they occur in the input.

Sample test(s)
input
5
1 2
1 3
2 4
2 5
2
1 4
3 5
output
2 1 1 1 
input
5
3 4
4 5
1 4
2 4
3
2 3
1 3
3 5
output
3 1 1 1 
Note

In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.

In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1.

 

 

 

 

 

给出一棵树,树有边权,边权初始都为0,给出k个操作,每一个操作给出u v

表示u,v路径上的所有边权都加1

所有操作完后,按照边输入的顺序输出每一条边的边权

 

一看,树链剖分裸题

但是想想,其实是可以不用树链剖分做的

可以用lca

 

我们令root=1

dis[i]表示节点i到root的距离

 

对于每一个操作u v,相当于

dis[u]++

dis[v]++

dis[lca(u,v)]-=2

 

所有操作完后,我们再一次dfs(root),自底向上累加所有dis,就可以了

dis[u]+=dis[son(u)]

 

然后按照输入的顺序输出边权就好啦

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<set>

#define LL long lon
#define pb push_back

using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e5+5;

int dis[maxn];
int dep[maxn];
int dp[maxn][18];
int e[maxn][2];

struct Edge
{
    int to,next;
};
Edge edge[maxn<<1];
int head[maxn];
int tot;

void init_edge()
{
    memset(head,-1,sizeof head);
    tot=0;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void solve(int );

int main()
{
    init_edge();
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        scanf("%d %d",&e[i][0],&e[i][1]);
        addedge(e[i][0],e[i][1]);
        addedge(e[i][1],e[i][0]);
    }
    solve(n);

    return 0;
}

void dfs0(int u,int pre)
{
    dep[u]=dep[pre]+1;
    for(int i=head[u];~i;i=edge[i].next){
        int v=edge[i].to;
        if(v==pre)
            continue;
        dp[v][0]=u;
        dfs0(v,u);
    }
}

void init_dp(int n)
{
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i<=n;i++){
            if(dp[i][j-1]!=-1)
                dp[i][j]=dp[dp[i][j-1]][j-1];
        }
    }
}

int query_lca(int a,int b)
{
    if(dep[a]<dep[b])
        swap(a,b);
    int cnt;
    for(cnt=0;(1<<cnt)<=dep[a];cnt++)
        ;
    cnt--;
    for(int j=cnt;j>=0;j--){
        if(dep[a]-(1<<j)>=dep[b])
            a=dp[a][j];
    }
    if(a==b)
        return a;
    for(int j=cnt;j>=0;j--){
        if(dp[a][j]!=-1 && dp[a][j]!=dp[b][j]){
            a=dp[a][j];
            b=dp[b][j];
        }
    }
    return dp[a][0];
}

void dfs1(int u)
{
    for(int i=head[u];~i;i=edge[i].next){
        int v=edge[i].to;
        if(v==dp[u][0])
            continue;
        dfs1(v);
        dis[u]+=dis[v];
    }
}

void solve(int n)
{
    memset(dp,-1,sizeof dp);
    memset(dep,0,sizeof dep);
    memset(dis,0,sizeof dis);
    dfs0(1,1);
    init_dp(n);
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        dis[u]++;
        dis[v]++;
        dis[query_lca(u,v)]-=2;
    }
    dfs1(1);

    for(int i=1;i<n;i++){
        int cur=e[i][0];
        if(dep[e[i][0]]<dep[e[i][1]])
            cur=e[i][1];
        printf("%d",dis[cur]);
        i==n-1?puts(""):printf(" ");
    }
    return ;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/-maybe/p/4874320.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值