답안 #597093

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597093 2022-07-15T13:33:39 Z KoD 게임 (APIO22_game) C++17
0 / 100
1 ms 208 KB
#include "game.h"
#include <bits/stdc++.h>

using std::pair;
using std::queue;
// using std::set;
template <class T> using set = std::unordered_set<T>;
using std::vector;

bool contains(set<int>& set, const int x) {
    return set.find(x) != set.end();
}

struct Interval {
    int mid, par, lch, rch;
    set<int> from, to;
    explicit Interval(const int m) : mid(m), par(-1), lch(-1), rch(-1), from(), to() {}
};

int N, K;
vector<vector<int>> graph, revgraph;
vector<Interval> segtree;

int build(int l, int r) {
    const int u = (int)segtree.size();
    const int m = (l + r) / 2;
    segtree.emplace_back(m);
    if (l < m) {
        segtree[u].lch = build(l, m);
        segtree[segtree[u].lch].par = u;
    }
    if (m + 1 < r) {
        segtree[u].rch = build(m + 1, r);
        segtree[segtree[u].rch].par = u;
    }
    for (int i = l; i < m; ++i)
        segtree[u].from.insert(i);
    for (int i = m + 1; i < r; ++i)
        segtree[u].to.insert(i);
    return u;
}

bool check(int x, int u) {
    const int p = segtree[x].par;
    if (p == -1) {
        return true;
    } else {
        if (segtree[p].lch == x) {
            return contains(segtree[p].from, u);
        } else {
            return contains(segtree[p].to, u);
        }
    }
}

void init(int n, int k) {
    N = n;
    K = k;
    graph.resize(n);
    revgraph.resize(n);
    build(0, k);
}

bool insert_recursive(int x, int u);
bool insert_recursive_rev(int x, int u);
bool add_recursive(int x, int u, int v);

bool insert_recursive(int x, int u) {
    auto& data = segtree[x];
    if (!check(x, u))
        return false;
    if (data.mid == u)
        return true;
    if (data.to.insert(u).second) {
        if (contains(data.from, u))
            return true;
        for (const int v : graph[u])
            if (add_recursive(x, u, v))
                return true;
    }
    return false;
}

bool insert_recursive_rev(int x, int u) {
    auto& data = segtree[x];
    if (!check(x, u))
        return false;
    if (data.mid == u)
        return true;
    if (data.from.insert(u).second) {
        if (contains(data.to, u))
            return true;
        for (const int v : revgraph[u])
            if (add_recursive(x, v, u))
                return true;
    }
    return false;
}

bool add_recursive(int x, int u, int v) {
    auto& data = segtree[x];
    if ((contains(data.from, v) or v == data.mid) and !contains(data.from, u)) {
        if (insert_recursive_rev(x, u))
            return true;
        if (data.lch != -1 and add_recursive(data.lch, u, v))
            return true;
    }
    if ((contains(data.to, u) or u == data.mid) and !contains(data.to, v)) {
        if (insert_recursive(x, v))
            return true;
        if (data.rch != -1 and add_recursive(data.rch, u, v))
            return true;
    }
    return false;
}

int add_teleporter(int u, int v) {
    graph[u].push_back(v);
    revgraph[v].push_back(u);
    static int counter = 0;
    counter++;
    const bool ret = add_recursive(0, u, v);
    return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Incorrect 1 ms 208 KB Wrong Answer[1]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Incorrect 1 ms 208 KB Wrong Answer[1]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Incorrect 1 ms 208 KB Wrong Answer[1]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Incorrect 1 ms 208 KB Wrong Answer[1]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Incorrect 1 ms 208 KB Wrong Answer[1]
3 Halted 0 ms 0 KB -