Submission #246194

# Submission time Handle Problem Language Result Execution time Memory
246194 2020-07-08T10:49:01 Z mieszko11b 007 (CEOI14_007) C++14
100 / 100
331 ms 18356 KB
#include <bits/stdc++.h>

using namespace std;

int n, m;
int s, d, a, b;
vector<int> G[200007];
int dista[200007], distb[200007], dists[200007], distd[200007];
bool vis[200007];

void run_bfs(int w, int *dist) {
	memset(vis, 0, sizeof vis);
	vis[w] = 1;
	dist[w] = 0;
	queue<int> Q;
	Q.push(w);
	
	while(!Q.empty()) {
		int u=  Q.front();
		Q.pop();
		
		for(int v : G[u]) {
			if(!vis[v]) {
				vis[v] = 1;
				dist[v] = dist[u] + 1;
				Q.push(v);
			}
		}
	}
}

int main() {
	scanf("%d%d%d%d%d%d", &n, &m, &s, &d, &a, &b);
	while(m--) {
		int a, b;
		scanf("%d%d", &a, &b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	
	run_bfs(s, dists);
	run_bfs(d, distd);
	run_bfs(a, dista);
	run_bfs(b, distb);
	int sa = dists[a], sb = dists[b];
	int da = distd[a], db = distd[b];
	
	if(sa != sb || da != db) {
		int val = min(da - sa, db - sb);
		if(val < 0) val = -1;
		
		printf("%d\n", val);
	} else {
		int fs = 0, fd = 0;
		for(int i = 1 ; i <= n ; i++) {
			if(dists[i] + dista[i] == sa && dists[i] + distb[i] == sb)
				fs = max(fs, dists[i]);
			if(distd[i] + dista[i] == da && distd[i] + distb[i] == db)
				fd = max(fd, distd[i]);
		}
		
		int val = min(da - sa, db - sb);
		if(sa - fs > da - fd)
			val--;
			
		if(val < 0) val = -1;
			
		printf("%d\n", val);
	}
	
	return 0;
}

Compilation message

007.cpp: In function 'int main()':
007.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d%d%d", &n, &m, &s, &d, &a, &b);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:36:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 7 ms 5248 KB Output is correct
5 Correct 7 ms 5248 KB Output is correct
6 Correct 7 ms 5248 KB Output is correct
7 Correct 7 ms 5248 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5248 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 7 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 8 ms 5248 KB Output is correct
17 Correct 8 ms 5248 KB Output is correct
18 Correct 8 ms 5248 KB Output is correct
19 Correct 8 ms 5248 KB Output is correct
20 Correct 8 ms 5248 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 8 ms 5248 KB Output is correct
23 Correct 7 ms 5248 KB Output is correct
24 Correct 8 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 7596 KB Output is correct
2 Correct 41 ms 8440 KB Output is correct
3 Correct 34 ms 7672 KB Output is correct
4 Correct 55 ms 8440 KB Output is correct
5 Correct 32 ms 7416 KB Output is correct
6 Correct 32 ms 7800 KB Output is correct
7 Correct 37 ms 7928 KB Output is correct
8 Correct 38 ms 7928 KB Output is correct
9 Correct 52 ms 8312 KB Output is correct
10 Correct 182 ms 12664 KB Output is correct
11 Correct 76 ms 9848 KB Output is correct
12 Correct 99 ms 10872 KB Output is correct
13 Correct 77 ms 10232 KB Output is correct
14 Correct 62 ms 9464 KB Output is correct
15 Correct 94 ms 11000 KB Output is correct
16 Correct 96 ms 11384 KB Output is correct
17 Correct 98 ms 10744 KB Output is correct
18 Correct 90 ms 10744 KB Output is correct
19 Correct 156 ms 12024 KB Output is correct
20 Correct 247 ms 14840 KB Output is correct
21 Correct 152 ms 13304 KB Output is correct
22 Correct 127 ms 12176 KB Output is correct
23 Correct 170 ms 13048 KB Output is correct
24 Correct 123 ms 12912 KB Output is correct
25 Correct 133 ms 12664 KB Output is correct
26 Correct 109 ms 12280 KB Output is correct
27 Correct 139 ms 13176 KB Output is correct
28 Correct 157 ms 13176 KB Output is correct
29 Correct 182 ms 13688 KB Output is correct
30 Correct 248 ms 15480 KB Output is correct
31 Correct 158 ms 14328 KB Output is correct
32 Correct 184 ms 13176 KB Output is correct
33 Correct 189 ms 13432 KB Output is correct
34 Correct 171 ms 13692 KB Output is correct
35 Correct 135 ms 13432 KB Output is correct
36 Correct 137 ms 13688 KB Output is correct
37 Correct 183 ms 14756 KB Output is correct
38 Correct 197 ms 14584 KB Output is correct
39 Correct 251 ms 14552 KB Output is correct
40 Correct 241 ms 16120 KB Output is correct
41 Correct 331 ms 18356 KB Output is correct