結果

問題 No.1244 Black Segment
ユーザー 👑 jupirojupiro
提出日時 2020-10-02 23:36:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,997 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 141,468 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2023-09-24 23:55:23
合計ジャッジ時間 4,494 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 20 ms
5,240 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 21 ms
5,436 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 24 ms
7,808 KB
testcase_19 AC 35 ms
8,776 KB
testcase_20 AC 35 ms
9,176 KB
testcase_21 AC 36 ms
9,532 KB
testcase_22 AC 39 ms
9,244 KB
testcase_23 AC 40 ms
9,296 KB
testcase_24 AC 41 ms
9,332 KB
testcase_25 AC 39 ms
9,452 KB
testcase_26 AC 37 ms
9,064 KB
testcase_27 AC 43 ms
9,664 KB
testcase_28 AC 39 ms
9,848 KB
testcase_29 AC 39 ms
9,452 KB
testcase_30 AC 37 ms
9,612 KB
testcase_31 AC 42 ms
9,796 KB
testcase_32 AC 42 ms
9,716 KB
testcase_33 AC 44 ms
9,652 KB
testcase_34 AC 40 ms
9,588 KB
testcase_35 AC 39 ms
10,184 KB
testcase_36 AC 41 ms
10,240 KB
testcase_37 AC 40 ms
10,172 KB
testcase_38 AC 41 ms
9,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 5000000;


int f(int a, int b) {
	return min(a, b);
}
template< typename Monoid >
struct SegmentTree {
	//using F = function< Monoid(Monoid, Monoid) >;

	int sz;
	vector< Monoid > seg;

	//const F f;
	const Monoid M1;

	SegmentTree(int n, /*const F f,*/ const Monoid& M1) : /*f(f),*/ M1(M1) {
		sz = 1;
		while (sz < n) sz <<= 1;
		seg.assign(2 * sz, M1);
	}

	void set(int k, const Monoid& x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; k--) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	void update(int k, const Monoid& x) {
		k += sz;
		seg[k] = x;
		while (k >>= 1) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	Monoid query(int a, int b) {
		Monoid L = M1, R = M1;
		if (a >= b) return M1;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}
};

vector<int> MultipleBFS(vector<int> start, vector<vector<int>>& g) {
	vector<int> d(g.size(), inf);
	queue<int> q;
	for (auto st : start) d[st] = 0, q.emplace(st);
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		for (int next : g[cur]) {
			if (chmin(d[next], d[cur] + 1)) {
				q.push(next);
			}
		}
	}
	return d;
}
void solve() {
	int n, m, A, B; cin >> n >> m >> A >> B; A--; B--;
	vector<pair<int, int>> vp(m);
	for (int i = 0; i < m; i++) cin >> vp[i].first >> vp[i].second, vp[i].first--;
	vector<vector<int>> g(n + 1);
	for (int i = 0; i < m; i++) {
		g[vp[i].first].emplace_back(vp[i].second);
		g[vp[i].second].emplace_back(vp[i].first);
	}
	vector<int> start; for (int i = 0; i <= A; i++) start.emplace_back(i);
	auto d = MultipleBFS(start, g);
	int res = *min_element(d.begin() + B + 1, d.end());
	if (res == inf) res = -1;
	cout << res << "\n";
}

int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int kkt = 1; while (kkt--) solve();
}
0