結果
| 問題 | No.3463 Beltway |
| コンテスト | |
| ユーザー |
AngrySadEight
|
| 提出日時 | 2026-02-28 14:11:31 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,825 bytes |
| 記録 | |
| コンパイル時間 | 5,311 ms |
| コンパイル使用メモリ | 235,304 KB |
| 実行使用メモリ | 69,232 KB |
| 最終ジャッジ日時 | 2026-02-28 15:50:46 |
| 合計ジャッジ時間 | 7,906 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 16 WA * 1 |
ソースコード
#include <algorithm>
#include <atcoder/all>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define mod1 1000000007
#define mod2 998244353
#define mod3 100000007
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vvl vector<vector<ll>>
#define vvb vector<vector<bool>>
#define vvvi vector<vector<vector<int>>>
#define vvvl vector<vector<vector<ll>>>
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define vvpii vector<vector<pair<int, int>>>
#define vvpll vector<vector<pair<ll, ll>>>
template <typename T>
void debug(T e) {
cerr << e << endl;
}
template <typename T>
void debug(vector<T> &v) {
rep(i, v.size()) { cerr << v[i] << " "; }
cerr << endl;
}
template <typename T>
void debug(vector<vector<T>> &v) {
rep(i, v.size()) {
rep(j, v[i].size()) { cerr << v[i][j] << " "; }
cerr << endl;
}
}
template <typename T>
void debug(vector<pair<T, T>> &v) {
rep(i, v.size()) { cerr << v[i].first << " " << v[i].second << endl; }
}
template <typename T>
void debug(set<T> &st) {
for (auto itr = st.begin(); itr != st.end(); itr++) {
cerr << *itr << " ";
}
cerr << endl;
}
template <typename T>
void debug(multiset<T> &ms) {
for (auto itr = ms.begin(); itr != ms.end(); itr++) {
cerr << *itr << " ";
}
cerr << endl;
}
template <typename T>
void debug(map<T, T> &mp) {
for (auto itr = mp.begin(); itr != mp.end(); itr++) {
cerr << itr->first << " " << itr->second << endl;
}
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << H << " ";
debug_out(T...);
}
using mint = modint998244353;
void debug_mint1(vector<mint> &vec) {
for (int i = 0; i < vec.size(); i++) {
cerr << vec[i].val() << " ";
}
cerr << endl;
}
void debug_mint2(vector<vector<mint>> &vec) {
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
cerr << vec[i][j].val() << " ";
}
cerr << endl;
}
}
// LowLink ライブラリ。verify 済み。
struct LowLink {
ll N;
ll M;
vector<ll> bridges;
vector<vector<pll>> graph;
vector<ll> ord;
vector<ll> low;
vector<bool> isvisited;
LowLink(ll N_, ll M_, vector<ll> &u, vector<ll> &v) {
N = N_;
M = M_;
vector<pll> emp(0);
for (ll i = 0; i < N; i++) {
graph.push_back(emp);
}
for (ll i = 0; i < M; i++) {
graph[u[i]].push_back(make_pair(v[i], i));
graph[v[i]].push_back(make_pair(u[i], i));
}
ord.assign(N, -1);
low.assign(N, -1);
isvisited.assign(N, false);
}
void dfs(ll v, ll now_ord, ll prev) {
isvisited[v] = true;
ord[v] = now_ord;
low[v] = now_ord;
now_ord++;
for (pll x : graph[v]) {
ll next_e = x.first;
if (!isvisited[next_e]) {
dfs(next_e, now_ord, v);
low[v] = min(low[v], low[next_e]);
if (ord[v] < low[next_e]) {
ll e_idx = x.second;
bridges.push_back(e_idx);
}
} else if (next_e != prev) {
low[v] = min(low[v], ord[next_e]);
}
}
}
vector<ll> find_bridges() {
ll now_ord = 0;
dfs(0, now_ord, -1);
// debug(ord);
// debug(low);
return bridges;
}
};
int main() {
ll N, M, S, G;
cin >> N >> M >> S >> G;
S--;
G--;
vector<ll> A(M);
vector<ll> B(M);
rep(i, M){
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
LowLink llink(N, M, A, B);
vector<ll> bridges = llink.find_bridges();
vector<bool> isb(M, false);
for (ll b: bridges) isb[b] = true;
ll INF = 100000023LL;
vector<pll> dist(N, make_pair(INF, INF));
dist[0] = make_pair(0, 0);
vector<vector<pll>> graph(N, vector<pll>(0));
for (ll i = 0; i < M; i++){
if (isb[i]){
graph[A[i]].push_back(make_pair(B[i], 1));
graph[B[i]].push_back(make_pair(A[i], 1));
}
else{
graph[A[i]].push_back(make_pair(B[i], 0));
graph[B[i]].push_back(make_pair(A[i], 0));
}
}
priority_queue<pair<pll, ll>, vector<pair<pll, ll>>, greater<pair<pll, ll>>> pque;
pque.push(make_pair(make_pair(0, 0), S));
while (pque.size()){
pair<pll, ll> pp = pque.top();
pque.pop();
pll pd = pp.first;
ll pe = pp.second;
if (dist[pe] < pd) continue;
for (pll x: graph[pe]){
pll new_c = make_pair(pd.first + 1, pd.second + x.second);
if (dist[x.first] > new_c){
dist[x.first] = new_c;
pque.push(make_pair(new_c, x.first));
}
}
}
// debug(dist);
if (dist[G].first == INF) cout << -1 << endl;
else cout << dist[G].first - dist[G].second << endl;
}
AngrySadEight