結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー rniyarniya
提出日時 2022-06-17 22:42:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 803 ms / 4,000 ms
コード長 14,278 bytes
コンパイル時間 3,444 ms
コンパイル使用メモリ 239,228 KB
実行使用メモリ 109,220 KB
最終ジャッジ日時 2024-10-09 08:59:45
合計ジャッジ時間 16,580 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 6 ms
6,820 KB
testcase_09 AC 9 ms
6,820 KB
testcase_10 AC 10 ms
6,820 KB
testcase_11 AC 12 ms
6,816 KB
testcase_12 AC 9 ms
6,816 KB
testcase_13 AC 281 ms
36,360 KB
testcase_14 AC 290 ms
29,752 KB
testcase_15 AC 380 ms
64,640 KB
testcase_16 AC 144 ms
49,132 KB
testcase_17 AC 265 ms
27,584 KB
testcase_18 AC 340 ms
65,780 KB
testcase_19 AC 448 ms
62,184 KB
testcase_20 AC 349 ms
61,864 KB
testcase_21 AC 367 ms
43,108 KB
testcase_22 AC 269 ms
30,132 KB
testcase_23 AC 581 ms
54,104 KB
testcase_24 AC 504 ms
53,868 KB
testcase_25 AC 525 ms
53,880 KB
testcase_26 AC 550 ms
53,992 KB
testcase_27 AC 557 ms
53,900 KB
testcase_28 AC 562 ms
53,732 KB
testcase_29 AC 543 ms
54,028 KB
testcase_30 AC 521 ms
53,912 KB
testcase_31 AC 564 ms
54,004 KB
testcase_32 AC 499 ms
54,004 KB
testcase_33 AC 2 ms
6,820 KB
testcase_34 AC 99 ms
47,068 KB
testcase_35 AC 135 ms
39,612 KB
testcase_36 AC 131 ms
25,556 KB
testcase_37 AC 2 ms
6,820 KB
testcase_38 AC 46 ms
6,816 KB
testcase_39 AC 381 ms
109,220 KB
testcase_40 AC 803 ms
88,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {
    for (size_t i = 0; i < N; i++) {
        os << v[i] << (i + 1 == N ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) void(0)
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }
#pragma endregion

#include <cassert>
#include <utility>
#include <vector>

struct HeavyLightDecomposition {
    std::vector<std::vector<int>> G;  // child of vertex v on heavy edge is G[v].front() if it is not parent of v
    int n, time;
    std::vector<int> par,  // parent of vertex v
        sub,               // size of subtree whose root is v
        dep,               // distance bitween root and vertex v
        head,              // vertex that is the nearest to root on heavy path of vertex v
        tree_id,           // id of tree vertex v belongs to
        vertex_id,         // id of vertex v (consecutive on heavy paths)
        vertex_id_inv;     // vertex_id_inv[vertex_id[v]] = v

    HeavyLightDecomposition(int n)
        : G(n),
          n(n),
          time(0),
          par(n, -1),
          sub(n),
          dep(n, 0),
          head(n),
          tree_id(n, -1),
          vertex_id(n, -1),
          vertex_id_inv(n) {}

    void add_edge(int u, int v) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void build() {
        int tree_id_cur = 0;
        for (int r = 0; r < n; r++) {
            // assert(0 <= r && r < n);
            if (vertex_id[r] != -1) continue;
            dfs_sz(r);
            head[r] = r;
            dfs_hld(r, tree_id_cur++);
        }
        assert(time == n);
        for (int v = 0; v < n; v++) vertex_id_inv[vertex_id[v]] = v;
    }

    int idx(int v) const { return vertex_id[v]; }

    int la(int v, int k) {
        assert(0 <= v && v < n);
        assert(0 <= k && k <= dep[v]);
        while (1) {
            int u = head[v];
            if (vertex_id[v] - k >= vertex_id[u]) return vertex_id_inv[vertex_id[v] - k];
            k -= vertex_id[v] - vertex_id[u] + 1;
            v = par[u];
        }
    }

    int lca(int u, int v) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        assert(tree_id[u] == tree_id[v]);
        for (;; v = par[head[v]]) {
            if (vertex_id[u] > vertex_id[v]) std::swap(u, v);
            if (head[u] == head[v]) return u;
        }
    }

    int distance(int u, int v) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        assert(tree_id[u] == tree_id[v]);
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }

    template <typename F> void query_path(int u, int v, const F& f, bool vertex = false) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        assert(tree_id[u] == tree_id[v]);
        int p = lca(u, v);
        for (auto& e : ascend(u, p)) f(e.second, e.first + 1);
        if (vertex) f(vertex_id[p], vertex_id[p] + 1);
        for (auto& e : descend(p, v)) f(e.first, e.second + 1);
    }

    template <typename F> void query_path_noncommutative(int u, int v, const F& f, bool vertex = false) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        assert(tree_id[u] == tree_id[v]);
        int p = lca(u, v);
        for (auto& e : ascend(u, p)) f(e.first + 1, e.second);
        if (vertex) f(vertex_id[p], vertex_id[p] + 1);
        for (auto& e : descend(p, v)) f(e.first, e.second + 1);
    }

    template <typename F> void query_subtree(int u, const F& f, bool vertex = false) const {
        assert(0 <= u && u < n);
        f(vertex_id[u] + !vertex, vertex_id[u] + sub[u]);
    }

private:
    void dfs_sz(int v) {
        sub[v] = 1;
        if (!G[v].empty() && G[v].front() == par[v]) std::swap(G[v].front(), G[v].back());
        for (int& u : G[v]) {
            if (u == par[v]) continue;
            par[u] = v;
            dep[u] = dep[v] + 1;
            dfs_sz(u);
            sub[v] += sub[u];
            if (sub[u] > sub[G[v].front()]) std::swap(u, G[v].front());
        }
    }

    void dfs_hld(int v, int tree_id_cur) {
        vertex_id[v] = time++;
        tree_id[v] = tree_id_cur;
        for (int& u : G[v]) {
            if (u == par[v]) continue;
            head[u] = (u == G[v][0] ? head[v] : u);
            dfs_hld(u, tree_id_cur);
        }
    }

    std::vector<std::pair<int, int>> ascend(int u, int v) const {  // [u, v), v is ancestor of u
        std::vector<std::pair<int, int>> res;
        while (head[u] != head[v]) {
            res.emplace_back(vertex_id[u], vertex_id[head[u]]);
            u = par[head[u]];
        }
        if (u != v) res.emplace_back(vertex_id[u], vertex_id[v] + 1);
        return res;
    }

    std::vector<std::pair<int, int>> descend(int u, int v) const {  // (u, v], u is ancestor of v
        if (u == v) return {};
        if (head[u] == head[v]) return {{vertex_id[u] + 1, vertex_id[v]}};
        auto res = descend(u, par[head[v]]);
        res.emplace_back(vertex_id[head[v]], vertex_id[v]);
        return res;
    }
};

