Submission #110762

# Submission time Handle Problem Language Result Execution time Memory
110762 2019-05-12T07:56:55 Z Mahdi_Jfri 007 (CEOI14_007) C++14
100 / 100
403 ms 19556 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;

vector<int> adj[maxn];

int d[3][maxn] , x[maxn];

void bfs(int src , int k)
{
	memset(d[k] , 63  , sizeof d[k]);
	queue<int> q;
	q.push(src);
	d[k][src] = 0;

	while(!q.empty())
	{
		int v = q.front();
		q.pop();
		for(auto u : adj[v])
			if(d[k][u] > d[k][v] + 1)
			{
				d[k][u] = d[k][v] + 1;
				q.push(u);
			}
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n , m;
	cin >> n >> m;

	int a , b , s , t;

	cin >> s >> t >> a >> b;
	s-- , t-- , a-- , b--;

	for(int i = 0; i < m; i++)
	{
		int a , b;
		cin >> a >> b;
		a-- , b--;

		adj[a].pb(b);
		adj[b].pb(a);
	}

	bfs(a , 0) , bfs(b , 1) , bfs(t , 2);

	if(d[0][s] > d[0][t] || d[1][s] > d[1][t])
		return cout << -1 << endl , 0;

	vector<pair<int , int> > tmp;
	for(int i = 0; i < n; i++)
		tmp.pb({d[0][i] , i});

	sort(tmp.begin() , tmp.end());

	memset(x , -1 , sizeof x);
	for(auto sht : tmp)
	{
		int v = sht.second;

		x[v] = 0;
		for(auto u : adj[v])
			if(d[0][u] + 1 == d[0][v] && d[1][u] + 1 == d[1][v])
				x[v] = max(x[v] , x[u] + 1);
	}

	int res = 1e9;
	for(int t = 0; t < n; t++)
	{
		if(d[0][s] > d[0][t] || d[1][s] > d[1][t])
			res = min(res , d[2][t]);
		else if(d[0][s] == d[0][t] && d[1][s] == d[1][t] && x[t] > x[s])
			res = min(res , d[2][t]);
	}

	cout << res - 1 << endl;
}
















# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
3 Correct 11 ms 8192 KB Output is correct
4 Correct 9 ms 8192 KB Output is correct
5 Correct 8 ms 8192 KB Output is correct
6 Correct 2 ms 8192 KB Output is correct
7 Correct 9 ms 8192 KB Output is correct
8 Correct 9 ms 8192 KB Output is correct
9 Correct 8 ms 8192 KB Output is correct
10 Correct 8 ms 8192 KB Output is correct
11 Correct 10 ms 8192 KB Output is correct
12 Correct 10 ms 8192 KB Output is correct
13 Correct 10 ms 8320 KB Output is correct
14 Correct 10 ms 8192 KB Output is correct
15 Correct 11 ms 8192 KB Output is correct
16 Correct 11 ms 8192 KB Output is correct
17 Correct 11 ms 8192 KB Output is correct
18 Correct 12 ms 8192 KB Output is correct
19 Correct 11 ms 8192 KB Output is correct
20 Correct 11 ms 8228 KB Output is correct
21 Correct 9 ms 8192 KB Output is correct
22 Correct 11 ms 8192 KB Output is correct
23 Correct 9 ms 8192 KB Output is correct
24 Correct 10 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 9848 KB Output is correct
2 Correct 69 ms 10588 KB Output is correct
3 Correct 49 ms 9972 KB Output is correct
4 Correct 56 ms 10736 KB Output is correct
5 Correct 38 ms 9844 KB Output is correct
6 Correct 39 ms 10064 KB Output is correct
7 Correct 44 ms 10224 KB Output is correct
8 Correct 40 ms 10224 KB Output is correct
9 Correct 59 ms 10608 KB Output is correct
10 Correct 182 ms 15028 KB Output is correct
11 Correct 79 ms 12012 KB Output is correct
12 Correct 139 ms 12880 KB Output is correct
13 Correct 98 ms 12140 KB Output is correct
14 Correct 64 ms 9948 KB Output is correct
15 Correct 108 ms 12880 KB Output is correct
16 Correct 142 ms 13024 KB Output is correct
17 Correct 113 ms 12528 KB Output is correct
18 Correct 114 ms 12656 KB Output is correct
19 Correct 184 ms 13804 KB Output is correct
20 Correct 281 ms 16620 KB Output is correct
21 Correct 165 ms 14704 KB Output is correct
22 Correct 160 ms 13936 KB Output is correct
23 Correct 169 ms 14532 KB Output is correct
24 Correct 183 ms 14576 KB Output is correct
25 Correct 222 ms 14196 KB Output is correct
26 Correct 142 ms 14064 KB Output is correct
27 Correct 166 ms 14572 KB Output is correct
28 Correct 169 ms 14576 KB Output is correct
29 Correct 193 ms 15344 KB Output is correct
30 Correct 314 ms 17312 KB Output is correct
31 Correct 188 ms 15584 KB Output is correct
32 Correct 223 ms 14568 KB Output is correct
33 Correct 174 ms 14756 KB Output is correct
34 Correct 230 ms 15088 KB Output is correct
35 Correct 226 ms 14804 KB Output is correct
36 Correct 186 ms 15076 KB Output is correct
37 Correct 219 ms 15924 KB Output is correct
38 Correct 233 ms 15688 KB Output is correct
39 Correct 240 ms 15852 KB Output is correct
40 Correct 266 ms 17556 KB Output is correct
41 Correct 403 ms 19556 KB Output is correct