結果
| 問題 | No.3463 Beltway |
| コンテスト | |
| ユーザー |
Tehom
|
| 提出日時 | 2026-03-02 23:49:15 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 85 ms / 2,000 ms |
| コード長 | 2,272 bytes |
| 記録 | |
| コンパイル時間 | 3,184 ms |
| コンパイル使用メモリ | 288,480 KB |
| 実行使用メモリ | 40,872 KB |
| 最終ジャッジ日時 | 2026-03-02 23:49:23 |
| 合計ジャッジ時間 | 4,813 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 17 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define ll long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define repl(i,a,b) for(ll i=(a);i<(b);i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
template <typename T> bool chmin(T &a,T b){if(a>b){a=b;return true;} return false;}
template <typename T> bool chmax(T &a,T b){if(a<b){a=b;return true;} return false;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,s,g; cin >> n >> m >> s >> g;
s--,g--;
vector<vector<int>> to(n);
vector<pair<int,int>> edge;
rep(i,0,m){
int u,v; cin >> u >> v;
u--,v--;
to[u].push_back(v);
to[v].push_back(u);
edge.push_back({u,v});
}
vector<int> ord(n,-1),low(n);
int num=0;
auto dfs=[&](auto dfs,int pr,int v) -> void{
ord[v]=num;
num++;
low[v]=ord[v];
for(auto nv:to[v]){
if(nv == pr || ord[nv] == -1) continue;
chmin(low[v],ord[nv]);
}
for(auto nv:to[v]){
if(nv == pr || ord[nv] != -1) continue;
dfs(dfs,v,nv);
chmin(low[v],low[nv]);
}
};
dfs(dfs,-1,s);
// rep(i,0,n){
// cout << ord[i] << " " << low[i] << '\n';
// }
vector<vector<pair<int,int>>> G(n);
rep(i,0,m){
auto [u,v]=edge[i];
if(ord[u]>ord[v]) swap(u,v);
if(ord[u]<low[v]){
// cout << i << ":" << 1 << "\n";
G[u].push_back({v,1});
G[v].push_back({u,1});
}
else{
// cout << i << ":" << 0 << "\n";
G[u].push_back({v,0});
G[v].push_back({u,0});
}
}
int INF=1e9;
vector<int> dist(n,INF);
queue<int> que;
que.push(g);
dist[g]=0;
while(!que.empty()){
auto v=que.front();
que.pop();
for(auto nv:to[v]){
if(chmin(dist[nv],dist[v]+1)) que.push(nv);
}
}
if(dist[s] == INF){
cout << -1 << '\n';
return 0;
}
deque<int> dq;
dq.push_back(s);
vector<int> d(n,INF);
d[s]=0;
while(!dq.empty()){
auto v=dq.front();
dq.pop_front();
for(auto [nv,cost]:G[v]){
if(dist[nv] != dist[v]-1) continue;
if(cost == 0){
if(chmin(d[nv],d[v])) dq.push_front(nv);
}
else{
if(chmin(d[nv],d[v]+1)) dq.push_back(nv);
}
}
}
cout << dist[s]-d[g] << '\n';
}
Tehom