이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<int> al[n+1];
int s, d, a, b; cin >> s >> d >> a >> b;
for (int i = 0; i < m; ++i) {
int u, v; cin >> u >> v;
al[u].push_back(v);
al[v].push_back(u);
}
queue<int> q;
int dist1[n+1], dist2[n+1];
memset(dist1, -1, sizeof dist1);
memset(dist2, -1, sizeof dist2);
q.push(a); dist1[a] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto v : al[u]) {
if (dist1[v] == -1) {
dist1[v] = dist1[u]+1;
q.push(v);
}
}
}
q.push(b); dist2[b] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto v : al[u]) {
if (dist2[v] == -1) {
dist2[v] = dist2[u]+1;
q.push(v);
}
}
}
//cout << dist1[s] << " " << dist2[s] << endl;
//cout << dist1[d] << " " << dist2[d] << endl;
cout << max(-1, min(dist2[d] - dist2[s], dist1[d] - dist1[s])) << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |