結果

問題 No.274 The Wall
ユーザー tatanaideyotatanaideyo
提出日時 2018-11-18 12:51:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 355 ms / 2,000 ms
コード長 2,819 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 80,988 KB
実行使用メモリ 132,116 KB
最終ジャッジ日時 2023-09-04 02:33:56
合計ジャッジ時間 3,123 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 122 ms
68,684 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 355 ms
132,116 KB
testcase_12 AC 19 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 14 ms
4,376 KB
testcase_16 AC 157 ms
62,056 KB
testcase_17 AC 146 ms
58,756 KB
testcase_18 AC 161 ms
63,544 KB
testcase_19 AC 26 ms
4,376 KB
testcase_20 AC 29 ms
4,376 KB
testcase_21 AC 31 ms
4,376 KB
testcase_22 AC 33 ms
4,380 KB
testcase_23 AC 32 ms
4,376 KB
testcase_24 AC 32 ms
4,380 KB
testcase_25 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:13:29: 警告: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   13 |     void Dfs(const int cur, auto &ord) {
      |                             ^~~~
main.cpp:61:22: 警告: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   61 | inline bool Is(const auto &s1, const auto &s2) {
      |                      ^~~~
main.cpp:61:38: 警告: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   61 | inline bool Is(const auto &s1, const auto &s2) {
      |                                      ^~~~

ソースコード

diff #

#include <iostream>
#include <vector>

struct Graph {
    const int n;
    std::vector<std::vector<int>> adj, radj;
    std::vector<int> scc;

    Graph(int _n) : n(_n), adj(n), radj(n), scc(n, false) {}

    void add_edge(int src, int dst) { adj[src].push_back(dst); radj[dst].push_back(src); }

    void Dfs(const int cur, auto &ord) {
        scc[cur] = true;
        for (auto dst : adj[cur])
            if (!scc[dst]) Dfs(dst, ord);
        ord.push_back(cur);
    }
    void RevDfs(const int id, const int cur) {
        scc[cur] = id;
        for (auto dst : radj[cur])
            if (scc[dst] == -1) RevDfs(id, dst);
    }

    void StronglyConnectedComponents() {
        std::vector<int> ord;
        for (int v = 0; v < n; ++v)
            if (!scc[v]) Dfs(v, ord);

        std::fill(scc.begin(), scc.end(), -1);
        for (int i = n - 1, no = 0; 0 <= i; --i)
            if (scc[ord[i]] == -1) RevDfs(no++, ord[i]);
    }
};

// -------------8<------- start of library -------8<------------------------
struct TwoSat {
    const int n;
    std::vector<bool> sol;
    Graph g;

    TwoSat(int _n) : n(_n), sol(n + 1, true), g(2 * n) {}

    void add_closure(int x1, bool lt1, int x2, bool lt2) {
        g.add_edge(x1 + (lt1 ? n : 0), x2 + (lt2 ? 0 : n));
        g.add_edge(x2 + (lt2 ? n : 0), x1 + (lt1 ? 0 : n));
    }

    bool Check() const { return sol[n]; }
    bool Solve() {
        g.StronglyConnectedComponents();
        for (int x = 0; x < n; ++x) {
            if (g.scc[x] == g.scc[x + n]) return sol[n] = false;
            sol[x] = (g.scc[x] > g.scc[x + n]);
        }
        return sol[n];
    }
};
// -------------8<------- end of library ---------8-------------------------

inline bool Is(const auto &s1, const auto &s2) {
    return (s1.first <= s2.first && s2.first <= s1.second) ||
        (s1.first <= s2.second && s2.second <= s1.second) ||
        (s2.first <= s1.first && s1.first <= s2.second) ||
        (s2.first <= s1.second && s1.second <= s2.second);
}

int main() {
    std::cin.tie(0); std::ios::sync_with_stdio(false);

    int n, m;
    std::cin >> n >> m;

    std::vector<std::pair<int, int>> seg(n), rseg(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> seg[i].first >> seg[i].second;
        rseg[i] = std::make_pair(m - 1 - seg[i].second, m - 1 - seg[i].first);
    }

    TwoSat sat(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (Is(seg[i], seg[j])) sat.add_closure(i, false, j, false);
            if (Is(seg[i], rseg[j])) sat.add_closure(i, false, j, true);
            if (Is(rseg[i], seg[j])) sat.add_closure(i, true, j, false);
            if (Is(rseg[i], rseg[j])) sat.add_closure(i, true, j, true);
        }
    }

    std::cout << (sat.Solve() ? "YES" : "NO") << std::endl;

    return 0;
}
0