Submission #37201

# Submission time Handle Problem Language Result Execution time Memory
37201 2017-12-22T14:04:23 Z aome 007 (CEOI14_007) C++14
100 / 100
366 ms 19956 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200005;
const int INF = 1e9;

int n, m;
int p0, p1, a[2];
int dis[2][N];
int f[N], dis2[N];
bool lose[N];
vector<int> G[N];

void bfs(int t) {
	for (int i = 1; i <= n; ++i) dis[t][i] = INF;
	queue<int> qu; dis[t][a[t]] = 0, qu.push(a[t]);
	while (qu.size()) {
		int u = qu.front(); qu.pop();
		for (auto v : G[u]) {
			if (dis[t][v] == INF) {
				dis[t][v] = dis[t][u] + 1, qu.push(v);
			}
		}
	}
}

void dfs(int u) {
	f[u] = 1;
	for (auto v : G[u]) {
		if (dis[0][v] + 1 != dis[0][u] || dis[1][v] + 1 != dis[1][u]) continue;
		if (!f[v]) dfs(v); f[u] = max(f[u], f[v] + 1);
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	cin >> p0 >> p1 >> a[0] >> a[1];
	for (int i = 1; i <= m; ++i) {
		int u, v; cin >> u >> v;
		G[u].push_back(v), G[v].push_back(u);
	}
	bfs(0), bfs(1);
	for (int i = 1; i <= n; ++i) {
		if (dis[0][i] < dis[0][p0] || dis[1][i] < dis[1][p0]) lose[i] = 1;
	}
	if (dis[0][p0] == dis[1][p0]) {
		for (int i = 1; i <= n; ++i) {
			if (dis[0][i] == dis[0][p0] && dis[1][i] == dis[1][p0] && !f[i]) dfs(i);
		}	
		for (int i = 1; i <= n; ++i) {
			if (f[i] > f[p0]) lose[i] = 1;
		}
	}
	for (int i = 1; i <= n; ++i) dis2[i] = -1;
	queue<int> qu; dis2[p1] = 0, qu.push(p1);
	while (qu.size()) {
		int u = qu.front(); qu.pop(); 
		if (lose[u]) {
			cout << dis2[u] - 1; return 0;
		}
		for (auto v : G[u]) {
			if (dis2[v] == -1) {
				dis2[v] = dis2[u] + 1, qu.push(v);
			}
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 10188 KB Output is correct
2 Correct 0 ms 10188 KB Output is correct
3 Correct 3 ms 10188 KB Output is correct
4 Correct 0 ms 10188 KB Output is correct
5 Correct 0 ms 10188 KB Output is correct
6 Correct 0 ms 10188 KB Output is correct
7 Correct 0 ms 10188 KB Output is correct
8 Correct 3 ms 10188 KB Output is correct
9 Correct 0 ms 10188 KB Output is correct
10 Correct 0 ms 10188 KB Output is correct
11 Correct 0 ms 10188 KB Output is correct
12 Correct 0 ms 10188 KB Output is correct
13 Correct 3 ms 10188 KB Output is correct
14 Correct 0 ms 10188 KB Output is correct
15 Correct 0 ms 10188 KB Output is correct
16 Correct 0 ms 10188 KB Output is correct
17 Correct 0 ms 10188 KB Output is correct
18 Correct 0 ms 10188 KB Output is correct
19 Correct 0 ms 10188 KB Output is correct
20 Correct 0 ms 10188 KB Output is correct
21 Correct 0 ms 10188 KB Output is correct
22 Correct 3 ms 10188 KB Output is correct
23 Correct 0 ms 10188 KB Output is correct
24 Correct 0 ms 10188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 11376 KB Output is correct
2 Correct 46 ms 12044 KB Output is correct
3 Correct 23 ms 11508 KB Output is correct
4 Correct 43 ms 12036 KB Output is correct
5 Correct 16 ms 11376 KB Output is correct
6 Correct 23 ms 11508 KB Output is correct
7 Correct 26 ms 11640 KB Output is correct
8 Correct 26 ms 11640 KB Output is correct
9 Correct 49 ms 12168 KB Output is correct
10 Correct 179 ms 16392 KB Output is correct
11 Correct 69 ms 12960 KB Output is correct
12 Correct 89 ms 13752 KB Output is correct
13 Correct 79 ms 13224 KB Output is correct
14 Correct 73 ms 12696 KB Output is correct
15 Correct 79 ms 13752 KB Output is correct
16 Correct 83 ms 14016 KB Output is correct
17 Correct 79 ms 13620 KB Output is correct
18 Correct 69 ms 13620 KB Output is correct
19 Correct 143 ms 14808 KB Output is correct
20 Correct 276 ms 17580 KB Output is correct
21 Correct 123 ms 15408 KB Output is correct
22 Correct 116 ms 14544 KB Output is correct
23 Correct 143 ms 15072 KB Output is correct
24 Correct 153 ms 15224 KB Output is correct
25 Correct 136 ms 14808 KB Output is correct
26 Correct 113 ms 14544 KB Output is correct
27 Correct 136 ms 15204 KB Output is correct
28 Correct 139 ms 15204 KB Output is correct
29 Correct 196 ms 15996 KB Output is correct
30 Correct 246 ms 18108 KB Output is correct
31 Correct 123 ms 16228 KB Output is correct
32 Correct 119 ms 15072 KB Output is correct
33 Correct 139 ms 15336 KB Output is correct
34 Correct 139 ms 15468 KB Output is correct
35 Correct 136 ms 15520 KB Output is correct
36 Correct 119 ms 15904 KB Output is correct
37 Correct 143 ms 16260 KB Output is correct
38 Correct 169 ms 16128 KB Output is correct
39 Correct 169 ms 16128 KB Output is correct
40 Correct 236 ms 17844 KB Output is correct
41 Correct 366 ms 19956 KB Output is correct