SPOJ 9577. Dynamic Tree Connectivity

Brief description :

… 动态维护无根树的连通性.. .

Analysis :

… 略 ..

/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)

#define DO(n) while(n--)
#define DO_C(n) int n____ = n; while(n____--)
#define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
#define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

#define Rush int T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Pascal system("fpc main.pas")

/** I/O Accelerator **/

/* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */

template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);

inline int RD(){ int x; RD(x); return x;}
template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline void RS(char *s){scanf("%s", s);}

template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}

/** Add - On **/

// <<= ` 0. Daily Use .,

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
int Ceil(int x, int y){return (x - 1) / y + 1;}

// <<= ` 1. Bitwise Operation .,

inline bool _1(int x, int i){return x & 1<<i;}
inline int _1(int i){return 1<<i;}
inline int _U(int i){return _1(i) - 1;};

inline int count_bits(int x){
    x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
    x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
    x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
    x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
    x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
    return x;
}

template<class T> inline T low_bit(T x) {
    return x & -x;
}

template<class T> inline T high_bit(T x) {
    T p = low_bit(x);
    while (p != x) x -= p, p = low_bit(x);
    return p;
}

// <<= ' 0. I/O Accelerator interface .,

template<class T> inline void RD(T &x){
    //cin >> x;
    //scanf("%d", &x);
    char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';

}

template<class T> inline void OT(const T &x){
    printf("%d\n", x);
}

/* .................................................................................................................................. */

const int N = 100001;

int l[N], r[N], p[N], rev[N]; bool rt[N];
int n;

#define lx l[x]
#define rx r[x]

inline void Release(int x){
    if (x == 0) return;

    if (rev[x]){
        swap(lx, rx);
        rev[lx] ^= 1, rev[rx] ^= 1;
        rev[x] = 0;
    }
}

inline void Set(int l[], int y, int x){
    l[y] = x, p[x] = y;
}

#define z p[y]
inline void Rotate(int x){
    int y = p[x];

    if (!rt[y]) Release(z), Set(y == l[z] ? l : r, z, x);
    else p[x] = z;

    Release(y), Release(x);

    if (x == l[y]) Set(l, y, rx), Set(r, x, y);
    else Set(r, y, lx), Set(l, x, y);

    if (rt[y]) rt[y] = false, rt[x] = true;
}

inline void Splay(int x){
    while (!rt[x]) Rotate(x);
}

int Access(int x){
    int y = 0; do{
        Splay(x), Release(x);
        rt[rx] = true, rt[rx = y] = false;
        x = p[y = x];
    } while (x);
    return y;
}

inline int Root(int x){
    for (x = Access(x); Release(x), lx ; x = lx);
    return x;
}

inline void Evert(int x){
    rev[Access(x)] ^= 1;
}

// Public :

void Query(int x, int y){
    puts(Root(x) == Root(y) ? "YES" : "NO");
}

void Link(int x, int y){

    if (Root(x) == Root(y)) return;

    Evert(x), Splay(x), p[x] = y, Access(x);
}

void Cut(int x, int y){

    Evert(y), Splay(y), Access(x), Splay(x);

    if (lx != y) return;

    p[lx] = p[x], rt[lx] = true, p[x] = lx = 0;
}

int main(){

    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    REP_1_C(i, _RD(n)) rt[i] = true;

    int a, b; char cmd[9]; DO_C(RD()){
        RS(cmd); RD(a, b); if (cmd[0] == 'c') Query(a, b);
        else if (cmd[0] == 'a') Link(a, b);
        else Cut(a, b);
    }

}

External link :

http://www.spoj.pl/problems/DYNACON1/

SPOJ 375. Query on a tree

Brief description :

动态维护树中两点之间路径上边权的最大值。

Tags: Classical

Analysis :

经典问题.. . 树链剖分 300- 行,3.10+ s
动态树 150- 行。。。 2.90+ s…

(目前 Yang Zhe 作业里的 全局平衡二叉树 还不知道怎么实现。>_<。)

/** ` Micro Mezzo Macro Flation — Overheated Economy ., **/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DO(n) while(n–)
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define Rush int T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")

typedef vector<int> VI;
template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);
template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline void RS(char *s){scanf("%s", s);}
template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void CLR(T &A){A.clear();}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
inline int _1(int i){return 1<<i;}
template<class T> inline void RD(T &x){char c; for (c = getchar(); c < ’0′; c = getchar()); x = c – ’0′; for (c = getchar(); c >= ’0′; c = getchar()) x = x * 10 + c – ’0′;}
template<class T> inline void OT(const T &x){printf("%d\n", x);}

/* …………………………………………………………………………………………………………………. */

const int N = 10001, L = 15;

struct Vertex;
struct Edge;
struct Path;

#define null &V[0]
struct Vertex{

    vector<Edge*> adj; Edge *host;
    int depth, size, euler_index;

    int id();
} V[N];

struct Edge{

    Vertex *a, *b; Path *host;
    int w;

    inline bool isLight(){return host == NULL;}
    int id();
} E[N-1];

struct Path{

