Submission #37163

# Submission time Handle Problem Language Result Execution time Memory
37163 2017-12-21T16:58:34 Z cheater2k 007 (CEOI14_007) C++14
100 / 100
339 ms 18956 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n, m, s, d, a, b;
vector<int> G[N];
int fa[N], fb[N];

void bfs(int start, int f[]) {
	for (int i = 1; i <= n; ++i) f[i] = -1;
	f[start] = 0;
	queue <int> q; q.push(start);

	while(!q.empty()) {
		int u = q.front(); q.pop();
		for (int v : G[u]) if (f[v] == -1) {
			f[v] = f[u] + 1; q.push(v);
		}
	}
}

int maxdep(int start) {
	vector<int> d(n + 1, -1);
	queue <int> q; q.push(start); d[start] = 0;
	int ans = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		ans = max(ans, d[u]);
		for (int v : G[u]) if (fa[v] == fa[u] - 1 && fb[v] == fb[u] - 1 && d[v] == -1) {
			d[v] = d[u] + 1; q.push(v);
		}
	}
	return ans;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	cin >> s >> d >> a >> b;
	while(m--) {
		int u, v; cin >> u >> v;
		G[u].push_back(v); G[v].push_back(u);
	}
	bfs(a, fa);
	bfs(b, fb);
	int wa = fa[d] - fa[s];
	int wb = fb[d] - fb[s];
	if (wa != wb) {
		printf("%d\n", max(min(wa, wb), -1));
	} else {
		int maxs = maxdep(s), maxd = maxdep(d);
		printf("%d\n", max(wa - (wa + maxs < maxd), -1));
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8428 KB Output is correct
2 Correct 0 ms 8428 KB Output is correct
3 Correct 0 ms 8428 KB Output is correct
4 Correct 3 ms 8428 KB Output is correct
5 Correct 0 ms 8428 KB Output is correct
6 Correct 0 ms 8428 KB Output is correct
7 Correct 3 ms 8428 KB Output is correct
8 Correct 0 ms 8428 KB Output is correct
9 Correct 0 ms 8428 KB Output is correct
10 Correct 0 ms 8428 KB Output is correct
11 Correct 0 ms 8428 KB Output is correct
12 Correct 0 ms 8428 KB Output is correct
13 Correct 0 ms 8428 KB Output is correct
14 Correct 0 ms 8428 KB Output is correct
15 Correct 0 ms 8428 KB Output is correct
16 Correct 0 ms 8428 KB Output is correct
17 Correct 0 ms 8428 KB Output is correct
18 Correct 0 ms 8428 KB Output is correct
19 Correct 0 ms 8428 KB Output is correct
20 Correct 0 ms 8428 KB Output is correct
21 Correct 0 ms 8428 KB Output is correct
22 Correct 0 ms 8428 KB Output is correct
23 Correct 3 ms 8428 KB Output is correct
24 Correct 3 ms 8428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 9896 KB Output is correct
2 Correct 59 ms 10544 KB Output is correct
3 Correct 23 ms 9976 KB Output is correct
4 Correct 36 ms 10620 KB Output is correct
5 Correct 19 ms 9616 KB Output is correct
6 Correct 19 ms 9748 KB Output is correct
7 Correct 19 ms 10184 KB Output is correct
8 Correct 23 ms 10184 KB Output is correct
9 Correct 39 ms 10600 KB Output is correct
10 Correct 179 ms 14872 KB Output is correct
11 Correct 46 ms 11644 KB Output is correct
12 Correct 73 ms 12440 KB Output is correct
13 Correct 59 ms 11888 KB Output is correct
14 Correct 49 ms 11352 KB Output is correct
15 Correct 66 ms 12532 KB Output is correct
16 Correct 79 ms 12256 KB Output is correct
17 Correct 59 ms 12296 KB Output is correct
18 Correct 76 ms 12296 KB Output is correct
19 Correct 119 ms 13484 KB Output is correct
20 Correct 246 ms 16268 KB Output is correct
21 Correct 116 ms 14116 KB Output is correct
22 Correct 99 ms 13340 KB Output is correct
23 Correct 96 ms 14024 KB Output is correct
24 Correct 126 ms 13976 KB Output is correct
25 Correct 103 ms 13680 KB Output is correct
26 Correct 93 ms 13412 KB Output is correct
27 Correct 93 ms 14088 KB Output is correct
28 Correct 116 ms 14096 KB Output is correct
29 Correct 133 ms 14784 KB Output is correct
30 Correct 213 ms 16856 KB Output is correct
31 Correct 136 ms 14948 KB Output is correct
32 Correct 113 ms 14024 KB Output is correct
33 Correct 103 ms 14236 KB Output is correct
34 Correct 133 ms 14496 KB Output is correct
35 Correct 106 ms 14280 KB Output is correct
36 Correct 109 ms 14508 KB Output is correct
37 Correct 143 ms 14500 KB Output is correct
38 Correct 146 ms 15144 KB Output is correct
39 Correct 176 ms 15152 KB Output is correct
40 Correct 246 ms 16792 KB Output is correct
41 Correct 339 ms 18956 KB Output is correct