結果

問題 No.3463 Beltway
コンテスト
ユーザー edon8618
提出日時 2026-02-28 20:54:24
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 6,848 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,868 ms
コンパイル使用メモリ 392,904 KB
実行使用メモリ 89,592 KB
最終ジャッジ日時 2026-02-28 20:54:36
合計ジャッジ時間 12,401 ms
ジャッジサーバーID
(参考情報)
judge2 / judge7
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

#define ll long long
#define ld long double
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vs vector<string>
#define vc vector<char>
#define ull unsigned long long
#define chmax(a, b) a = max(a, (b))
#define chmin(a, b) a = min(a, (b))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()


// #define ll int
// #define ll int128_t
// #define ll int256_t
// #define ll cpp_int


constexpr ll inf = (1ll << 60);
// constexpr ll inf = (1 << 30);
// const double PI=3.1415926535897932384626433832795028841971;

// ll rui(ll a,ll b){
//     if(b==0)return 1;
//     if(b%2==1) return a*rui(a*a,b/2);
//     return rui(a*a,b/2);
// }

// vl fact;
// ll kai(ll n){
//     fact.resize(n,1);
//     rep(i,n-1)fact[i+1]=fact[i]*(i+1);
// }

using mint = modint998244353;//static_modint<998244353>
// using mint = modint1000000007;//static_modint<1000000007>
// using mint = static_modint<922267487>;   // 多分落とされにくい NOT ntt-friendly
// using mint = static_modint<469762049>;   // ntt-friendly
// using mint = static_modint<167772161>;   // ntt-friendly
// using mint = modint;//mint::set_mod(mod);

// ll const mod=1000000007ll;
// ll const mod=998244353ll;
// ll modrui(ll a,ll b,ll mod){
//     a%=mod;
//     if(b[i]==0)return 1;
//     if(b%2==1) return a*modrui(a*a%mod,b/2,mod)%mod;
//     return modrui(a*a%mod,b/2,mod)%mod;
// }

// ll inv(ll x){
//     x%=mod;
//     return modrui(x,mod-2);
// }

// void incr(vl &v,ll n){// n進法
//     ll k=v.size();
//     v[k-1]++;
//     ll now=k-1;
//     while (v[now]>=n)
//     {
//         v[now]=0;
//         if(now==0)break;
//         v[now-1]++;
//         now--;
//     }
//     return;
// }

// vector<mint> fact,invf;
// void init_modfact(ll sz){
//     fact.resize(sz);
//     invf.resize(sz);
//     fact[0]=1;
//     rep(i,sz-1){
//         fact[i+1]=fact[i]*(i+1);
//     }
//     invf[sz-1]=1/fact[sz-1];
//     for(ll i=sz-2; i>=0; i--){
//         invf[i]=invf[i+1]*(i+1);
//     }
// }
// mint choose(ll n,ll r){
//     if(n<r || r<0)return 0;
//     return fact[n]*invf[r]*invf[n-r];
// }
// vector<mint> modpow,invpow;
// void init_modpow(ll x,ll sz){
//     mint inv=1/mint(x);
//     modpow.assign(sz,1);
//     invpow.assign(sz,1);
//     rep(i,sz-1){
//         modpow[i+1]=modpow[i]*x;
//         invpow[i+1]=invpow[i]*inv;
//     }
// }

















