/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
int N, M;
int me, him, sv1, sv2;
vector <int> adj[N_MAX + 2];
void bfs (int start, int dist[]) {
fill(dist + 1, dist + N + 1, INT_MAX);
queue <int> q;
dist[start] = 0;
q.push(start);
while (q.empty() == false) {
int u = q.front(); q.pop();
for (int v : adj[u]) {
if (dist[v] == INT_MAX) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
int dist1[N_MAX + 2];
int dist2[N_MAX + 2];
bool seen[N_MAX + 2];
int dfs (int u) {
seen[u] = true;
int ret = 0;
for (int v : adj[u]) {
if (seen[v] == false && dist1[u] > dist1[v] && dist2[u] > dist2[v]) {
ret = max(ret, dfs(v) + 1);
}
}
return ret;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
cin >> me >> him >> sv1 >> sv2;
for (int i = 1; i <= M; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bfs(sv1, dist1);
bfs(sv2, dist2);
int wait1 = dist1[him] - dist1[me];
int wait2 = dist2[him] - dist2[me];
int wait;
if (wait1 != wait2) {
wait = min(wait1, wait2);
} else {
int decme = dfs(me);
fill(seen + 1, seen + N + 1, false);
int dechim = dfs(him);
if (wait1 + decme >= dechim) {
wait = wait1;
} else {
wait = wait1 - 1;
}
}
cout << (wait >= 0 ? wait : -1) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
4 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
6612 KB |
Output is correct |
2 |
Correct |
26 ms |
7636 KB |
Output is correct |
3 |
Correct |
18 ms |
6740 KB |
Output is correct |
4 |
Correct |
24 ms |
7676 KB |
Output is correct |
5 |
Correct |
20 ms |
6556 KB |
Output is correct |
6 |
Correct |
17 ms |
6788 KB |
Output is correct |
7 |
Correct |
19 ms |
6988 KB |
Output is correct |
8 |
Correct |
20 ms |
6996 KB |
Output is correct |
9 |
Correct |
29 ms |
7380 KB |
Output is correct |
10 |
Correct |
109 ms |
11644 KB |
Output is correct |
11 |
Correct |
36 ms |
8632 KB |
Output is correct |
12 |
Correct |
52 ms |
9488 KB |
Output is correct |
13 |
Correct |
40 ms |
8908 KB |
Output is correct |
14 |
Correct |
37 ms |
8268 KB |
Output is correct |
15 |
Correct |
48 ms |
9744 KB |
Output is correct |
16 |
Correct |
54 ms |
9700 KB |
Output is correct |
17 |
Correct |
45 ms |
9396 KB |
Output is correct |
18 |
Correct |
48 ms |
9376 KB |
Output is correct |
19 |
Correct |
77 ms |
10728 KB |
Output is correct |
20 |
Correct |
137 ms |
13316 KB |
Output is correct |
21 |
Correct |
76 ms |
12084 KB |
Output is correct |
22 |
Correct |
63 ms |
10484 KB |
Output is correct |
23 |
Correct |
70 ms |
12292 KB |
Output is correct |
24 |
Correct |
77 ms |
11664 KB |
Output is correct |
25 |
Correct |
64 ms |
11020 KB |
Output is correct |
26 |
Correct |
60 ms |
10700 KB |
Output is correct |
27 |
Correct |
71 ms |
11348 KB |
Output is correct |
28 |
Correct |
85 ms |
11340 KB |
Output is correct |
29 |
Correct |
107 ms |
12044 KB |
Output is correct |
30 |
Correct |
147 ms |
14036 KB |
Output is correct |
31 |
Correct |
83 ms |
13132 KB |
Output is correct |
32 |
Correct |
76 ms |
11336 KB |
Output is correct |
33 |
Correct |
80 ms |
11736 KB |
Output is correct |
34 |
Correct |
83 ms |
11892 KB |
Output is correct |
35 |
Correct |
73 ms |
12348 KB |
Output is correct |
36 |
Correct |
80 ms |
12616 KB |
Output is correct |
37 |
Correct |
104 ms |
12608 KB |
Output is correct |
38 |
Correct |
99 ms |
12592 KB |
Output is correct |
39 |
Correct |
102 ms |
12560 KB |
Output is correct |
40 |
Correct |
151 ms |
14300 KB |
Output is correct |
41 |
Correct |
195 ms |
16352 KB |
Output is correct |