[Luogu] 部落冲突--00

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Luogu] 部落冲突--00相关的知识,希望对你有一定的参考价值。

https://www.luogu.org/problemnew/show/P3950

思路简单,树剖不用动脑子

#include <bits/stdc++.h>

using namespace std;
const int N = 3e5 + 10;

#define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1 

struct Node_1{
    int v, nxt;
}G[N << 1];
struct Node_2{
    int fa, son, deep, topp, siz, tree;
}P[N];
struct Node_3{
    int l, r, w;
}T[N << 2];

int n, Ti, now = 1, head[N], LCAi, u1[N], u2[N], js, LCA, tim, ans;

inline int read(){
    int x = 0; char c = gc;
    while(c < 0 || c > 9) c = gc;
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x;
}

inline void add(int u, int v){
    G[now].v = v;
    G[now].nxt = head[u];
    head[u] = now ++;
}

void dfs_find_son(int u, int fa, int dep){
    P[u].fa = fa;
    P[u].deep = dep;
    P[u].siz = 1;
    for(int i = head[u]; ~ i; i = G[i].nxt){
        int v = G[i].v;
        if(v != fa){
            dfs_find_son(v, u, dep + 1);
            P[u].siz += P[v].siz;
            if(P[v].siz > P[P[u].son].siz) P[u].son = v;
        }
    }
}

void dfs_to_un(int u, int tp){
    P[u].topp = tp;
    P[u].tree = ++ tim;
    if(!P[u].son) return ;
    dfs_to_un(P[u].son, tp);
    for(int i = head[u]; ~ i; i = G[i].nxt){
        int v = G[i].v;
        if(v != P[u].son && v != P[u].fa) dfs_to_un(v, v);
    }
}

void build_tree(int l, int r, int jd){
    T[jd].l = l; T[jd].r = r;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build_tree(l, mid, lson);
    build_tree(mid + 1, r, rson);
}

void Poi_G(int l, int r, int jd, int x, int yj){
    if(l == r) {
        T[jd].w += ((r - l + 1) * yj);
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Poi_G(l, mid, lson, x, yj);
    else Poi_G(mid + 1, r, rson, x, yj);
    T[jd].w = T[lson].w + T[rson].w;
}

void Sec_A(int l, int r, int jd, int x, int y){
    if(x <= l && r <= y){
        ans += T[jd].w;
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_A(l, mid, lson, x, y);
    if(y > mid) Sec_A(mid + 1, r, rson, x, y); 
}

inline void Sec_A_imp(int x, int y){
    int tp1 = P[x].topp, tp2 = P[y].topp;
    ans = 0;
    while(tp1 != tp2){
        if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
        Sec_A(1, n, 1, P[tp1].tree, P[x].tree);
        if(ans != 0){
            puts("No"); return ;
        }
        x = P[tp1].fa;
        tp1 = P[x].topp;
    }
    /*if(x == y) {
        puts("Yes"); return ;
    }*/
    if(P[x].deep > P[y].deep) swap(x, y);
    Sec_A(1, n, 1, P[x].deep + 1, P[y].deep);
    if(ans) puts("No");
    else puts("Yes");
    return ;
}

void debug(){
    for(int i = 1; i <= n; i ++) cout << i << ":" << P[i].tree << endl;
    exit(0);
}

int main()
{
    freopen("lct.in", "r", stdin);
    freopen("lct.out", "w", stdout);
    n = read(); Ti = read();
    for(int i = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i < n; i ++) {
        int u = read(), v = read();
        add(u, v); add(v, u);
    }
    dfs_find_son(1, 0, 1);
    dfs_to_un(1, 1);
    build_tree(1, n, 1);
   // debug();
    while(Ti --){
        string s; cin >> s;
            if(s[0] == U){
            int x = read();
            int point = P[P[u1[x]].deep > P[u2[x]].deep ? u1[x] : u2[x]].tree;
            Poi_G(1, n, 1, point, -1);
        }
        else if(s[0] == C){
            u1[++ js] = read(), u2[js] = read();
            int point = P[P[u1[js]].deep > P[u2[js]].deep ? u1[js] : u2[js]].tree;
            Poi_G(1, n, 1, point, 1);
        }
        else{
            int x = read(), y = read();
            Sec_A_imp(x, y);
        }
    }
    return 0;
}

 

以上是关于[Luogu] 部落冲突--00的主要内容,如果未能解决你的问题,请参考以下文章

Luogu P3950部落冲突

Luogu P1692 部落卫队

CSS冲突导致左导航条异常,并且输入框无法获得焦点,反而被左导航条获取了求解

部落冲突豌豆荚为啥不能安装

Git分支的创建以及分支冲突解决

部落冲突联赛额外奖励规则