HDU 5044 Tree

Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 416    Accepted Submission(s): 80


Problem Description
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
 

Source


题意:有一颗树,对这颗树进行M次操作,其中一个操作是(1,u,v,w) u ->v的路径上所有点的权值加上w,(2,u,v,w)的路径上所有边的权值加上w。操作完后,把每个点的权值和每条边的权值输出来。注意n=1时,没有边,但是也要换行。


思路:其实是大水题。。。直接树链剖分+zkw线段树可以2S多水过的.....当然这种题目必须加输入挂啊!!!

点的权值我不多说了,我大概说说边的,我们其实也可以看成是点权的,除了根节点外,每个点都会有一条边与它的父亲相连,我们可以直接把这条边的权值当做是这个点的权值就行了。在更新的时候和更新点的是一样的,不过要注意它们的LCA不能加上,所以我们只需要减掉就行了。

这道题的真正做法貌似是O(n)的LCA? 然后dfs 处理标记? ....我大概想了下好像是从叶子开始把值往上传。

标记的话,假设有一个询问(u,v) 那么我们求出它的LCA,然后把询问拆成(u,LCA) , (v,LCA) , 然后u,v处标记+w, 然后LCA标记-w , 大概是这样? 大概是这样,细节要讨论一下。

不知道对不对....


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define eps 1e-9
#define LL long long
#define zero(x) (-eps < (x) && (x) < eps)
const int maxn = 100000 + 5;
int top[maxn],fa[maxn],son[maxn],size[maxn],dep[maxn];
int node[maxn];
LL node_value[maxn<<2],edges_value[maxn<<2];
int q[maxn],front,rear;
int n, Q;

struct Node
{
    int v,id;
    Node * next;
}*first[maxn],edges[maxn*2];
int ptr;

void add(int u,int v,int id)
{
    edges[++ptr].v = v; edges[ptr].id = id;
    edges[ptr].next = first[u];
    first[u] = &edges[ptr];
}

void read_int(int & x)
{
    char ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    x = ch - '0'; ch = getchar();
    while ('0' <= ch && ch <= '9') {
        x = 10 * x + ch - '0';
        ch = getchar();
    }
}

int edges_id[maxn];

void build()
{
    front = rear = 0;
    fa[1] = -1;
    q[rear++] = 1;
    dep[1] = 1;
    while (front < rear) {
        int u = q[front++];
        for (Node * p = first[u] ; p ; p = p->next) {
            int v = p->v;
            if (v == fa[u]) continue;
            edges_id[v] = p->id;
            fa[v] = u;
            dep[v] = dep[u] + 1;
            q[rear++] = v;
        }
    }
    rrep(i,rear-1,0) {
        int u = q[i];
        top[u] = u;
        size[u] = 1; son[u] = -1;
        for(Node * p = first[u]; p ; p = p->next) {
            int v = p->v;
            if (v == fa[u]) continue;
            size[u] += size[v];
            if (son[u] == -1 || size[son[u]] < size[v]) son[u] = v;
        }
    }
    int dfn = 0;
    rep(i,0,rear) {
        int u = q[i];
        if (top[u] != u) continue;
        node[u] = ++dfn;
        while (son[u] != -1) {
            top[son[u]] = top[u];
            u = son[u];
            node[u] = ++dfn;
        }
    }
}

int tree_size;
void input()
{
    ptr = 0; clr(first,0);
    scanf("%d%d",&n,&Q);
    tree_size = 1; while (tree_size < n) tree_size <<= 1; --tree_size;
    rep(i,0,n-1) {
        int u , v;
        read_int(u); read_int(v);
        add(u,v,i); add(v,u,i);
    }
    build();
}

void update(int l,int r,LL w, LL * value)
{
    l += tree_size;  r += tree_size;
    while (l <= r) {
        if (l & 1) value[l++] += w;
        if (~r & 1)  value[r--] += w;
        l >>= 1; r >>= 1;
    }
}

LL query(int l,LL * value)
{
    LL ret = 0; l += tree_size;
    while (l >= 1) {
        ret += value[l];
        l >>= 1;
    }
    return ret;
}

void Query(int a,int b,int w,LL * value,int e)
{
    int L , R;
    for(;;) {
        if (dep[top[a]] < dep[top[b]]) swap(a,b);
        if (top[a] == top[b]) {
            L = min(node[a],node[b]);
            R = max(node[a],node[b]);
            update(L,R,w,value);
            break;
        } else {
            L = min(node[top[a]],node[a]);
            R = max(node[top[a]],node[a]);
            update(L,R,w,value);
            a = fa[top[a]];
        }
    }
    if (e == 1) update(L,L,-w,value);
}

LL e_ans[maxn];
void solve()
{
    clr(node_value,0); clr(edges_value,0);
    char Oper[10];
    int u , v, w;
    while (Q--) {
        scanf("%s",Oper);
        read_int(u); read_int(v); read_int(w);
        if (Oper[3] == '1') Query(u,v,w,node_value,0);
        else Query(u,v,w,edges_value,1);
    }
    rep(i,1,n+1) {
        printf("%I64d",query(node[i],node_value));
        if (i < n) printf(" ");
    }
    puts("");
    rep(i,2,n+1)
    e_ans[edges_id[i]] = query(node[i],edges_value);
    rep(i,0,n-1) {
        printf("%I64d",e_ans[i]);
        if (i < n-2) printf(" ");
    }
    puts("");
}

int main()
{
   // freopen("in.txt","r",stdin);
    int T; cin >> T;
    rep(cas,1,T+1) {
        input();
        printf("Case #%d:\n",cas);
        solve();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值