码迷,mamicode.com
首页 > 其他好文 > 详细

[Luogu] 部落冲突--00

时间:2017-12-10 12:42:13      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:stdin   http   ==   out   new   bug   return   部落   efi   

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

标签:stdin   http   ==   out   new   bug   return   部落   efi   

原文地址:http://www.cnblogs.com/shandongs1/p/8016393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!