結果

問題 No.1244 Black Segment
ユーザー kimiyukikimiyuki
提出日時 2020-10-17 00:00:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,557 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 207,916 KB
実行使用メモリ 10,768 KB
最終ジャッジ日時 2023-09-28 06:43:45
合計ジャッジ時間 6,487 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 19 ms
5,992 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 18 ms
5,876 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 20 ms
7,984 KB
testcase_19 AC 28 ms
9,116 KB
testcase_20 AC 32 ms
9,552 KB
testcase_21 AC 30 ms
9,920 KB
testcase_22 AC 31 ms
9,544 KB
testcase_23 AC 30 ms
9,916 KB
testcase_24 AC 33 ms
9,908 KB
testcase_25 AC 31 ms
10,208 KB
testcase_26 AC 30 ms
9,980 KB
testcase_27 AC 35 ms
10,416 KB
testcase_28 AC 33 ms
10,584 KB
testcase_29 AC 33 ms
10,100 KB
testcase_30 AC 33 ms
10,152 KB
testcase_31 AC 35 ms
10,144 KB
testcase_32 AC 34 ms
10,076 KB
testcase_33 AC 33 ms
10,156 KB
testcase_34 AC 31 ms
10,100 KB
testcase_35 AC 30 ms
10,768 KB
testcase_36 AC 32 ms
10,680 KB
testcase_37 AC 33 ms
10,604 KB
testcase_38 AC 32 ms
10,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(int n, int m, int64_t a, int64_t b, const vector<int64_t>& l, const vector<int64_t>& r) {
    vector<vector<int> > g(n + 1);
    REP (j, m) {
        g[l[j]].push_back(r[j]);
        g[r[j]].push_back(l[j]);
    }

    // 0/1-bfs
    vector<int> dist(n + 1, INT_MAX);
    queue<int> que;
    REP (x, n + 1) {
        if (x <= a) {
            dist[x] = 0;
            que.push(x);
        }
    }
    while (not que.empty()) {
        int x = que.front();
        que.pop();
        if (b <= x) {
            return dist[x];
        }
        for (int y : g[x]) {
            if (dist[y] == INT_MAX) {
                dist[y] = dist[x] + 1;
                que.push(y);
            }
        }
    }
    return -1;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    constexpr char endl = '\n';
    int N, M;
    int64_t A, B;
    cin >> N >> M;
    vector<int64_t> L(M), R(M);
    cin >> A >> B;
    -- A;
    REP (i, M) {
        cin >> L[i] >> R[i]; -- L[i];
    }
    auto ans = solve(N, M, A, B, L, R);
    cout << ans << endl;
    return 0;
}
0