天天看點

Codeforces 192E Fools and Roads【樹鍊剖分】

E. Fools and Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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 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.

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.

題目大意:

給出一個N個點的一顆樹,有Q次操作,每次操作表示從x到y的路徑上,每個邊權都加1.

問最終所有操作都結束之後,每個邊的邊權。

思路:

(上古時代的CF,E題竟然是闆子題)

裸樹鍊剖分,直接将邊權壓到點權上去搞就行了。

Ac代碼:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int>mp[150000];
int n;
/*************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[150000*4];
int flag[150000*4];
void pushup(int rt)
{
    tree[rt]=tree[rt*2]+tree[rt*2+1];
}
void pushdown(int l,int r,int rt)
{
    if(flag[rt])
    {
        int m=(l+r)/2;
        flag[rt*2]+=flag[rt];
        flag[rt*2+1]+=flag[rt];
        tree[rt*2]+=(m-l+1)*flag[rt];
        tree[rt*2+1]+=(r-(m+1)+1)*flag[rt];
        flag[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt]=0;flag[rt]=0;
    if(l==r)
    {
        tree[rt]=0;
        return ;
    }
    int m=(l+r)/2;
    build(lson);build(rson);pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        tree[rt]+=(l-r+1)*c;
        flag[rt]+=c;
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)update(L,R,c,lson);
    if(R>m)update(L,R,c,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return tree[rt];
    }
    int ans=0;
    pushdown(l,r,rt);
    int m=(l+r)/2;
    if(L<=m)ans+=query(L,R,lson);
    if(R>m)ans+=query(L,R,rson);
    pushup(rt);
    return ans;
}
/*************************/
int cnt;
int depth[150000];
int son[150000];
int size[150000];
int fa[150000];
int dfn[150000];
int Top[150000];
void Dfs(int u,int from,int d)
{
    fa[u]=from,depth[u]=d,son[u]=-1,size[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,d+1);
        size[u]+=size[v];
        if(son[u]==-1||size[v]>size[son[u]])son[u]=v;
    }
}
void Dfs2(int u,int from,int top)
{
    Top[u]=top;dfn[u]=++cnt;
    if(son[u]!=-1)
    {
        Dfs2(son[u],u,top);
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from||v==son[u])continue;
        Dfs2(v,u,v);
    }
}
void Slove(int x,int y)
{
    int fx=Top[x],fy=Top[y];
    while(fx!=fy)
    {
        if(depth[fx]<depth[fy])
        {
            swap(x,y);swap(fx,fy);
        }
        update(dfn[fx],dfn[x],1,1,n,1);
        x=fa[fx],fx=Top[x];
    }
    if(x==y)return ;
    if(depth[x]<depth[y])swap(x,y);
    update(dfn[son[y]],dfn[x],1,1,n,1);
}
/*************************/
int xx[250000],yy[250000];
int num[250000];
int main()
{
    while(~scanf("%d",&n))
    {
        build(1,n,1);
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&xx[i],&yy[i]);
            mp[xx[i]].push_back(yy[i]);
            mp[yy[i]].push_back(xx[i]);
        }
        cnt=0;
        Dfs(1,-1,1);
        Dfs2(1,-1,1);
        for(int i=1;i<=n-1;i++)
        {
            if(depth[xx[i]]<depth[yy[i]])swap(xx[i],yy[i]);
            num[i]=dfn[xx[i]];
        }
        int q;scanf("%d",&q);
        while(q--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            Slove(x,y);
        }
        for(int i=1;i<=n-1;i++)
        {
            printf("%d ",query(num[i],num[i],1,n,1));
        }
        printf("\n");
    }
}