#include <bits/stdc++.h>
#define MP make_pair
#define F first
#define PB push_back
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 2e5 + 4;
vector <int> g[maxn];
int dis[maxn][2];
int h[maxn];
int maximal_path (int v) {
memset (h, -1, sizeof h);
int ret = 0;
queue <int> q;
q.push(v);
h[v] = 0;
while (!q.empty()) {
v = q.front();
q.pop();
ret = max (ret, h[v]);
for (auto u : g[v]) {
if (h[u] == -1 and dis[u][0] < dis[v][0] and dis[u][1] < dis[v][1]) {
h[u] = h[v] + 1;
q.push (u);
}
}
}
return ret;
}
void bfs (int src, bool wh) {
queue <int> q;
q.push(src);
dis[src][wh] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto u : g[v]) {
if (dis[u][wh] == -1) {
dis[u][wh] = dis[v][wh] + 1;
q.push(u);
}
}
}
}
int main (){
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
int s, t, a, b;
cin >> s >> t >> a >> b;
for (int e = 0; e < m; e ++) {
int v, u;
cin >> v >> u;
g[v].PB(u);
g[u].PB(v);
}
memset (dis, -1, sizeof dis);
bfs (a, 0);
bfs (b, 1);
int ans;
if (dis[t][0] < dis[s][0])
ans = -1;
else
ans = dis[t][0] - dis[s][0];
if (dis[t][1] < dis[s][1])
ans = -1;
else
ans = min (ans, dis[t][1] - dis[s][1]);
if (ans == -1)
return cout << -1 << endl, 0;
if (dis[s][0] == dis[s][1] and maximal_path(t) > maximal_path(s) + ans)
ans --;
return cout << ans << endl, 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6648 KB |
Output is correct |
2 |
Correct |
7 ms |
6752 KB |
Output is correct |
3 |
Correct |
8 ms |
7412 KB |
Output is correct |
4 |
Correct |
8 ms |
7456 KB |
Output is correct |
5 |
Correct |
8 ms |
7628 KB |
Output is correct |
6 |
Correct |
8 ms |
7628 KB |
Output is correct |
7 |
Correct |
8 ms |
7628 KB |
Output is correct |
8 |
Correct |
9 ms |
7628 KB |
Output is correct |
9 |
Correct |
8 ms |
7692 KB |
Output is correct |
10 |
Correct |
7 ms |
7692 KB |
Output is correct |
11 |
Partially correct |
9 ms |
7692 KB |
Partially correct |
12 |
Correct |
8 ms |
7692 KB |
Output is correct |
13 |
Correct |
10 ms |
7692 KB |
Output is correct |
14 |
Correct |
8 ms |
7692 KB |
Output is correct |
15 |
Correct |
8 ms |
7692 KB |
Output is correct |
16 |
Correct |
8 ms |
7692 KB |
Output is correct |
17 |
Correct |
8 ms |
7692 KB |
Output is correct |
18 |
Correct |
8 ms |
7692 KB |
Output is correct |
19 |
Correct |
9 ms |
7692 KB |
Output is correct |
20 |
Correct |
9 ms |
7760 KB |
Output is correct |
21 |
Correct |
7 ms |
7760 KB |
Output is correct |
22 |
Correct |
9 ms |
7760 KB |
Output is correct |
23 |
Correct |
8 ms |
7760 KB |
Output is correct |
24 |
Correct |
9 ms |
7760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
8928 KB |
Output is correct |
2 |
Correct |
40 ms |
9440 KB |
Output is correct |
3 |
Correct |
31 ms |
9440 KB |
Output is correct |
4 |
Correct |
51 ms |
9548 KB |
Output is correct |
5 |
Correct |
36 ms |
9548 KB |
Output is correct |
6 |
Correct |
37 ms |
9548 KB |
Output is correct |
7 |
Correct |
40 ms |
9548 KB |
Output is correct |
8 |
Correct |
33 ms |
9548 KB |
Output is correct |
9 |
Correct |
53 ms |
9568 KB |
Output is correct |
10 |
Correct |
178 ms |
13832 KB |
Output is correct |
11 |
Correct |
54 ms |
13832 KB |
Output is correct |
12 |
Correct |
78 ms |
13832 KB |
Output is correct |
13 |
Correct |
68 ms |
13832 KB |
Output is correct |
14 |
Correct |
50 ms |
13832 KB |
Output is correct |
15 |
Correct |
73 ms |
13832 KB |
Output is correct |
16 |
Correct |
72 ms |
13832 KB |
Output is correct |
17 |
Correct |
68 ms |
13832 KB |
Output is correct |
18 |
Correct |
70 ms |
13832 KB |
Output is correct |
19 |
Correct |
135 ms |
13832 KB |
Output is correct |
20 |
Correct |
230 ms |
15048 KB |
Output is correct |
21 |
Correct |
101 ms |
15048 KB |
Output is correct |
22 |
Correct |
102 ms |
15048 KB |
Output is correct |
23 |
Correct |
97 ms |
15048 KB |
Output is correct |
24 |
Correct |
100 ms |
15048 KB |
Output is correct |
25 |
Correct |
99 ms |
15048 KB |
Output is correct |
26 |
Correct |
90 ms |
15048 KB |
Output is correct |
27 |
Correct |
171 ms |
15048 KB |
Output is correct |
28 |
Correct |
124 ms |
15048 KB |
Output is correct |
29 |
Correct |
161 ms |
15048 KB |
Output is correct |
30 |
Correct |
230 ms |
15596 KB |
Output is correct |
31 |
Correct |
119 ms |
15596 KB |
Output is correct |
32 |
Correct |
128 ms |
15596 KB |
Output is correct |
33 |
Correct |
107 ms |
15596 KB |
Output is correct |
34 |
Correct |
121 ms |
15596 KB |
Output is correct |
35 |
Correct |
102 ms |
15596 KB |
Output is correct |
36 |
Correct |
113 ms |
15596 KB |
Output is correct |
37 |
Correct |
124 ms |
15596 KB |
Output is correct |
38 |
Correct |
120 ms |
15596 KB |
Output is correct |
39 |
Correct |
169 ms |
15596 KB |
Output is correct |
40 |
Correct |
239 ms |
15596 KB |
Output is correct |
41 |
Correct |
327 ms |
17488 KB |
Output is correct |