結果

問題 No.3463 Beltway
コンテスト
ユーザー AngrySadEight
提出日時 2026-02-28 14:15:10
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 6,003 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,011 ms
コンパイル使用メモリ 235,780 KB
実行使用メモリ 69,148 KB
最終ジャッジ日時 2026-02-28 15:50:50
合計ジャッジ時間 8,173 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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;

        dsu tree(N);
        rep(i, N) rep(j, graph[i].size()) tree.merge(graph[i][j].first, i);
        rep(i, N){
            if (tree.leader(i) != i) continue;
            dfs(i, 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;
}
0