답안 #82369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
82369 2018-10-30T10:05:28 Z Dat160601 007 (CEOI14_007) C++17
100 / 100
275 ms 17532 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int read(){
	char p;
	while((p = getchar())){
		if(p < '0' || p > '9') continue;
		break;
	}
	int ret = p - '0';
	while((p = getchar())){
		if(p >= '0' && p <= '9'){
			ret *= 10;
			ret += p - '0';
		}
		else break;
	}
	return ret;
}

int n, m, u, v;
int s, d, a, b, disa[200007], disb[200007], dist[200007];
bool vis[200007];
queue <int> q;
vector <int> edge[200007];

int go(int st){
	memset(dist, 0, sizeof(dist));
	memset(vis, false, sizeof(vis));
	int res = 0;
	q.push(st);
	vis[st] = true;
	while(!q.empty()){
		u = q.front();
		q.pop();
		res = max(res, dist[u]);
		for(int v : edge[u]){
			if(vis[v]) continue;
			if(disa[u] == disa[v] + 1 && disb[u] == disb[v] + 1){
				vis[v] = true;
				dist[v] = dist[u] + 1;
				q.push(v);
			}
		}
	}
	return res;
}

int main(){
	n = read(), m = read();
	s = read(), d = read(), a = read(), b = read();
	for(int i = 1; i <= m; i++){
		u = read();
		v = read();
		edge[u].pb(v);
		edge[v].pb(u);
	}
	vis[a] = true;
	q.push(a);
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v : edge[u]){
			if(vis[v]) continue;
			vis[v] = true;
			disa[v] = disa[u] + 1;
			q.push(v);
		}
	}
	memset(vis, false, sizeof(vis));
	vis[b] = true;
	q.push(b);
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v : edge[u]){
			if(vis[v]) continue;
			vis[v] = true;
			disb[v] = disb[u] + 1;
			q.push(v);
		}
	}
	int cal_a = disa[d] - disa[s];
	int cal_b = disb[d] - disb[s];
	//cout << cal_a << " " << cal_b << endl;
	if(min(cal_a, cal_b) < 0){
		printf("-1");
		return 0;
	}
	if(cal_a != cal_b){
		cal_a = min(cal_a, cal_b);
		cout << cal_a;
		return 0;
	}
	int down_s = go(s), down_d = go(d);
	if(cal_a + down_s < down_d) cal_a--;
	if(cal_a < 0) cout << -1;
	else cout << cal_a;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5240 KB Output is correct
2 Correct 9 ms 6004 KB Output is correct
3 Correct 6 ms 6044 KB Output is correct
4 Correct 7 ms 6116 KB Output is correct
5 Correct 7 ms 6116 KB Output is correct
6 Correct 6 ms 6116 KB Output is correct
7 Correct 6 ms 6164 KB Output is correct
8 Correct 7 ms 6292 KB Output is correct
9 Correct 7 ms 6292 KB Output is correct
10 Correct 6 ms 6292 KB Output is correct
11 Correct 6 ms 6292 KB Output is correct
12 Correct 7 ms 6292 KB Output is correct
13 Correct 7 ms 6292 KB Output is correct
14 Correct 7 ms 6292 KB Output is correct
15 Correct 7 ms 6292 KB Output is correct
16 Correct 7 ms 6292 KB Output is correct
17 Correct 7 ms 6292 KB Output is correct
18 Correct 7 ms 6292 KB Output is correct
19 Correct 7 ms 6292 KB Output is correct
20 Correct 7 ms 6292 KB Output is correct
21 Correct 6 ms 6292 KB Output is correct
22 Correct 7 ms 6292 KB Output is correct
23 Correct 7 ms 6396 KB Output is correct
24 Correct 7 ms 6396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 7804 KB Output is correct
2 Correct 28 ms 8572 KB Output is correct
3 Correct 22 ms 8572 KB Output is correct
4 Correct 28 ms 8700 KB Output is correct
5 Correct 20 ms 8700 KB Output is correct
6 Correct 20 ms 8700 KB Output is correct
7 Correct 22 ms 8700 KB Output is correct
8 Correct 24 ms 8700 KB Output is correct
9 Correct 37 ms 8700 KB Output is correct
10 Correct 108 ms 12924 KB Output is correct
11 Correct 41 ms 12924 KB Output is correct
12 Correct 56 ms 12924 KB Output is correct
13 Correct 44 ms 12924 KB Output is correct
14 Correct 34 ms 12924 KB Output is correct
15 Correct 54 ms 12924 KB Output is correct
16 Correct 51 ms 12924 KB Output is correct
17 Correct 43 ms 12924 KB Output is correct
18 Correct 47 ms 12924 KB Output is correct
19 Correct 99 ms 12924 KB Output is correct
20 Correct 154 ms 14536 KB Output is correct
21 Correct 88 ms 14536 KB Output is correct
22 Correct 71 ms 14536 KB Output is correct
23 Correct 114 ms 14536 KB Output is correct
24 Correct 76 ms 14536 KB Output is correct
25 Correct 67 ms 14536 KB Output is correct
26 Correct 67 ms 14536 KB Output is correct
27 Correct 71 ms 14536 KB Output is correct
28 Correct 94 ms 14536 KB Output is correct
29 Correct 137 ms 14536 KB Output is correct
30 Correct 170 ms 15148 KB Output is correct
31 Correct 107 ms 15148 KB Output is correct
32 Correct 91 ms 15148 KB Output is correct
33 Correct 81 ms 15148 KB Output is correct
34 Correct 101 ms 15148 KB Output is correct
35 Correct 81 ms 15148 KB Output is correct
36 Correct 89 ms 15148 KB Output is correct
37 Correct 101 ms 15148 KB Output is correct
38 Correct 101 ms 15148 KB Output is correct
39 Correct 118 ms 15148 KB Output is correct
40 Correct 176 ms 15356 KB Output is correct
41 Correct 275 ms 17532 KB Output is correct