Codeforce-191C-Fools and Roads (树链剖分 更新边权)

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


题意:给你一个无向图,然后人可以从一个点走到另一个点,问最后每条路呗走了几次;

思路:树链剖分裸题,维护边权,然后按输入顺序输出每条边的次数

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<sstream>
#define maxn 150000
using namespace std;
int n,k,tot,num;
struct E{int x;int y;}e[maxn];
struct Edge{int to;int next;}edge[maxn*2];
int dep[maxn],son[maxn],sz[maxn],fa[maxn],id[maxn],top[maxn],head[maxn];
void add_edge(int u,int v)
{
    edge[++tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot;
}
void dfs1(int u,int f,int deep)
{
    dep[u]=deep;
    sz[u]=1;
    son[u]=0;
    fa[u]=f;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==f)continue;
        dfs1(ff,u,deep+1);
        sz[u]+=sz[ff];
        if(sz[son[u]]<sz[ff])
            son[u]=ff;
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=++num;
    if(son[u])dfs2(son[u],tp);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int ff=edge[i].to;
        if(ff==fa[u]||ff==son[u])continue;
        dfs2(ff,ff);
    }
}struct node
{
    int l;
    int r;
    int tag;
}tree[maxn*4];
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].tag=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}
void pushdown(int root)
{
    if(tree[root].tag)
    {
        tree[root<<1].tag+=tree[root].tag;
        tree[root<<1|1].tag+=tree[root].tag;
        tree[root].tag=0;
    }
}
void update(int root,int l,int r,int val)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(l<=ll&&rr<=r)
    {
        tree[root].tag+=val;
        return;
    }
    pushdown(root);
    int mid=(ll+rr)>>1;
    if(l<=mid) update(root<<1,l,r,val);
    if(r>mid) update(root<<1|1,l,r,val);
}
int query(int root,int p)
{
    int ll=tree[root].l;
    int rr=tree[root].r;
    if(ll==rr)
        return tree[root].tag;
    pushdown(root);
    int mid=(ll+rr)>>1;
    if(p<=mid)
        return query(root<<1,p);
    else
        return query(root<<1|1,p);
}
void Yougth(int u,int v)
{
    int tp1=top[u];
    int tp2=top[v];
    while(tp1!=tp2)
    {
        if(dep[tp1]<dep[tp2])
        {
            swap(u,v);
            swap(tp1,tp2);
        }
        update(1,id[tp1],id[u],1);
        u=fa[tp1];
        tp1=top[u];
    }
    if(u==v) return;
    if(dep[u]>dep[v]) swap(u,v);
    update(1,id[son[u]],id[v],1);
}
void init()
{
    tot=num=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        int x,y;
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&e[i].x,&e[i].y);
            add_edge(e[i].x,e[i].y);
            add_edge(e[i].y,e[i].x);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=1;i<n;i++)
        {
            if(dep[e[i].x]>dep[e[i].y])
                swap(e[i].x,e[i].y);
        }
        build(1,1,n);
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d",&x,&y);
            Yougth(x,y);
        }
        for(int i=1;i<n;i++)
            printf("%d%c",query(1,id[e[i].y]),i==n-1?'\n':' ');
    }
    return 0;
}


发布了76 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/78195915
今日推荐