답안 #995116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
995116 2024-06-08T13:12:41 Z Szil Jail (JOI22_jail) C++17
5 / 100
1891 ms 181344 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200'100;

vector<int> g[MAXN], g2[MAXN*21];
int pos[MAXN], tin[MAXN], tout[MAXN], head[MAXN], heavy[MAXN], sz[MAXN], par[MAXN], 
    s[MAXN], e[MAXN], depth[MAXN], col[MAXN*21], timer, pos_timer, n, m;
bool ok;

void init() {
    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }
    for (int i = 1; i <= 20*n; i++) {
        g2[i].clear();
    }
    fill(heavy, heavy+n+1, 0);
    fill(s, s+n+1, 0);
    fill(e, e+n+1, 0);
    fill(col, col+20*n+1, 0);
    timer = pos_timer = 1;
    ok = true;
}

int get_segtree1_index(int u) {
    return 10*n + u;
}

int get_segtree2_index(int u) {
    return n + u;
}

int get_path_index(int u) {
    return u;
}

void dfs(int u, int p = -1, int d = 0) {
    tin[u] = timer++;
    sz[u] = 1;
    depth[u] = d;
    for (int v : g[u]) {
        if (v == p) continue;
        par[v] = u;
        dfs(v, u, d+1);
        sz[u] += sz[v];
        if (sz[heavy[u]] < sz[v]) {
            heavy[u] = v;
        }
    }
    tout[u] = timer++;
}

bool is_ancestor(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int nxt(int u, int v) {
    if (is_ancestor(u, v)) {
        for (int x : g[u]) {
            if (x != par[u] && is_ancestor(x, v)) return x;
        }
    } else {
        return par[u];
    }
}

void dfs2(int u, int h, int p) {
    head[u] = h;
    pos[u] = pos_timer++;
    if (heavy[u]) {
        dfs2(heavy[u], h, u);
    }
    for (int v : g[u]) {
        if (v == p || v == heavy[u]) continue;
        dfs2(v, v, u);
    }
}

void dfs3(int u) {
    col[u] = 1;
    for (int v : g2[u]) {
        if (col[v] == 0) dfs3(v);
        else if (col[v] == 1) ok = false;
    }
    col[u] = 2;
}

void build_segtree1(int v, int tl, int tr) {
    if (tl == tr) {
        if (s[tl]) {
            g2[get_segtree1_index(v)].emplace_back(get_path_index(s[tl]));
        }
    } else {
        int tm = (tl + tr) / 2;
        build_segtree1(2*v, tl, tm);
        build_segtree1(2*v+1, tm+1, tr);
        g2[get_segtree1_index(v)].emplace_back(get_segtree1_index(2*v));
        g2[get_segtree1_index(v)].emplace_back(get_segtree1_index(2*v+1));
    }
}

void build_segtree2(int v, int tl, int tr) {
    if (tl == tr) {
        if (e[tl]) {
            g2[get_path_index(e[tl])].emplace_back(get_segtree2_index(v));
        }
    } else {
        int tm = (tl + tr) / 2;
        build_segtree2(2*v, tl, tm);
        build_segtree2(2*v+1, tm+1, tr);
        g2[get_segtree2_index(2*v)].emplace_back(get_segtree2_index(v));
        g2[get_segtree2_index(2*v+1)].emplace_back(get_segtree2_index(v));
    }
}

void add_segtree1_edge(int v, int tl, int tr, int l, int r, int node) {
    if (l > r) return;
    if (l == tl && r == tr) {
        g2[node].emplace_back(get_segtree1_index(v));
    } else {
        int tm = (tl + tr) / 2;
        add_segtree1_edge(2*v, tl, tm, l, min(tm, r), node);
        add_segtree1_edge(2*v+1, tm+1, tr, max(tm+1, l), r, node);
    }
}

void add_segtree2_edge(int v, int tl, int tr, int l, int r, int node) {
    if (l > r) return;
    if (l == tl && r == tr) {
        g2[get_segtree2_index(v)].emplace_back(node);
    } else {
        int tm = (tl + tr) / 2;
        add_segtree2_edge(2*v, tl, tm, l, min(tm, r), node);
        add_segtree2_edge(2*v+1, tm+1, tr, max(tm+1, l), r, node);
    }
}

void qry1(int u, int v, int node) {
    cerr << "s" << u << " " << v << endl;
    while (head[u] != head[v]) {
        if (depth[head[u]] < depth[head[v]]) swap(u, v);
        add_segtree1_edge(1, 1, n, pos[head[u]], pos[u], node);
        u = par[head[u]];
    }
    if (depth[u] > depth[v]) swap(u, v);
    add_segtree1_edge(1, 1, n, pos[u], pos[v], node);
}

void qry2(int u, int v, int node) {
    cerr << "e" << u << " " << v << endl;
    while (head[u] != head[v]) {
        if (depth[head[u]] < depth[head[v]]) swap(u, v);
        add_segtree2_edge(1, 1, n, pos[u], pos[v], node);
        u = par[head[u]];
    }
    if (depth[u] > depth[v]) swap(u, v);
    add_segtree2_edge(1, 1, n, pos[u], pos[v], node);
}

void solve() {
    init();
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    } 
    cin >> m;
    vector<pair<int, int>> edges(m+1);
    for (int i = 1; i <= m; i++) {
        cin >> edges[i].first >> edges[i].second;
        auto [u, v] = edges[i];
        s[u] = i;
        e[v] = i;
    }
    build_segtree1(1, 1, n);
    build_segtree2(1, 1, n);
    dfs(1);
    dfs2(1, 1, -1);
    for (int i = 1; i <= m; i++) {
        auto [u, v] = edges[i];
        qry1(nxt(u, v), v, get_path_index(i));
        qry2(u, nxt(v, u), get_path_index(i));
    }
    for (int i = 1; i <= m; i++) {
        int u = get_path_index(i);
        if (col[u] == 0) dfs3(u);
    }
    cout << (ok?"Yes\n":"No\n");
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
}

