#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 2e5 + 7;
ll disk [N], dist [N], diss1 [N], diss2 [N], n, m, k, t, s1, s2;
vector <ll> adj [N];
void BFS (ll *dis, ll v) {
fill(dis, dis + N, inf);
dis[v] = 0;
vector <ll> q (N); q[0] = v;
for (ll l = 0, r = 1; l < r; l++) {
v = q[l];
for (auto &u : adj[v]) if (dis[u] > dis[v] + 1) dis[u] = dis[v] + 1, q[r++] = u;
}
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n >> m >> k >> t >> s1 >> s2;
for (ll i = 0, v, u; i < m; i++) {
cin>> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
BFS(disk, k); BFS(dist, t); BFS(diss1, s1); BFS(diss2, s2);
ll ans1 = dist[s1] - disk[s1], ans2 = dist[s2] - disk[s2], d1 = -inf, d2 = -inf;
if (ans1 > ans2) swap(ans1, ans2);
if (ans1 < 0) kill(-1);
if (ans2 - ans1) kill(ans1);
for (ll v = 1; v <= n; v++) {
if (disk[v] + diss1[v] == disk[s1] && disk[v] + diss2[v] == disk[s2]) d1 = max(d1, disk[v]);
if (dist[v] + diss1[v] == dist[s1] && dist[v] + diss2[v] == dist[s2]) d2 = max(d2, dist[v]);
}
if (ans1 + d1 >= d2) cout<< ans1 << endl;
else cout<< ans1 - 1 << endl;
cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
11 12
8 1 5 6
7 8
1 11
11 2
2 3
3 4
4 5
4 6
5 6
7 9
9 5
7 10
10 6
? 1 ? 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8924 KB |
Output is correct |
2 |
Correct |
6 ms |
8924 KB |
Output is correct |
3 |
Correct |
6 ms |
8924 KB |
Output is correct |
4 |
Correct |
6 ms |
8924 KB |
Output is correct |
5 |
Correct |
6 ms |
8924 KB |
Output is correct |
6 |
Correct |
6 ms |
8924 KB |
Output is correct |
7 |
Correct |
6 ms |
8924 KB |
Output is correct |
8 |
Correct |
7 ms |
8924 KB |
Output is correct |
9 |
Correct |
8 ms |
8924 KB |
Output is correct |
10 |
Correct |
6 ms |
8924 KB |
Output is correct |
11 |
Correct |
6 ms |
8924 KB |
Output is correct |
12 |
Correct |
8 ms |
8924 KB |
Output is correct |
13 |
Correct |
7 ms |
8924 KB |
Output is correct |
14 |
Correct |
6 ms |
8924 KB |
Output is correct |
15 |
Correct |
6 ms |
8924 KB |
Output is correct |
16 |
Correct |
7 ms |
8924 KB |
Output is correct |
17 |
Correct |
7 ms |
8924 KB |
Output is correct |
18 |
Correct |
7 ms |
8924 KB |
Output is correct |
19 |
Correct |
7 ms |
8924 KB |
Output is correct |
20 |
Correct |
7 ms |
9180 KB |
Output is correct |
21 |
Correct |
6 ms |
8924 KB |
Output is correct |
22 |
Correct |
7 ms |
8924 KB |
Output is correct |
23 |
Correct |
7 ms |
8924 KB |
Output is correct |
24 |
Correct |
7 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
10716 KB |
Output is correct |
2 |
Correct |
37 ms |
11484 KB |
Output is correct |
3 |
Correct |
29 ms |
10844 KB |
Output is correct |
4 |
Correct |
42 ms |
11612 KB |
Output is correct |
5 |
Correct |
31 ms |
10716 KB |
Output is correct |
6 |
Correct |
27 ms |
10844 KB |
Output is correct |
7 |
Correct |
36 ms |
11100 KB |
Output is correct |
8 |
Correct |
38 ms |
11184 KB |
Output is correct |
9 |
Correct |
43 ms |
11868 KB |
Output is correct |
10 |
Correct |
151 ms |
20444 KB |
Output is correct |
11 |
Correct |
60 ms |
12892 KB |
Output is correct |
12 |
Correct |
90 ms |
14044 KB |
Output is correct |
13 |
Correct |
70 ms |
13276 KB |
Output is correct |
14 |
Correct |
52 ms |
12508 KB |
Output is correct |
15 |
Correct |
81 ms |
14172 KB |
Output is correct |
16 |
Correct |
78 ms |
14300 KB |
Output is correct |
17 |
Correct |
80 ms |
13916 KB |
Output is correct |
18 |
Correct |
86 ms |
13660 KB |
Output is correct |
19 |
Correct |
117 ms |
16092 KB |
Output is correct |
20 |
Correct |
203 ms |
21980 KB |
Output is correct |
21 |
Correct |
113 ms |
16092 KB |
Output is correct |
22 |
Correct |
102 ms |
15068 KB |
Output is correct |
23 |
Correct |
111 ms |
15964 KB |
Output is correct |
24 |
Correct |
126 ms |
15848 KB |
Output is correct |
25 |
Correct |
108 ms |
15484 KB |
Output is correct |
26 |
Correct |
106 ms |
15068 KB |
Output is correct |
27 |
Correct |
136 ms |
15964 KB |
Output is correct |
28 |
Correct |
146 ms |
16092 KB |
Output is correct |
29 |
Correct |
147 ms |
18140 KB |
Output is correct |
30 |
Correct |
205 ms |
22748 KB |
Output is correct |
31 |
Correct |
134 ms |
17244 KB |
Output is correct |
32 |
Correct |
130 ms |
15964 KB |
Output is correct |
33 |
Correct |
136 ms |
16220 KB |
Output is correct |
34 |
Correct |
183 ms |
16604 KB |
Output is correct |
35 |
Correct |
119 ms |
16220 KB |
Output is correct |
36 |
Correct |
116 ms |
16732 KB |
Output is correct |
37 |
Correct |
165 ms |
17500 KB |
Output is correct |
38 |
Correct |
158 ms |
17372 KB |
Output is correct |
39 |
Correct |
177 ms |
17500 KB |
Output is correct |
40 |
Correct |
210 ms |
21084 KB |
Output is correct |
41 |
Correct |
277 ms |
26076 KB |
Output is correct |