Submission #546340

# Submission time Handle Problem Language Result Execution time Memory
546340 2022-04-07T10:26:40 Z Jarif_Rahman Jail (JOI22_jail) C++17
0 / 100
1 ms 212 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int K = 19;
 
int n, m;
vector<vector<int>> v;
vector<int> s, t;
vector<vector<int>> sth;
vector<int> state;
vector<int> anc[K];
vector<int> d;
bool cycle;
 
void dfs1(int nd, int ss, int dis){
    for(int x: v[nd]) if(x != ss) dfs1(x, nd, dis+1);
    if(ss != -1) anc[0][nd] = ss;
    d[nd] = dis;
}

int get_anc(int nd, int h){
    for(int i = 0; i < K; i++){
        if(h%2 == 1) nd = anc[i][nd];
        h/=2;
    }
    return nd;
}
 
int lca_jump(int a, int b){
    if(a == b) return a;
    for(int i = K-1; i >= 0; i--) if(anc[i][a] != anc[i][b])
        return lca_jump(anc[i][a], anc[i][b]);
    return anc[0][a];
}
 
int lca(int a, int b){
    if(d[b] < d[a]) swap(a, b);
    b = get_anc(b, d[b]-d[a]);
    return lca_jump(a, b);
}

int fix_path(int a, int b){
    if(lca(a, b) == a) return get_anc(b, d[b]-d[a]-1);
    return anc[0][a];
}

struct segtree{
    int k;
    bool dir;
    vector<int> v;
    segtree(){}
    segtree(int n, bool _dir){
        k = 1, dir = _dir;
        while(k < n) k*=2;
        k*=2;
        v.resize(k);
    }
    void graph_init(){
        for(int i = k/2-1; i > 0; i--){
            v[i] = sth.size();
            sth.pb({});
            state.pb(0);
            if(dir) sth[v[i]].pb(v[2*i]), sth[v[i]].pb(v[2*i+1]);
            else sth[v[2*i]].pb(v[i]), sth[v[2*i+1]].pb(v[i]);
        }
    }
    void add_edge(int l, int r, int nd, int a, int b, int x){
        if(a > r || b < l) return;
        if(a >= l && b <= r){
            if(dir) sth[x].pb(v[nd]);
            else sth[v[nd]].pb(x);
            return;
        }
        int md = (a+b)/2;
        add_edge(l, r, 2*nd, a, md, x);
        add_edge(l, r, 2*nd+1, md+1, b, x);
    }
    void add_edge(int l, int r, int x){
        add_edge(l, r, 1, 0, k/2-1, x);
    }
};

struct HLD{
    int n;
    bool dir;
    static const int k = 21;
    vector<vector<int>> v;
    vector<int> sz;
    vector<int> depth;
    vector<int> p;
    segtree seg;
    vector<int> id;
    vector<int> top;
    int cnt = 0;

    HLD(int _n, bool _dir){
        dir = _dir;
        n = _n;
        v.assign(n, {});
    }

    void pre_dfs(int nd, int ss, int d){
        for(int x: v[nd]) if(x != ss) pre_dfs(x, nd, d+1);
        for(int x: v[nd]) if(x != ss) sz[nd]+=sz[x];
        p[nd] = ss;
        depth[nd] = d;
    }

    void dfs(int nd, int ss, int tp){
        id[nd] = cnt;
        top[nd] = tp;
        cnt++;
        int mx = 0, in = -1;
        for(int x: v[nd]) if(x != ss) if(sz[x] > mx) mx = sz[x], in = x;
        if(in != -1) dfs(in, nd, tp);
        for(int x: v[nd]) if(x != ss  && x != in) dfs(x, nd, x);
    }

    void add_edge(int a, int b, int x){
        while(top[a] != top[b]){
            if(depth[top[a]] < depth[top[b]]) swap(a, b);
            seg.add_edge(id[top[a]], id[a], x);
            a = p[top[a]];
        }
        if(depth[a] > depth[b]) swap(a, b);
        seg.add_edge(id[a], id[b], x);
    }

    void init(){
        sz.assign(n, 1);
        depth.assign(n, -1);
        p.assign(n, -1);

        pre_dfs(0, -1, 0);

        id.assign(n, -1);
        top.assign(n, -1);
        dfs(0, -1, 0);

        seg = segtree(n, dir);

        for(int i = seg.k/2; i < seg.k; i++) seg.v[i] = 2*n+1;
        for(int i = 0; i < n; i++){
            seg.v[id[i]+k/2] = i+(dir?0:n);
        }

        seg.graph_init();
    }

};

void dfs2(int nd){
    if(state[nd] == 1){
        cycle = 1;
        return;
    }
    if(state[nd] == 2) return;
    state[nd] = 1;
    for(int x: sth[nd]) dfs2(x);
    state[nd] = 2;
}
 
void solve(){
    cin >> n;
    v.assign(n, {});
    fill(anc, anc+K, vector<int>(n, 0));

    d.assign(n, 0);
    sth.assign(2*n+1, {});
    state.assign(2*n+1, 0);

    HLD hld1(n, 1), hld2(n, 0);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);

        hld1.v[a].pb(b);
        hld1.v[b].pb(a);

        hld2.v[a].pb(b);
        hld2.v[b].pb(a);
    }

    hld1.init();
    hld2.init();
 
    dfs1(0, -1, 0);
    for(int p = 1; p < K; p++) for(int i = 0; i < n; i++)
        anc[p][i] = anc[p-1][anc[p-1][i]];
 
    cin >> m;
    s.assign(m, 0);
    t.assign(m, 0);
    for(int i = 0; i < m; i++) cin >> s[i] >> t[i], s[i]--, t[i]--;
    for(int i = 0; i < m; i++){
        sth[s[i]].pb(t[i]+n);
    }
 
    for(int i = 0; i < m; i++){
        hld1.add_edge(fix_path(s[i], t[i]), t[i], t[i]+n);
        hld2.add_edge(s[i], fix_path(t[i], s[i]), s[i]);
    }
 
    cycle = 0;
 
    for(int i = 0; i < 2*n; i++) if(state[i] == 0) dfs2(i);
 
    cout << (cycle?"No\n":"Yes\n");
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T; cin >> T; while(T--) solve();
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -