結果

問題 No.3463 Beltway
コンテスト
ユーザー cho435
提出日時 2026-02-28 15:52:31
言語 C++23(gnu拡張)
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 5,145 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,852 ms
コンパイル使用メモリ 396,924 KB
実行使用メモリ 80,792 KB
最終ジャッジ日時 2026-02-28 15:53:08
合計ジャッジ時間 9,785 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

// https://ei1333.github.io/library/graph/connected-components/bi-connected-components.hpp.html
#line 2 "graph/graph-template.hpp"

template <typename T = int>
struct Edge {
	int from, to;
	T cost;
	int idx;

	Edge() = default;

	Edge(int from, int to, T cost = 1, int idx = -1)
		: from(from), to(to), cost(cost), idx(idx) {
	}

	operator int() const {
		return to;
	}
};

template <typename T = int>
struct Graph {
	vector<vector<Edge<T>>> g;
	int es;

	Graph() = default;

	explicit Graph(int n) : g(n), es(0) {
	}

	size_t size() const {
		return g.size();
	}

	void add_directed_edge(int from, int to, T cost = 1) {
		g[from].emplace_back(from, to, cost, es++);
	}

	void add_edge(int from, int to, T cost = 1) {
		g[from].emplace_back(from, to, cost, es);
		g[to].emplace_back(to, from, cost, es++);
	}

	void read(int M,
			  int padding = -1,
			  bool weighted = false,
			  bool directed = false) {
		for (int i = 0; i < M; i++) {
			int a, b;
			cin >> a >> b;
			a += padding;
			b += padding;
			T c = T(1);
			if (weighted) cin >> c;
			if (directed) add_directed_edge(a, b, c);
			else add_edge(a, b, c);
		}
	}

	inline vector<Edge<T>>& operator[](const int& k) {
		return g[k];
	}

	inline const vector<Edge<T>>& operator[](const int& k) const {
		return g[k];
	}
};

template <typename T = int>
using Edges = vector<Edge<T>>;
#line 2 "graph/others/low-link.hpp"

#line 4 "graph/others/low-link.hpp"

/**
 * @brief Low Link(橋/関節点)
 * @see http://kagamiz.hatenablog.com/entry/2013/10/05/005213
 *
 */
template <typename T = int>
struct LowLink : Graph<T> {
   public:
	using Graph<T>::Graph;
	vector<int> ord, low, articulation;
	vector<Edge<T>> bridge;
	using Graph<T>::g;

	virtual void build() {
		used.assign(g.size(), 0);
		ord.assign(g.size(), 0);
		low.assign(g.size(), 0);
		int k = 0;
		for (int i = 0; i < (int)g.size(); i++) {
			if (!used[i]) k = dfs(i, k, -1);
		}
	}

	explicit LowLink(const Graph<T>& g) : Graph<T>(g) {
	}

   private:
	vector<int> used;

	int dfs(int idx, int k, int par) {
		used[idx] = true;
		ord[idx] = k++;
		low[idx] = ord[idx];
		bool is_articulation = false, beet = false;
		int cnt = 0;
		for (auto& to : g[idx]) {
			if (to == par && !exchange(beet, true)) {
				continue;
			}
			if (!used[to]) {
				++cnt;
				k = dfs(to, k, idx);
				low[idx] = min(low[idx], low[to]);
				is_articulation |= par >= 0 && low[to] >= ord[idx];
				if (ord[idx] < low[to]) bridge.emplace_back(to);
			} else {
				low[idx] = min(low[idx], ord[to]);
			}
		}
		is_articulation |= par == -1 && cnt > 1;
		if (is_articulation) articulation.push_back(idx);
		return k;
	}
};
#line 3 "graph/connected-components/bi-connected-components.hpp"

template <typename T = int>
struct BiConnectedComponents : LowLink<T> {
   public:
	using LowLink<T>::LowLink;
	using LowLink<T>::g;
	using LowLink<T>::ord;
	using LowLink<T>::low;

	vector<vector<Edge<T>>> bc;

	void build() override {
		LowLink<T>::build();
		used.assign(g.size(), 0);
		for (int i = 0; i < (int)used.size(); i++) {
			if (!used[i]) dfs(i, -1);
		}
	}

	explicit BiConnectedComponents(const Graph<T>& g) : Graph<T>(g) {
	}

   private:
	vector<int> used;
	vector<Edge<T>> tmp;

	void dfs(int idx, int par) {
		used[idx] = true;
		bool beet = false;
		for (auto& to : g[idx]) {
			if (to == par && !exchange(beet, true)) continue;
			if (!used[to] || ord[to] < ord[idx]) {
				tmp.emplace_back(to);
			}
			if (!used[to]) {
				dfs(to, idx);
				if (low[to] >= ord[idx]) {
					bc.emplace_back();
					for (;;) {
						auto e = tmp.back();
						bc.back().emplace_back(e);
						tmp.pop_back();
						if (e.idx == to.idx) break;
					}
				}
			}
		}
	}
};

void solve() {
	int n, m, s, g;
	cin >> n >> m >> s >> g;
	s--, g--;
	vector<array<int, 2>> edge(m);
	for (auto& [a, b] : edge) cin >> a >> b, a--, b--;

	BiConnectedComponents<> bb(n);
	for (auto [a, b] : edge) bb.add_edge(a, b);
	bb.build();

	vector<vector<pair<int, int>>> gg(n);

	for (auto ve : bb.bc) {
		if (ve.size() == 1) {
			auto e = ve[0];
			gg[e.from].push_back({e.to, 0});
			gg[e.to].push_back({e.from, 0});
			continue;
		}
		for (auto e : ve) {
			gg[e.from].push_back({e.to, 1});
			gg[e.to].push_back({e.from, 1});
		}
	}

	vector<ll> dist(n, 1e18);
	using arr = array<ll, 2>;
	priority_queue<arr, vector<arr>, greater<arr>> pq;
	pq.push({0, s});
	dist[s] = 0;
	while (!pq.empty()) {
		auto [d, nw] = pq.top();
		pq.pop();
		if (dist[nw] < d) continue;
		for (auto [nx, ad] : gg[nw]) {
			if (dist[nx] > dist[nw] + ad) {
				dist[nx] = dist[nw] + ad;
				pq.push({dist[nx], nx});
			}
		}
	}

	if (dist[g] == 1e18) cout << "-1\n";
	else cout << dist[g] << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	int t = 1;
	// cin >> t;
	while (t--) solve();
}
0