    VI leaf; int *key; Vertex *head;
    int size, res, a, b;

private:

#define root 1, 0, size
#define lx (x << 1)
#define rx (lx | 1)
#define m ((l + r) >> 1)
#define lc lx, l, m
#define rc rx, m+1, r

#define x_ (x ^ 1)
#define y (x >> 1)

#define Update key[x] = max(key[lx], key[rx])

    inline void _B(int x, int l, int r){

        if (l == r){
            key[x] = leaf[l];
            leaf[l] = x;
        }
        else {
            _B(lc), _B(rc), Update;
        }
    }

    inline void _Q(int x, int l, int r){

        if (a <= l && r <= b){
            checkMax(res, key[x]);
        }
        else {
            if (a <= m) _Q(lc);
            if (m < b) _Q(rc);
        }
    }

    inline void _M(int x){
        if (b < key[x]){

            key[x] = b;

            while (y){
                if (key[y] == key[x_]) return;
                key[y] = max(key[x], key[x_]), x >>= 1;
            }

        }
        else if (b > key[x]){

            key[x] = b;

            while (y){
                if (key[x] <= key[x_]) return;
                key[y] = key[x], x >>= 1;
            }

        }
    }

public:

    inline void Build(){
        size = SZ(leaf), key = new int [4 * size];
        –size, _B(root);
    }

    inline int Query(Vertex *_a, Vertex *_b){
        res = 0, a = _a->depth – head->depth, b = _b->depth – head->depth – 1, _Q(root);
        return res;
    }

    inline void Modify(Edge *e, int v){
        b = v, _M(leaf[e->a->depth - head->depth]);
    }

    int id();
} P[N / 2];

#undef y

inline bool elder(Vertex *a, Vertex *b){return a->depth < b->depth;}
int Vertex::id(){return this – V;}
int Edge::id(){return this – E;}
int Path::id(){return this – P;}

Vertex *ST[L][2*N];
int n, l, cnt, res;

#define arc u->adj[i]

inline void dfs(Vertex *u = &V[1], Vertex *p = null){

    u->size = 1;
    u->depth = p->depth + 1;

    ST[0][u->euler_index = ++cnt] = u;

    REP(i, SZ(u->adj)){

        Vertex *v = arc->a == u ? arc->b : arc->a;

        if (v != p){
            dfs(v, u), v->host = arc, ST[0][++cnt] = u;
            u->size += v->size;
        }
    }
}

// Heavy_Light_Decomposition

inline void hld(Vertex *u = &V[1], Path *t = NULL){

    REP(i, SZ(u->adj)){

        Vertex *v = arc->b;

        if (v == u) continue;

        if (v->size * 2 >= u->size){
            if (t == NULL) t = &P[l++], t->head = u;
            arc->host = t, t->leaf.PB(arc->w), hld(v, t);
        }
        else {
            hld(v, NULL);
        }
    }
}

inline Vertex *lca(Vertex *a, Vertex *b){

    int l = a->euler_index, r = b->euler_index;

    if (l > r) swap(l, r); ++r; int lv = log2(r – l);

    return min(ST[lv][l], ST[lv][r -_1(lv)], elder);

}

#undef arc
#define arc x->host

inline void Query(Vertex *y, Vertex *x){

    if (x == y) return;

    if (arc->isLight()){
        checkMax(res, arc->w), Query(y, arc->a);
    }
    else {

        Path *path = arc->host;

        if (path->head->depth <= y->depth){
            checkMax(res, path->Query(y, x));
        }
        else {
            checkMax(res, path->Query(path->head, x));
            Query(y, path->head);
        }
    }
}

int a, b; char cmd[5];
Vertex *x, *y, *z;

int main(){

    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(false);

    Rush{

        // Initializing Phase …

        FOR_C(i, 1, _RD(n)){
            RD(a, b, E[i].w), E[i].a = &V[a], E[i].b = &V[b];
            V[a].adj.PB(&E[i]), V[b].adj.PB(&E[i]);
        }

        cnt = 0, dfs();

        FOR(i, 1, n) if (elder(E[i].b, E[i].a)){
            swap(E[i].a, E[i].b);
        }

        l = 0, hld(); REP(i, l) P[i].Build();

        for ( int lv = 1; _1(lv) <= cnt ; ++ lv ){
            for ( int i = 1; i + _1(lv) <= cnt + 1; ++ i )
                ST[lv][i] = min(ST[lv - 1][i], ST[lv - 1][i + _1(lv - 1)], elder);
        }

        // Interaction Phase …

        while (true){

            RS(cmd);

            if (cmd[0] == ‘C’){

                RD(a, b); if (E[a].isLight()) E[a].w = b;
                else E[a].host->Modify(&E[a], b);
            }
            else if (cmd[0] == ‘Q’){
                RD(a, b), x = &V[a], y = &V[b], z = lca(x, y);
                res = 0, Query(z, x), Query(z, y), OT(res);
            }
            else
                break;
        }

        // Rececling ….

        REP_1(i, n) CLR(V[i].adj), E[i].host = 0;

        REP(i, l){
            CLR(P[i].leaf);
            delete [] P[i].key;
        }
    }
}

External link:

https://www.spoj.pl/problems/QTREE/

Page 1 of 212