/**
 * LowLink (Graph Analysis for Bridges & Articulation Points)
 * Reference: https://ei1333.github.io/luzhiled/snippets/graph/lowlink.html
 *
 * [概要]
 * 無向グラフにおける「橋 (Bridge)」と「関節点 (Articulation Point)」を
 * DFS 1回 O(V+E) で列挙する構造体。
 * 連結グラフでなくても動作し、DFS森として処理される。
 * DFS木の親情報(parent)も保持するため、グラフの構造解析にも利用可能。
 *
 * [メンバ変数]
 * - articulation: 関節点の頂点IDリスト。
 * (削除すると連結成分数が増える頂点)
 * - bridge:       橋となる辺 (u, v) のペアリスト。u < v で統一される。
 * (削除すると連結成分数が増える辺)
 * - parent:       DFS木における頂点 i の親頂点ID。根の場合は -1。
 * (これを使うことでDFS木の構造を外部から復元可能)
 *
 * [アルゴリズムの仕組み]
 * - ord[u]: DFSでの訪問順 (Time Stamp)
 * - low[u]: u から DFS木を降り、後退辺を最大1回利用して到達できる最小の ord
 *
 * [判定条件]
 * 1. 橋 (辺 u-v, uが親):
 * ord[u] < low[v]
 * -> 子 v は u を経由しないと u より上の祖先に辿り着けない。
 *
 * 2. 関節点 (頂点 u):
 * (a) u が根の場合: 子が 2 つ以上ある
 * (b) u が根以外の場合: ある子 v について ord[u] <= low[v]
 * -> 子 v は u を経由しないと u より上の祖先に辿り着けない。
 *
 * [使い方]
 * LowLink<vector<vector<int>>> lk(G);
 * lk.build();  // 構築 (必須)
 *
 * for(int v : lk.articulation) { ... }
 * for(auto [u, v] : lk.bridge) { ... }
 */
template< typename G >
struct LowLink{
	const G &g;
	vector<int> used, ord, low;
	vector<int> articulation;
	vector<pair<int, int>> bridge;
    vector<int> parent;

	LowLink(const G &g) : g(g) {}

	int dfs(int idx, int k, int par){
        parent[idx] = par;
		used[idx] = true;
		ord[idx] = k++;
		low[idx] = ord[idx];
		bool is_articulation = false;
		int cnt = 0;
		for (auto &to : g[idx]){
			if (!used[to]){
				++cnt;
				k = dfs(to, k, idx);
				low[idx] = min(low[idx], low[to]);
				is_articulation |= ~par && low[to] >= ord[idx];
				if (ord[idx] < low[to])
					bridge.emplace_back(minmax(idx, (int)to));
			}
			else if (to != par){
				low[idx] = min(low[idx], ord[to]);
			}
		}
		is_articulation |= par == -1 && cnt > 1;
		if (is_articulation) articulation.push_back(idx);
		return k;
	}

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



ll off=10000000;
void solve(){
    ll v,e,s,g;
    cin >> v >> e >> s >> g;
    s--;
    g--;


    vl a(e),b(e);
    vector<set<pair<ll,ll>>> h(v);
    rep(i,e){
        cin >> a[i] >> b[i];
        a[i]--;
        b[i]--;
        h[a[i]].insert({b[i],off-1});
        h[b[i]].insert({a[i],off-1});
    }

    {
        vector<vl> f(v);
        rep(i,e){
            f[a[i]].push_back(b[i]);
            f[b[i]].push_back(a[i]);
        }
        LowLink<vector<vl>> lnk(f);
        lnk.build();
        for(auto &p:lnk.bridge){
            ll a=p.first,b=p.second;
            h[a].erase({b,off-1});
            h[b].erase({a,off-1});
            h[a].insert({b,off});
            h[b].insert({a,off});
        }
    }

    ll n=v;
    vl dist(n,inf);
    vc decided(n,0);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
    dist[s]=0;
    pq.push({0,s});
    while(!pq.empty()){
        auto [d,x]=pq.top();
        pq.pop();

        if(decided[x])continue;
        decided[x]=1;
        for(auto &edge:h[x]){
            ll to=edge.first,cost=edge.second;
            if(!decided[to] && dist[to]>d+cost){
                dist[to]=d+cost;
                pq.push({d+cost,to});
            }
        }
    }
    if(dist[g]==inf)cout << -1 << endl;
    else{
        cout << (off-dist[g]%off)%off << endl;
    }
}


int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll t = 1;
    // cin >> t;
    while (t--) solve();
}
0