#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define V vector
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "LINE(" << __LINE__ << ") ->" << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 2e5 + 7;
int da[N], db[N], ds[N], dd[N];
vi G[N];
void BFS(int s, int* d) {
d[s] = 0;
queue<int> q({s});
while(q.size()) {
int u = q.front(); q.pop();
for(int v:G[u]) if(d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
vi tt[N];
int dp[N];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0);
memset(da, -1, sizeof da);
memset(db, -1, sizeof db);
memset(ds, -1, sizeof ds);
memset(dd, -1, sizeof dd);
int n, m;
cin >> n >> m;
int s, d, a, b;
cin >> s >> d >> a >> b;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].PB(v), G[v].PB(u);
}
BFS(a, da), BFS(b, db), BFS(s, ds), BFS(d, dd);
for(int i = 1; i <= n; i++) {
if(da[i] == db[i]) {
tt[da[i]].PB(i);
}
}
memset(dp, 0xc0, sizeof dp);
for(int i = 1; i <= n; i++) {
for(int j:tt[i]) {
dp[j] = 0;
for(int k:G[j]) if(da[k] == db[k] && da[k] == da[j] - 1)
dp[j] = max(dp[j], dp[k] + 1);
}
}
if(da[s] != db[s]) {
int can = min(da[d] - da[s], db[d] - db[s]);
if(can < 0) can = -1;
cout << can << '\n';
} else {
int need = INF;
for(int i = 1; i <= n; i++) {
if(da[i] < da[s] || db[i] < db[s]) {
need = min(need, dd[i]);
} else if(da[i] == da[s] && db[i] == db[s] && dp[i] > dp[s])
need = min(need, dd[i]);
}
cout << need - 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13516 KB |
Output is correct |
2 |
Correct |
9 ms |
13516 KB |
Output is correct |
3 |
Correct |
9 ms |
13516 KB |
Output is correct |
4 |
Correct |
10 ms |
13516 KB |
Output is correct |
5 |
Correct |
8 ms |
13516 KB |
Output is correct |
6 |
Correct |
9 ms |
13592 KB |
Output is correct |
7 |
Correct |
12 ms |
13516 KB |
Output is correct |
8 |
Correct |
9 ms |
13628 KB |
Output is correct |
9 |
Correct |
9 ms |
13560 KB |
Output is correct |
10 |
Correct |
9 ms |
13516 KB |
Output is correct |
11 |
Correct |
9 ms |
13540 KB |
Output is correct |
12 |
Correct |
9 ms |
13632 KB |
Output is correct |
13 |
Correct |
9 ms |
13564 KB |
Output is correct |
14 |
Correct |
9 ms |
13592 KB |
Output is correct |
15 |
Correct |
9 ms |
13516 KB |
Output is correct |
16 |
Correct |
9 ms |
13612 KB |
Output is correct |
17 |
Correct |
9 ms |
13656 KB |
Output is correct |
18 |
Correct |
10 ms |
13712 KB |
Output is correct |
19 |
Correct |
10 ms |
13644 KB |
Output is correct |
20 |
Correct |
9 ms |
13644 KB |
Output is correct |
21 |
Correct |
15 ms |
13652 KB |
Output is correct |
22 |
Correct |
9 ms |
13644 KB |
Output is correct |
23 |
Correct |
10 ms |
13632 KB |
Output is correct |
24 |
Correct |
9 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
14924 KB |
Output is correct |
2 |
Correct |
46 ms |
15588 KB |
Output is correct |
3 |
Correct |
32 ms |
14912 KB |
Output is correct |
4 |
Correct |
56 ms |
15656 KB |
Output is correct |
5 |
Correct |
33 ms |
14772 KB |
Output is correct |
6 |
Correct |
30 ms |
14924 KB |
Output is correct |
7 |
Correct |
45 ms |
15056 KB |
Output is correct |
8 |
Correct |
35 ms |
15064 KB |
Output is correct |
9 |
Correct |
51 ms |
15820 KB |
Output is correct |
10 |
Correct |
162 ms |
20164 KB |
Output is correct |
11 |
Correct |
67 ms |
16708 KB |
Output is correct |
12 |
Correct |
87 ms |
17096 KB |
Output is correct |
13 |
Correct |
67 ms |
16592 KB |
Output is correct |
14 |
Correct |
64 ms |
16240 KB |
Output is correct |
15 |
Correct |
80 ms |
17356 KB |
Output is correct |
16 |
Correct |
89 ms |
17436 KB |
Output is correct |
17 |
Correct |
74 ms |
16968 KB |
Output is correct |
18 |
Correct |
79 ms |
17008 KB |
Output is correct |
19 |
Correct |
121 ms |
18956 KB |
Output is correct |
20 |
Correct |
221 ms |
21624 KB |
Output is correct |
21 |
Correct |
122 ms |
18904 KB |
Output is correct |
22 |
Correct |
110 ms |
18188 KB |
Output is correct |
23 |
Correct |
110 ms |
18456 KB |
Output is correct |
24 |
Correct |
110 ms |
18680 KB |
Output is correct |
25 |
Correct |
109 ms |
18372 KB |
Output is correct |
26 |
Correct |
106 ms |
18020 KB |
Output is correct |
27 |
Correct |
131 ms |
18616 KB |
Output is correct |
28 |
Correct |
135 ms |
18588 KB |
Output is correct |
29 |
Correct |
161 ms |
20256 KB |
Output is correct |
30 |
Correct |
248 ms |
22232 KB |
Output is correct |
31 |
Correct |
137 ms |
19692 KB |
Output is correct |
32 |
Correct |
134 ms |
18628 KB |
Output is correct |
33 |
Correct |
134 ms |
18652 KB |
Output is correct |
34 |
Correct |
150 ms |
19076 KB |
Output is correct |
35 |
Correct |
122 ms |
19140 KB |
Output is correct |
36 |
Correct |
125 ms |
19448 KB |
Output is correct |
37 |
Correct |
154 ms |
19692 KB |
Output is correct |
38 |
Correct |
166 ms |
19524 KB |
Output is correct |
39 |
Correct |
173 ms |
19516 KB |
Output is correct |
40 |
Correct |
230 ms |
22292 KB |
Output is correct |
41 |
Correct |
336 ms |
24376 KB |
Output is correct |