2021-03-04

Through Path
有题意,需要用到差分的知识
以任意一个节点为根节点,假设a,b两点分别是+x,-x的点
当a能够不通过b点到达根节点时:
w[a] += x,w[b] -= x
否则:
w[b] += x
Q次操作若每次操作都要判断a能否到达根节点,也会超时。建完树后求出每个点的深度,可在O(1)的时间复杂度判定

#include 
#include 
#include 
#include 

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;


const int N = 200010, M = 2 * N;


int h[N], e[M], ne[M], dep[N], idx, n, m, Q;
LL w[N];
PII edge[N];
int q[N];
bool st[N];

void add(int a, int b) {
     
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}


void get_dep(int u) {
     
    memset(st, 0, sizeof st);
    st[u] = 1;
    q[0] = u;
    int hh = 0, tt = 0;
    while (hh <= tt) {
     
        auto t = q[hh ++];

        for (int i = h[t]; ~i; i = ne[i]) {
     
            int j = e[i];
            if (st[j])  continue;

            q[++ tt] = j;
            st[j] = 1;
            dep[j] = dep[t] + 1;
        }
    }
}

void bfs(int u) {
     
    memset(st, 0, sizeof st);
    st[u] = 1;

    int hh = 0, tt = 0;
    q[0] = u;
    while (hh <= tt) {
     
        auto t = q[hh ++];
        for (int i = h[t]; i != -1; i = ne[i]) {
     
            int j = e[i];
            if (st[j]) continue;
            w[j] += w[t];
            q[++ tt] = j;
            st[j] = 1;
        }
    }
}
int main() {
     
    memset(h, -1, sizeof h);

    scanf("%d", &n);
    for (int i = 1; i < n; i ++) {
     
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
        edge[i] = {
     a, b};
    }

    get_dep(1);


    scanf("%d", &Q);
    while (Q --) {
     
        int t, E, x;
        scanf("%d%d%d", &t, &E, &x);
        int a = edge[E].first, b = edge[E].second;
        if (t == 2)     swap(a, b);

        if (dep[a] < dep[b])       w[1] += x, w[b] -= x;
        else  w[a] += x;
    }

    bfs(1);

    for (int i = 1; i <= n; i ++)   cout << w[i] << endl;
    return 0;
}

你可能感兴趣的:(树结构,算法)