struct TwoEdgeConnectedComponents {
    int time, k;
    vector<int> ord, low, cmp;
    vector<vector<int>> G, C, T;
    vector<pair<int, int>> bridge;
    TwoEdgeConnectedComponents(int n) : time(0), k(0), ord(n, -1), low(n), cmp(n, -1), G(n) {}
    void add_edge(int u, int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }
    bool is_bridge(int u, int v) {
        if (ord[u] > ord[v]) swap(u, v);
        return ord[u] < low[v];
    }
    void dfs(int v, int p) {
        ord[v] = low[v] = time++;
        int bic = 0;
        for (int u : G[v]) {
            if (u == p && !bic) {
                bic = 1;
                continue;
            }
            if (~ord[u]) {
                low[v] = min(low[v], ord[u]);
                continue;
            }
            dfs(u, v);
            low[v] = min(low[v], low[u]);
            if (is_bridge(v, u)) bridge.emplace_back(v, u);
        }
    }
    void fill_component(int v) {
        C[cmp[v]].emplace_back(v);
        for (int u : G[v]) {
            if (~cmp[u] || is_bridge(u, v)) continue;
            cmp[u] = cmp[v];
            fill_component(u);
        }
    }
    void add_component(int v) {
        if (~cmp[v]) return;
        cmp[v] = k++;
        C.emplace_back();
        fill_component(v);
    }
    int build() {
        int n = G.size();
        for (int v = 0; v < n; ++v) {
            if (ord[v] < 0) dfs(v, -1);
        }
        for (int v = 0; v < n; ++v) add_component(v);
        T.resize(k);
        for (auto e : bridge) {
            int u = cmp[e.first], v = cmp[e.second];
            T[u].emplace_back(v);
            T[v].emplace_back(u);
        }
        return k;
    }
    int operator[](int i) const { return cmp[i]; }
};

#include "atcoder/fenwicktree"

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, Q;
    cin >> N >> M >> Q;
    TwoEdgeConnectedComponents TECC(N);
    for (; M--;) {
        int x, y;
        cin >> x >> y;
        TECC.add_edge(--x, --y);
    }

    int n = TECC.build();
    auto& C = TECC.C;
    auto& T = TECC.T;
    HeavyLightDecomposition HLD(n);
    for (int v = 0; v < n; v++) {
        for (int& u : T[v]) {
            if (v < u) {
                HLD.add_edge(v, u);
            }
        }
    }
    HLD.build();
    atcoder::fenwick_tree<int> FT(n);
    // for (int i = 0; i < n; i++) {
    //     if (C[i].size() > 1) {
    //         FT.add(HLD.idx(i), 1);
    //     }
    // }
    vector<map<int, int>> mp(n);  // 各連結成分で隣接点 v に向かう辺を持つ頂点
    for (auto e : TECC.bridge) {
        int u = TECC[e.first], v = TECC[e.second];
        mp[u][v] = e.first;
        mp[v][u] = e.second;
    }
    vector<int> val(n, 0);
    for (auto e : TECC.bridge) {
        int a = TECC[e.first], b = TECC[e.second];
        if (HLD.dep[a] > HLD.dep[b]) swap(a, b);
        if (HLD.par[a] == -1) continue;
        if (mp[a][b] != mp[a][HLD.par[a]]) {
            FT.add(HLD.idx(b), 1);
            val[b] = 1;
        }
    }

    auto query = [&](int x, int y) -> bool {
        int a = TECC[x], b = TECC[y];
        if (HLD.tree_id[a] != HLD.tree_id[b]) return false;
        if (a == b) return false;
        int sum = 0, p = HLD.lca(a, b);
        auto q = [&](int l, int r) { sum += FT.sum(l, r); };
        HLD.query_path(a, b, q, false);
        if (p == b) {
            swap(a, b);
            swap(x, y);
        }
        if (p == a) {
            int diff = HLD.dep[b] - HLD.dep[a];
            int c = HLD.la(b, diff - 1);
            if (mp[a][c] != x) return false;
            if (mp[b][HLD.la(b, 1)] != y) return false;
            sum -= val[c];
        } else {
            if (mp[a][HLD.la(a, 1)] != x) return false;
            if (mp[b][HLD.la(b, 1)] != y) return false;
            int f = -1;
            for (int v : {a, b}) {
                int diff = HLD.dep[v] - HLD.dep[p];
                int u = HLD.la(v, diff - 1);
                sum -= val[u];
                if (f == -1)
                    f = mp[p][u];
                else if (f != mp[p][u])
                    return false;
            }
        }
        return sum == 0;
    };

    for (; Q--;) {
        int x, y;
        cin >> x >> y;
        cout << (query(--x, --y) ? "Yes" : "No") << '\n';
    }
    return 0;
}
0