답안 #580371

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580371 2022-06-21T07:20:54 Z balbit Jail (JOI22_jail) C++14
0 / 100
59 ms 107580 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second

#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)

#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT

const int maxn = 120000+5;


vector<int> tree[maxn];
int dep[maxn];
int fa[18][maxn];
int tos[18][maxn], toe[18][maxn];
int nxt[maxn];
int st[maxn], en[maxn]; // starting or ending???
vector<int> g[maxn * 37];

int IT = 0;

void dfs(int v, int p) {
    tos[0][v] = IT++; toe[0][v] = IT++;
    if (st[v] != -1) {
        g[tos[0][v]].pb(st[v]);
    }
    if (en[v] != -1) {
        g[en[v]].pb(toe[0][v]);
    }
//    bug(v, nxt[v]);
    for (int u : tree[v]) {
        if (u == p) continue;
        dep[u] = dep[v] + 1;
        fa[0][u] = v;
        dfs(u,v);
    }
}

int snode[maxn], enode[maxn];
void CLR(int n){
    REP(i,n) {
        tree[i].clear(); dep[i] = 0;
        st[i] = en[i] = -1;
        nxt[i] = -1;
    }
}

int lca(int a, int b) {
    if (dep[a] < dep[b]) swap(a,b);
    int need = dep[a] - dep[b];
    REP(k, 18) if (need & (1<<k)) a = fa[k][a];
    assert(dep[a] == dep[b]);
    for (int j = 17; j>=0; --j) {
        if (fa[j][a] != fa[j][b]) {
            a = fa[j][a]; b = fa[j][b];
        }
    }
    if (a != b) return fa[0][a];
    return a;
}


int indeg[maxn*37];

int kth(int a, int k) {
    REP(j,18) {
        if (k & (1<<j)) a = fa[j][a];
    }
    return a;
}

signed main(){
    ios::sync_with_stdio(0), cin.tie(0);
    bug(1,2);

    int T; cin>>T;
    while (T--) {
        int n; cin>>n;
        CLR(n);

        REP(i,n-1) {
            int a,b;
//            cin>>a>>b; --a; --b;
            a = i; b = i+1;
            tree[a].pb(b); tree[b].pb(a);
        }


        int q; cin>>q;
        REP(i,q) {
            cin>>snode[i]>>enode[i]; --snode[i]; --enode[i];
            st[snode[i]] = i; en[enode[i]] = i;
        }

        IT = q;
        REP(i,q + n * 36) g[i].clear();

        dfs(0,-1);

        REP1(j, 18-1) {
            REP(i,n) {
                fa[j][i] = fa[j-1][fa[j-1][i]];
//                int F = fa[j][i];
                if (dep[i] + 1 - (1<<j) >= 0) {
                    tos[j][i] = IT++;
                    g[tos[j][i]].pb(tos[j-1][i]);
                    g[tos[j][i]].pb(tos[j-1][fa[j-1][i]]);

                    toe[j][i] = IT++;
                    g[toe[j-1][i]].pb(toe[j][i]);
                    g[toe[j-1][fa[j-1][i]]].pb(toe[j][i]);
                }
            }
        }


        REP(i,q) {
//            if (i) continue;
            int a = snode[i], b = enode[i];
            int c = lca(a,b);
//            bool yo = 0;

            for (int go : {a,b}) {

                // do s
                {
                    int v = go;
                    if (st[v] == i && st[c] == i) goto bruh;
                    if (st[v] == i) v = fa[0][v];
                    if (dep[v] < dep[c]) goto bruh;
                    if (st[c] == i) {
                        int ho = dep[v] - dep[c] - 1;
                        bug(ho);
                        c = kth(v, ho);
                    }
                    if (st[v] != i && dep[v] >= dep[c]) {
                        int gap = dep[v] - dep[c] + 1;
                        int L = __lg(gap);
                        bug(v,c,gap,L);
                        g[i].pb(tos[L][v]);
                        int nh = -dep[c] + dep[v] + 1 - (1<<L);
                        bug(kth(v, nh), L);
                        g[i].pb(tos[L][kth(v,nh)]);
                    }
                }
                bruh:;
                {
                    int v = go;
                    if (en[v] == i && en[c] == i) goto bruhh;
                    if (en[v] == i) v = fa[0][v];
                    if (dep[v] < dep[c]) goto bruhh;
                    if (en[c] == i) {
                        int ho = dep[v] - dep[c] - 1;
                        bug(ho);
                        c = kth(v, ho);
                    }
                    if (en[v] != i && dep[v] >= dep[c]) {
                        int gap = dep[v] - dep[c] + 1;
                        int L = __lg(gap);
                        bug(v,c,gap,L);
                        g[toe[L][v]].pb(i);
                        int nh = -dep[c] + dep[v] + 1 - (1<<L);
                        bug(kth(v, nh), L);
                        g[toe[L][kth(v,nh)]].pb(i);
                    }
                }
                bruhh:;

            }
        }

        bug("out");

        REP(i,IT) {
            indeg[i] = 0;
        }
        REP(i,IT) {
            for (int u : g[i]) {
                ++indeg[u];
            }
        }
        queue<int> qq;
        REP(i,IT) {
            if (indeg[i] == 0) {
                qq.push(i);
            }
        }
        int done = 0;
        while (!qq.empty() ){
            int v = qq.front(); qq.pop();
            ++done;
            bug(v, done);
            for (int u : g[v]) {
                if (--indeg[u] == 0) {
                    qq.push(u);
                }
            }
        }

        bug(done, IT);
        cout<<(done==IT?"Yes":"No")<<endl;

    }


}
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 107580 KB Output is correct
2 Incorrect 52 ms 107528 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Incorrect 50 ms 107416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Incorrect 50 ms 107416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Incorrect 50 ms 107416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Incorrect 50 ms 107416 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 107460 KB Output is correct
2 Correct 58 ms 107432 KB Output is correct
3 Incorrect 59 ms 107504 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 107580 KB Output is correct
2 Incorrect 52 ms 107528 KB Output isn't correct
3 Halted 0 ms 0 KB -