Codeforces 192E Fools and Roads【树链剖分】

针对经典CF E题,采用树链剖分算法解决边权更新问题。题目要求对一棵树上的路径进行多次操作,更新路径上的边权,并求最终每条边的权值。

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

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

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





### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值