HDU5044 Tree(树链剖分)

来源:互联网 发布:快啊晒密软件 编辑:程序博客网 时间:2024/06/14 06:50

Tree

传送门1
传送门2
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N.

There are N - 1 edges numbered from 1 to N - 1.

Each node has a value and each edge has a value. The initial value is 0.

There are two kind of operation as follows:

● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.

● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.

After finished M operation on the tree, please output the value of each node and edge.

Input

The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),denoting the number of nodes and operations, respectively.

The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.

For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -10 5 ≤ k ≤ 10 5)

Output

For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.

The second line contains N integer which means the value of each node.

The third line contains N - 1 integer which means the value of each edge according to the input order.

Sample Input

2
4 2
1 2
2 3
2 4
ADD1 1 4 1
ADD2 3 4 2
4 2
1 2
2 3
1 4
ADD1 1 4 5
ADD2 3 2 4

Sample Output

Case #1:
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0


题意

一棵树n个节点,有两种操作
1. 将u到v之间点权加k;
2. 将u到v之间边权加k;
输出修改后的点权和边权。

分析

树链剖分+差分

CODE

#include<cstdio>#include<memory.h>#include<algorithm>#define N 100005using namespace std;struct node {    int to,next,id;} edge[N<<1];int head[N],tot=1;int dep[N],sz[N],fa[N],son[N],tot2,top[N];int sgID[N],pos[N];int n;void add(int a,int b,int id) {    edge[tot]=(node) {b,head[a],id},head[a]=tot++;}void dfs1(int x) {    int k=0;    sz[x]=1;    dep[x]=dep[fa[x]]+1;    for(int i=head[x]; ~i; i=edge[i].next) {        int y=edge[i].to;        if(y==fa[x])continue;        fa[y]=x;        pos[y]=edge[i].id;        dfs1(y);        sz[x]+=sz[y];        if(sz[y]>sz[k])k=y;    }    if(k)son[x]=k;}void dfs2(int x,int tp) {    top[x]=tp;    sgID[x]=++tot2;    if(son[x])dfs2(son[x],tp);    for(int i=head[x]; ~i; i=edge[i].next) {        int y=edge[i].to;        if(y==fa[x]||y==son[x])continue;        dfs2(y,y);    }}long long sumedge[N],sumpoint[N];void updatepoint(int x,int y,int k) {    while(top[x]!=top[y]) {        if(dep[top[x]]<dep[top[y]]) {            sumpoint[sgID[top[y]]]+=k;            sumpoint[sgID[y]+1]-=k;            y=fa[top[y]];        } else {            sumpoint[sgID[top[x]]]+=k;            sumpoint[sgID[x]+1]-=k;            x=fa[top[x]];        }    }    if(dep[x]<dep[y]) {        sumpoint[sgID[x]]+=k;        sumpoint[sgID[y]+1]-=k;    } else {        sumpoint[sgID[y]]+=k;        sumpoint[sgID[x]+1]-=k;    }}void updatedge(int x,int y,int k) {    while(top[x]!=top[y]) {        if(dep[top[x]]<dep[top[y]]) {            sumedge[sgID[top[y]]]+=k;            sumedge[sgID[y]+1]-=k;            y=fa[top[y]];        } else {            sumedge[sgID[top[x]]]+=k;            sumedge[sgID[x]+1]-=k;            x=fa[top[x]];        }    }    if(dep[x]>dep[y]) {        sumedge[sgID[y]+1]+=k;        sumedge[sgID[x]+1]-=k;    } else {        sumedge[sgID[x]+1]+=k;        sumedge[sgID[y]+1]-=k;    }}long long f[N];int main() {    int T,m;    scanf("%d",&T);    for(int cas=1; cas<=T; cas++) {        printf("Case #%d:\n",cas);        scanf("%d%d",&n,&m);        tot=1;        tot2=0;        memset(sumpoint,0,sizeof sumpoint);        memset(sumedge,0,sizeof sumedge);        memset(head,-1,sizeof head);        memset(son,0,sizeof son);        memset(sz,0,sizeof sz);        int x,y;        for(int i=1; i<n; ++i) {            scanf("%d%d",&x,&y);            add(x,y,i);            add(y,x,i);        }        dfs1(1);        dfs2(1,0);        while(m--) {            char ch[10];            int x,y,k;            scanf("%s",ch);            scanf("%d%d%d",&x,&y,&k);            if(ch[3]=='1')updatepoint(x,y,k);            else if(x!=y)updatedge(x,y,k);        }        for(int i=2; i<=n; ++i) {            sumedge[i]+=sumedge[i-1];            sumpoint[i]+=sumpoint[i-1];        }        for(int i=2; i<=n; ++i)f[pos[i]]=sumedge[sgID[i]];        for(int i=1; i<=n-1; ++i)            printf("%lld ",sumpoint[sgID[i]]);        printf("%lld\n",sumpoint[sgID[n]]);        for(int i=1; i<=n-2; ++i)printf("%lld ",f[i]);        if(n-1>0)printf("%lld",f[n-1]);        puts("");    }    return 0;}
原创粉丝点击