Submission #414023

# Submission time Handle Problem Language Result Execution time Memory
414023 2021-05-29T19:07:11 Z sinamhdv 007 (CEOI14_007) C++11
100 / 100
254 ms 17116 KB
// CEOI14_007
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'

#define MAXN 200100
#define MAXE 600100

int n, m;
int s, d, a, b;
vector<int> adj[MAXN];
int h[3][MAXN];
bool mark[MAXN];
bool amark[2][MAXN], bmark[2][MAXN];
int ans;

void bfs(int r, int *h)
{
	memset(mark, 0, sizeof(mark));
	queue<int> q;
	mark[r] = true;
	q.push(r);

	while (!q.empty())
	{
		int v = q.front(); q.pop();
		for (int u : adj[v])
		{
			if (mark[u]) continue;
			mark[u] = true;
			h[u] = h[v] + 1;
			q.push(u);
		}
	}
}

void findpars(int r, int *h, bool *mark)
{
	queue<int> q;
	q.push(r);
	mark[r] = true;
	while (!q.empty())
	{
		int v = q.front(); q.pop();
		for (int u : adj[v])
		{
			if (mark[u] || h[u] >= h[v]) continue;
			mark[u] = true;
			q.push(u);
		}
	}
}

int32_t main(void)
{
	fast_io;
	cin >> n >> m;
	cin >> s >> d >> a >> b;
	FOR(i, 0, m)
	{
		int x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}

	bfs(a, h[1]);
	bfs(b, h[2]);

	if (h[1][s] > h[1][d] || h[2][s] > h[2][d]) return cout << "-1\n", 0;

	ans = min(h[1][d] - h[1][s], h[2][d] - h[2][s]);

	if (h[1][d] - h[1][s] != h[2][d] - h[2][s]) return cout << ans << endl, 0;

	findpars(s, h[1], amark[0]);
	findpars(s, h[2], bmark[0]);
	findpars(d, h[1], amark[1]);
	findpars(d, h[2], bmark[1]);

	int spy = 0, doc = 0;
	FOR(i, 1, n + 1)
	{
		if (amark[0][i] && bmark[0][i] && h[1][i] - h[1][s] == h[2][i] - h[2][s]) spy = max(spy, h[1][s] - h[1][i]);
		if (amark[1][i] && bmark[1][i] && h[1][i] - h[1][d] == h[2][i] - h[2][d]) doc = max(doc, h[2][d] - h[2][i]);
	}

	if (spy + h[1][d] - h[1][s] < doc) ans--;

	cout << ans << endl;

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 5196 KB Output is correct
2 Correct 3 ms 5196 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 3 ms 5196 KB Output is correct
7 Correct 3 ms 5196 KB Output is correct
8 Correct 3 ms 5196 KB Output is correct
9 Correct 3 ms 5232 KB Output is correct
10 Correct 3 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 3 ms 5196 KB Output is correct
15 Correct 3 ms 5196 KB Output is correct
16 Correct 4 ms 5196 KB Output is correct
17 Correct 4 ms 5196 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
21 Correct 4 ms 5196 KB Output is correct
22 Correct 4 ms 5196 KB Output is correct
23 Correct 4 ms 5196 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 7008 KB Output is correct
2 Correct 29 ms 7660 KB Output is correct
3 Correct 24 ms 7052 KB Output is correct
4 Correct 30 ms 7788 KB Output is correct
5 Correct 21 ms 6724 KB Output is correct
6 Correct 21 ms 6988 KB Output is correct
7 Correct 25 ms 7324 KB Output is correct
8 Correct 25 ms 7200 KB Output is correct
9 Correct 39 ms 7656 KB Output is correct
10 Correct 140 ms 12084 KB Output is correct
11 Correct 45 ms 9028 KB Output is correct
12 Correct 64 ms 10048 KB Output is correct
13 Correct 56 ms 9412 KB Output is correct
14 Correct 41 ms 8396 KB Output is correct
15 Correct 63 ms 10108 KB Output is correct
16 Correct 67 ms 9924 KB Output is correct
17 Correct 59 ms 9812 KB Output is correct
18 Correct 59 ms 9828 KB Output is correct
19 Correct 93 ms 11008 KB Output is correct
20 Correct 175 ms 13872 KB Output is correct
21 Correct 89 ms 12128 KB Output is correct
22 Correct 81 ms 11172 KB Output is correct
23 Correct 82 ms 11972 KB Output is correct
24 Correct 88 ms 12024 KB Output is correct
25 Correct 86 ms 11604 KB Output is correct
26 Correct 75 ms 11260 KB Output is correct
27 Correct 93 ms 12032 KB Output is correct
28 Correct 100 ms 12156 KB Output is correct
29 Correct 135 ms 12708 KB Output is correct
30 Correct 201 ms 14648 KB Output is correct
31 Correct 113 ms 13108 KB Output is correct
32 Correct 111 ms 12060 KB Output is correct
33 Correct 93 ms 12176 KB Output is correct
34 Correct 108 ms 12508 KB Output is correct
35 Correct 95 ms 12280 KB Output is correct
36 Correct 95 ms 12612 KB Output is correct
37 Correct 115 ms 12860 KB Output is correct
38 Correct 112 ms 13396 KB Output is correct
39 Correct 121 ms 13284 KB Output is correct
40 Correct 185 ms 14900 KB Output is correct
41 Correct 254 ms 17116 KB Output is correct