HDU 5044 Tree

一眼看过去是一道赤果果的树链剖分。。。但是时间卡的实在是紧,更好的做法应该是LCA啊,待我去看看,这里先练练树链剖分。


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
#define LL long long
const int N = 100005;
struct EG
{
    int u, v;
} EE[N];
struct EZ
{
    int v;
    int next;
} eg[N*2];
int cnt;
int head[N];
void add_eg(int u,int v)
{
    ++cnt;
    eg[cnt].v = v;
    eg[cnt].next = head[u];
    head[u] = cnt;
}
int dep[N], w[N], fa[N], sz[N], son[N], top[N];
void dfs_1(int v,int _fa){
    fa[v] = _fa;
    son[v] = 0;
    sz[v] = 1;
    int to;
    for(int i = head[v]; ~i; i=eg[i].next){
        to = eg[i].v;
        if(to == _fa) continue;
        dep[to] = dep[v]+1;
        dfs_1(to, v);
        if(sz[to] > sz[son[v]]){
            son[v] = to;
        }
        sz[v] += sz[to];
    }
}
int id;
void build_tree(int v,int tp){
    w[v] = ++id;
    top[v] = tp;
    if(son[v] != 0) build_tree(son[v], top[v]);
    int to;
    for(int i = head[v]; ~i; i=eg[i].next){
        to = eg[i].v;
        if(to == fa[v] || to == son[v]) continue;
        build_tree(to, to);
    }
}

LL nd_val[N], eg_val[N];
LL ans_1[N], ans_2[N];
void upd1(int x,int y,int k)
{
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        nd_val[w[top[x]]] += k;
        nd_val[w[x]+1] -= k;
        x = fa[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    nd_val[w[x]] += k;
    nd_val[w[y]+1] -= k;
}
void upd2(int x,int y,int k)
{
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        eg_val[w[top[x]]] += k;
        eg_val[w[x]+1] -= k;
        x = fa[top[x]];
    }
    if(x == y) return;
    if(dep[x] > dep[y]) swap(x, y);
    eg_val[w[son[x]]] += k;
    eg_val[w[y]+1] -= k;
}
int main()
{
    int i, j, T, n, m;
    int st = 0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        cnt = 0;
        memset(head, -1, sizeof(head));
        for(i=1; i<n; ++i){
            scanf("%d%d",&EE[i].u,&EE[i].v);
            add_eg(EE[i].u, EE[i].v);
            add_eg(EE[i].v, EE[i].u);
        }
        id = 0;
        dfs_1(1, 1);
        build_tree(1, 1);
        char op[13];
        int x, y, k;
        memset(nd_val, 0, sizeof(nd_val));
        memset(eg_val, 0, sizeof(eg_val));
        while(m--){
            scanf("%s%d%d%d",op,&x,&y,&k);
            if(op[3] == '1'){
                upd1(x, y, k);
            }else{
                upd2(x, y, k);
            }
        }
        for(i=1; i<n; ++i){
            if(dep[EE[i].u] > dep[EE[i].v]) swap(EE[i].u, EE[i].v);
        }
        LL go = 0;
        for(i=1; i<=id; ++i){
            go += nd_val[i];
            ans_1[i] = go;
        }
        go = 0;
        for(i=1; i<=id; ++i){
            go += eg_val[i];
            ans_2[i] = go;
        }
        printf("Case #%d:\n",++st);
        for(i=1; i<=n; ++i){
            if(i > 1) printf(" ");
            printf("%lld",ans_1[w[i]]);
        }
        puts("");
        for(i=1; i<n; ++i){
            if(i > 1) printf(" ");
            printf("%lld",ans_2[w[EE[i].v]]);
        }
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,这是一道关于二叉树的问题,需要输出每个二叉树的层序遍历。如果二叉树没有完全给出,则输出“not complete”。而且,这道题目是一个ACM竞赛的题目,需要使用Java语言进行编写。 以下是Java语言的代码实现: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.equals("")) { continue; } String[] arr = s.split(" "); int len = arr.length; int[] tree = new int[len]; boolean[] flag = new boolean[len]; for (int i = 0; i < len; i++) { if (arr[i].equals("()")) { tree[i] = -1; flag[i] = true; } else { tree[i] = Integer.parseInt(arr[i].substring(1, arr[i].length() - 1)); } } int root = 0; for (int i = 0; i < len; i++) { if (!flag[i]) { root = i; break; } } boolean isComplete = true; Queue<Integer> queue = new LinkedList<>(); queue.offer(root); int index = 1; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); if (tree[cur] == -1) { if (index < len && !flag[index]) { isComplete = false; } } else { if (cur * 2 + 1 < len) { queue.offer(cur * 2 + 1); if (tree[cur * 2 + 1] != -1) { flag[cur * 2 + 1] = true; } } if (cur * 2 + 2 < len) { queue.offer(cur * 2 + 2); if (tree[cur * 2 + 2] != -1) { flag[cur * 2 + 2] = true; } } } index++; } } if (!isComplete) { System.out.println("not complete"); continue; } queue.offer(root); StringBuilder sb = new StringBuilder(); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); sb.append(tree[cur]).append(" "); if (cur * 2 + 1 < len && tree[cur * 2 + 1] != -1) { queue.offer(cur * 2 + 1); } if (cur * 2 + 2 < len && tree[cur * 2 + 2] != -1) { queue.offer(cur * 2 + 2); } } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值