Submission #70958

# Submission time Handle Problem Language Result Execution time Memory
70958 2018-08-23T20:33:39 Z spencercompton 007 (CEOI14_007) C++17
100 / 100
409 ms 103804 KB
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200000];
int n, m;
vector<int> use1;
vector<int> use2;
vector<bool> v;
vector<int> bfs(int src){
	vector<int> li;
	vector<int> ret;
	ret.resize(n);
	for(int i = 0; i<n; i++){
		ret[i] = -1;
	}
	li.push_back(src);
	ret[src] = 0;
	for(int i = 0; i<n; i++){
		int now = li[i];
		for(int j = 0; j<adj[now].size(); j++){
			int to = adj[now][j];
			if(ret[to]==-1){
				ret[to] = ret[now]+1;
				li.push_back(to);
			}
		}
	}
	return ret;
}
int dfs(int now){
	int ret = 1;
	v[now] = true;
	for(int i = 0; i<adj[now].size(); i++){
		int to = adj[now][i];
		if(use1[now]-1==use1[to] && use2[to]==use1[to] && !v[to]){
			ret = max(ret, 1 + dfs(to));
		}
	}
	return ret;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	int s, d, h1, h2;
	cin >> s >> d >> h1 >> h2;
	s--;
	d--;
	h1--;
	h2--;
	for(int i = 0; i<m; i++){
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	vector<int> d1 = bfs(h1);
	vector<int> d2 = bfs(h2);
	int dest = -1;
	if(d1[d]!=d2[d]){
		if(d1[d]<d2[d]){
			dest = 1;
		}
		else{
			dest = 2;
		}
	}
	if(d1[s]!=d2[s]){
		if(d1[s]<d2[s]){
			dest = 2;
		}
		else{
			dest = 1;
		}
	}
	int dif1 = d1[d]-d1[s];
	int dif2 = d2[d]-d2[s];
	if(dest!=-1){
		int z = min(dif1,dif2);
		if(z<-1){
			z = -1;
		}
		cout << z << endl;
		return 0;
	}
	else{
		int ans = -1;
		use1 = d1;
		use2 = d2;
		v.clear();
		v.resize(n);
		int a = dfs(d);
		v.clear();
		v.resize(n);
		int b = dfs(s);
		for(int i = 0; i<=n; i++){
			if(i+b<a){
				if(i+d1[s]+1<=d1[d]){
					ans = i;
				}
			}
			else{
				if(i+d1[s]<=d1[d]){
					ans = i;
				}
			}
		}
		cout << ans << endl;
	}
}

Compilation message

007.cpp: In function 'std::vector<int> bfs(int)':
007.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j<adj[now].size(); j++){
                  ~^~~~~~~~~~~~~~~~
007.cpp: In function 'int dfs(int)':
007.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i<adj[now].size(); i++){
                 ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 5232 KB Output is correct
3 Correct 7 ms 5392 KB Output is correct
4 Correct 7 ms 5392 KB Output is correct
5 Correct 6 ms 5392 KB Output is correct
6 Correct 8 ms 5392 KB Output is correct
7 Correct 6 ms 5392 KB Output is correct
8 Correct 7 ms 5392 KB Output is correct
9 Correct 7 ms 5408 KB Output is correct
10 Correct 7 ms 5408 KB Output is correct
11 Correct 7 ms 5432 KB Output is correct
12 Correct 8 ms 5432 KB Output is correct
13 Correct 8 ms 5484 KB Output is correct
14 Correct 7 ms 5484 KB Output is correct
15 Correct 7 ms 5484 KB Output is correct
16 Correct 7 ms 5504 KB Output is correct
17 Correct 7 ms 5524 KB Output is correct
18 Correct 7 ms 5532 KB Output is correct
19 Correct 8 ms 5624 KB Output is correct
20 Correct 7 ms 5624 KB Output is correct
21 Correct 7 ms 5624 KB Output is correct
22 Correct 8 ms 5740 KB Output is correct
23 Correct 7 ms 5740 KB Output is correct
24 Correct 8 ms 5740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8072 KB Output is correct
2 Correct 40 ms 9652 KB Output is correct
3 Correct 43 ms 9652 KB Output is correct
4 Correct 52 ms 11080 KB Output is correct
5 Correct 39 ms 11080 KB Output is correct
6 Correct 30 ms 11176 KB Output is correct
7 Correct 34 ms 11924 KB Output is correct
8 Correct 41 ms 12488 KB Output is correct
9 Correct 50 ms 13880 KB Output is correct
10 Correct 178 ms 23504 KB Output is correct
11 Correct 82 ms 23504 KB Output is correct
12 Correct 97 ms 24276 KB Output is correct
13 Correct 65 ms 24796 KB Output is correct
14 Correct 70 ms 25044 KB Output is correct
15 Correct 140 ms 27948 KB Output is correct
16 Correct 105 ms 29636 KB Output is correct
17 Correct 73 ms 30444 KB Output is correct
18 Correct 141 ms 31744 KB Output is correct
19 Correct 163 ms 35596 KB Output is correct
20 Correct 285 ms 43784 KB Output is correct
21 Correct 130 ms 44592 KB Output is correct
22 Correct 114 ms 45216 KB Output is correct
23 Correct 127 ms 48096 KB Output is correct
24 Correct 152 ms 49900 KB Output is correct
25 Correct 137 ms 51360 KB Output is correct
26 Correct 111 ms 52852 KB Output is correct
27 Correct 138 ms 55684 KB Output is correct
28 Correct 170 ms 57692 KB Output is correct
29 Correct 213 ms 61436 KB Output is correct
30 Correct 286 ms 68996 KB Output is correct
31 Correct 161 ms 70560 KB Output is correct
32 Correct 158 ms 71032 KB Output is correct
33 Correct 124 ms 73452 KB Output is correct
34 Correct 148 ms 75900 KB Output is correct
35 Correct 126 ms 77952 KB Output is correct
36 Correct 147 ms 80428 KB Output is correct
37 Correct 167 ms 83764 KB Output is correct
38 Correct 165 ms 85816 KB Output is correct
39 Correct 192 ms 88204 KB Output is correct
40 Correct 249 ms 94492 KB Output is correct
41 Correct 409 ms 103804 KB Output is correct