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

本文详细介绍了一道经典的树链剖分题目,通过解决Berland城市间的道路与愚人访问问题,展示了树链剖分算法的应用及其实现过程。文章包括了完整的AC代码,为读者提供了深入理解该算法的机会。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值