結果
| 問題 |
No.1244 Black Segment
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-17 00:00:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 41 ms / 2,000 ms |
| コード長 | 1,557 bytes |
| コンパイル時間 | 2,160 ms |
| コンパイル使用メモリ | 203,840 KB |
| 最終ジャッジ日時 | 2025-01-15 10:21:02 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#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;
}