CF191C Fools and Roads - 树剖解法

Codeforces Round #121 (Div. 1) C. Fools and Roads

time limit per test :2 seconds

memory limit per test : 256 megabytes

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 ≤ n, ui ≠ vi), that means that there is a road connecting cities *u**i* and *v**i*.

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 *a**i* and visits the fool number 2i, who lives in city *b**i*. 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.

Examples

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.

题目大意

给你一棵树,然后给你k个操作,每次操作输入两个整数a b

表示从a 走到b的边的权值都加1

一开始所有权值都为0

最后输出每条边的权值, 按照边输入的顺序

n <= 10^5

Solution

显然树链剖分可做

把边权该做下面的(dep深)的点权。

这样就处理好了

然后在跑一下树链剖分,注意公共祖先不能赋值。

在做的过程中注意边的编号要记录

然后就好了

code

#include<bits/stdc++.h>
#define DEBUG cerr << "Call out at function: " << __func__ << ", In line: " << __LINE__ << " --- "
using namespace std;
vector <int> f[110000];
vector <int> g[110000];
int n;
int w[110000];
int son[110000];
int seg[110000];int pl;
int rev[110000];
int dep[110000];
int top[110000];
int fa[110000];
int id[110000];

long long C[110000];

inline int lowbit(int x){
    return x & (-x);
}

void add(int x,long long v){
    while (x > 0) C[x] += v, x -= lowbit(x);
}

long long query(int x){
    long long ret = 0;
    while (x <= n) ret += C[x], x += lowbit(x);
    return ret;
}

int DFS1(int fat,int x)
{
    fa[x] = fat;
    w[x] = 1;
    dep[x] = dep[fat] + 1;
    int MAX = 0;
    for (int i=0;i<f[x].size();i++)
        if (f[x][i] != fat){
            id[g[x][i]] = f[x][i];
            int tmp = DFS1(x,f[x][i]);
            w[x] += tmp;
            if (tmp > MAX)
                son[x] = f[x][i], MAX = tmp;
        }
    return w[x];
}

void DFS2(int x){
    seg[x] = ++pl;
    rev[pl] = x;
    if (son[x] == 0) return;
    top[son[x]] = top[x];
    DFS2(son[x]);
    for (int i=0;i<f[x].size();i++){
        if (f[x][i] != son[x] && f[x][i] != fa[x])
            top[f[x][i]] = f[x][i], DFS2(f[x][i]);
    }
}

int add(int x,int y,long long val){
    while (top[x] != top[y]){
        if (dep[top[x]] < dep[top[y]])
            swap(x,y);
        add(seg[x],1);
        add(seg[top[x]]-1,-1);
        x = fa[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x,y);
    add(seg[y],1);
    add(seg[x],-1);
}

int main()
{
    cin >> n;
    for (int i=1;i<n;i++){
        int tp1,tp2;
        cin >> tp1 >> tp2;
        f[tp1].push_back(tp2);
        f[tp2].push_back(tp1);
        g[tp1].push_back(i);
        g[tp2].push_back(i);
    }
    DFS1(-1,1);
    top[1] = 1,DFS2(1);
    int m;
    cin >> m;
    for (int i=1;i<=m;i++){
        int tp1,tp2;
        cin >> tp1 >> tp2;
        add(tp1,tp2,1);
    }
    for (int i=1;i<=n-1;i++)
        cout << query(seg[id[i]]) << ' ';
}

转载于:https://www.cnblogs.com/dgklr/p/11217707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值