CF191C Fools and Roads

Posted miraclys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF191C Fools and Roads相关的知识,希望对你有一定的参考价值。

(Large extbf{Description:} large{有一颗 n 个节点的树,k 次旅行,问每一条边被走过的次数。})

(Large extbf{Solution:} large{树上差分板子??})

(Large extbf{Code:})

#include <cstdio>
#include <algorithm>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 1e5 + 5; 
int n, k, cnt, head[N], u[N], v[N], ans[N];
int size[N], top[N], dep[N], fa[N], son[N], w[N];

struct Edge {
    int to, next;   
}e[N << 1];

inline int read() {
    char ch = gc();
    int ans = 0, flag = 1;
    while (ch > '9' || ch < '0') {
        if (ch == '-') flag = -1;
        ch = gc();
    }   
    while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
    return ans;
}

inline void add(int x, int y) {
    e[++cnt].to = y;
    e[cnt].next = head[x];
    head[x] = cnt;
}

inline void dfs1(int x, int y) {
    int Max = -1;
    dep[x] = dep[y] + 1;
    fa[x] = y;
    size[x] = 1;
    for (int i = head[x]; i ; i = e[i].next) {
        int U = e[i].to;
        if (U == y) continue;
        dfs1(U, x);
        size[x] += size[U];
        if (size[U] > Max) Max = size[U], son[x] = U;
    }   
}

inline void dfs2(int x, int y) {
    top[x] = y;
    if (!son[x]) return ;
    dfs2(son[x], y);
    for (int i = head[x]; i ; i = e[i].next) {
        int U = e[i].to;
        if (U == fa[x] || U == son[x]) continue;
        dfs2(U, U);
    }
}

inline int lca(int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }   
    if (dep[x] > dep[y]) swap(x, y);
    return x;
}

inline int dfs(int x) {
    int cur = w[x];
    for (int i = head[x]; i ; i = e[i].next) {
        int U = e[i].to;
        if (U == fa[x]) continue;
        cur += dfs(U);
    }
    return ans[x] = cur;
}

int main() {
    n = read();
    rep(i, 1, n - 1) { u[i] = read(); v[i] = read(); add(u[i], v[i]); add(v[i], u[i]); }
    dfs1(1, 0);//树剖 方便求lca。
    dfs2(1, 1);
    k = read();
    int l, r;
    rep(i, 1, k) l = read(), r = read(), ++w[l], ++w[r], w[lca(l, r)] -= 2;//边的差分。
    dfs(1);//最后遍历一遍树,每条边的走过次数就是它的子树和。
    rep(i, 1, n - 1) { if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]); printf("%d ", ans[v[i]]); }//差分时我们以每条边深度更深的那个点存储这条边的值。
    return 0; 
}

(largecolor{pink}{byquad Miraclys})

以上是关于CF191C Fools and Roads的主要内容,如果未能解决你的问题,请参考以下文章

Fools and Roads CodeForces - 191C

CF567E President and Roads

[CF711D]Directed Roads(强联通分量,计数)

CF543B Destroying Roads 枚举 + 思维 + BFS

CF711D Directed Roads

[cf1149D]Abandoning Roads