Submission #65020

# Submission time Handle Problem Language Result Execution time Memory
65020 2018-08-06T12:44:53 Z zadrga 007 (CEOI14_007) C++14
100 / 100
457 ms 21088 KB
/*
Idea:
- http://ceoi2014.informatik-olympiade.de/wp-content/uploads/2014/06/007-spoiler.pdf
*/

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1 << 27)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 200111

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

int da[maxn], db[maxn];
vector<int> adj[maxn];

void BFS(int zac, int d[]){
	for(int i = 0; i < maxn; i++)
		d[i] = INF;

	queue<int> q;
	d[zac] = 0;
	q.push(zac);
	while(!q.empty()){
		int x = q.front();
		q.pop();

		for(int v : adj[x]){
			if(d[x] + 1 < d[v]){
				d[v] = d[x] + 1;
				q.push(v);
			}
		}
	}
}

bool used[maxn];

int find_dist(int x){
	used[x] = 1;
	int ret = 0;
	for(int v : adj[x]){
		if(!used[v] && (da[v] + 1 == da[x]) && (db[v] + 1 == db[x]))
			ret = max(ret, 1 + find_dist(v));
	}
	return ret;
}

int main(){
	int n, m, agent, drnull, a, b;
	scanf("%d%d%d%d%d%d", &n, &m, &agent, &drnull, &a, &b);
	for(int i = 0; i < m; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		adj[x].pb(y);
		adj[y].pb(x);
	}

	BFS(a, da);
	BFS(b, db);

	int w1 = da[drnull] - da[agent];
	int w2 = db[drnull] - db[agent];

	int ans;	
	if(min(w1, w2) < 0){
		printf("-1\n");
		return 0;
	}	

	if(abs(w1 - w2) == 1)
		ans = min(w1, w2);

	if(w1 == w2){
		memset(used, 0, sizeof(used));
		int dist_drnull = find_dist(drnull);

		memset(used, 0, sizeof(used));
		int dist_agent = find_dist(agent);

		if(dist_agent >= dist_drnull - w1)
			ans = w1;
		else
			ans = w1 - 1;
	}

	printf("%d\n", ans);
	return 0;
}

Compilation message

007.cpp: In function 'int main()':
007.cpp:59: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, &agent, &drnull, &a, &b);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 6520 KB Output is correct
2 Correct 13 ms 6792 KB Output is correct
3 Correct 11 ms 6880 KB Output is correct
4 Correct 10 ms 6968 KB Output is correct
5 Correct 10 ms 6968 KB Output is correct
6 Correct 12 ms 6968 KB Output is correct
7 Correct 10 ms 6968 KB Output is correct
8 Correct 10 ms 6968 KB Output is correct
9 Correct 11 ms 7104 KB Output is correct
10 Correct 10 ms 7104 KB Output is correct
11 Correct 15 ms 7104 KB Output is correct
12 Correct 9 ms 7104 KB Output is correct
13 Correct 11 ms 7104 KB Output is correct
14 Correct 14 ms 7104 KB Output is correct
15 Correct 10 ms 7104 KB Output is correct
16 Correct 11 ms 7104 KB Output is correct
17 Correct 11 ms 7148 KB Output is correct
18 Correct 11 ms 7148 KB Output is correct
19 Correct 9 ms 7148 KB Output is correct
20 Correct 12 ms 7148 KB Output is correct
21 Correct 10 ms 7148 KB Output is correct
22 Correct 10 ms 7148 KB Output is correct
23 Correct 10 ms 7148 KB Output is correct
24 Correct 12 ms 7148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 8956 KB Output is correct
2 Correct 71 ms 10376 KB Output is correct
3 Correct 54 ms 10376 KB Output is correct
4 Correct 69 ms 11656 KB Output is correct
5 Correct 59 ms 11656 KB Output is correct
6 Correct 58 ms 11728 KB Output is correct
7 Correct 75 ms 12712 KB Output is correct
8 Correct 87 ms 12768 KB Output is correct
9 Correct 102 ms 13264 KB Output is correct
10 Correct 340 ms 17452 KB Output is correct
11 Correct 131 ms 17452 KB Output is correct
12 Correct 113 ms 17452 KB Output is correct
13 Correct 125 ms 17452 KB Output is correct
14 Correct 95 ms 17452 KB Output is correct
15 Correct 102 ms 17452 KB Output is correct
16 Correct 146 ms 17452 KB Output is correct
17 Correct 104 ms 17452 KB Output is correct
18 Correct 123 ms 17452 KB Output is correct
19 Correct 208 ms 17452 KB Output is correct
20 Correct 310 ms 18776 KB Output is correct
21 Correct 206 ms 18776 KB Output is correct
22 Correct 151 ms 18776 KB Output is correct
23 Correct 176 ms 18776 KB Output is correct
24 Correct 164 ms 18776 KB Output is correct
25 Correct 139 ms 18776 KB Output is correct
26 Correct 129 ms 18776 KB Output is correct
27 Correct 194 ms 18776 KB Output is correct
28 Correct 198 ms 18776 KB Output is correct
29 Correct 238 ms 18776 KB Output is correct
30 Correct 361 ms 19244 KB Output is correct
31 Correct 177 ms 19244 KB Output is correct
32 Correct 168 ms 19244 KB Output is correct
33 Correct 156 ms 19244 KB Output is correct
34 Correct 169 ms 19244 KB Output is correct
35 Correct 203 ms 19244 KB Output is correct
36 Correct 150 ms 19244 KB Output is correct
37 Correct 181 ms 19244 KB Output is correct
38 Correct 189 ms 19244 KB Output is correct
39 Correct 236 ms 19244 KB Output is correct
40 Correct 376 ms 19244 KB Output is correct
41 Correct 457 ms 21088 KB Output is correct