Compilation message

jail.cpp: In function 'int nxt(int, int)':
jail.cpp:68:1: warning: control reaches end of non-void function [-Wreturn-type]
   68 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 113496 KB Output is correct
2 Correct 18 ms 113496 KB Output is correct
3 Correct 18 ms 113500 KB Output is correct
4 Correct 41 ms 113500 KB Output is correct
5 Correct 64 ms 113632 KB Output is correct
6 Correct 19 ms 113496 KB Output is correct
7 Correct 20 ms 113516 KB Output is correct
8 Correct 41 ms 113496 KB Output is correct
9 Correct 154 ms 115624 KB Output is correct
10 Correct 73 ms 139092 KB Output is correct
11 Correct 101 ms 113744 KB Output is correct
12 Correct 435 ms 115024 KB Output is correct
13 Correct 657 ms 150352 KB Output is correct
14 Correct 721 ms 150476 KB Output is correct
15 Correct 773 ms 155332 KB Output is correct
16 Correct 1891 ms 181344 KB Output is correct
17 Correct 856 ms 158660 KB Output is correct
18 Correct 1369 ms 156624 KB Output is correct
19 Correct 855 ms 156756 KB Output is correct
20 Correct 769 ms 156668 KB Output is correct
21 Correct 810 ms 158156 KB Output is correct
22 Correct 743 ms 148184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 113500 KB Output is correct
2 Correct 24 ms 113500 KB Output is correct
3 Correct 20 ms 113480 KB Output is correct
4 Incorrect 25 ms 113624 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 113500 KB Output is correct
2 Correct 24 ms 113500 KB Output is correct
3 Correct 20 ms 113480 KB Output is correct
4 Incorrect 25 ms 113624 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 113500 KB Output is correct
2 Correct 24 ms 113500 KB Output is correct
3 Correct 20 ms 113480 KB Output is correct
4 Incorrect 25 ms 113624 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 113500 KB Output is correct
2 Correct 24 ms 113500 KB Output is correct
3 Correct 20 ms 113480 KB Output is correct
4 Incorrect 25 ms 113624 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 113500 KB Output is correct
2 Correct 18 ms 113500 KB Output is correct
3 Correct 19 ms 113500 KB Output is correct
4 Correct 17 ms 113496 KB Output is correct
5 Correct 128 ms 113576 KB Output is correct
6 Correct 21 ms 113508 KB Output is correct
7 Incorrect 18 ms 113500 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 113496 KB Output is correct
2 Correct 18 ms 113496 KB Output is correct
3 Correct 18 ms 113500 KB Output is correct
4 Correct 41 ms 113500 KB Output is correct
5 Correct 64 ms 113632 KB Output is correct
6 Correct 19 ms 113496 KB Output is correct
7 Correct 20 ms 113516 KB Output is correct
8 Correct 41 ms 113496 KB Output is correct
9 Correct 154 ms 115624 KB Output is correct
10 Correct 73 ms 139092 KB Output is correct
11 Correct 101 ms 113744 KB Output is correct
12 Correct 435 ms 115024 KB Output is correct
13 Correct 657 ms 150352 KB Output is correct
14 Correct 721 ms 150476 KB Output is correct
15 Correct 773 ms 155332 KB Output is correct
16 Correct 1891 ms 181344 KB Output is correct
17 Correct 856 ms 158660 KB Output is correct
18 Correct 1369 ms 156624 KB Output is correct
19 Correct 855 ms 156756 KB Output is correct
20 Correct 769 ms 156668 KB Output is correct
21 Correct 810 ms 158156 KB Output is correct
22 Correct 743 ms 148184 KB Output is correct
23 Correct 20 ms 113500 KB Output is correct
24 Correct 24 ms 113500 KB Output is correct
25 Correct 20 ms 113480 KB Output is correct
26 Incorrect 25 ms 113624 KB Output isn't correct
27 Halted 0 ms 0 KB -