답안 #59887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
59887 2018-07-23T08:34:48 Z tmwilliamlin168 007 (CEOI14_007) C++14
100 / 100
333 ms 17976 KB
#include <bits/stdc++.h>
using namespace std;

inline int in() {
	int result = 0;
	char ch = getchar_unlocked();
	while(true) {
		if(ch >= '0' && ch <= '9')
			break;
		ch = getchar_unlocked();
	}
	result = ch-'0';
	while(true) {
		ch = getchar_unlocked();
		if (ch < '0' || ch > '9')
			break;
		result = result*10 + (ch - '0');
	}
	return result;
}
inline void out(int x) {
	if(!x) {
		putchar_unlocked('0');
		return;
	}
	if(x<0) {
		putchar_unlocked('-');
		x=-x;
	}
	int rev=x, c=0;
	while(!(rev%10)) {
		++c;
		rev/=10;
	}
	rev=0;
	while(x) {
		rev=rev*10+x%10;
		x/=10;
	}
	while(rev) {
		putchar_unlocked(rev%10+'0');
		rev/=10;
	}
	while(c--)
		putchar_unlocked('0');
}

const int mxN=2e5;
int n, m, s, d, a, b, da[mxN], db[mxN], qu[mxN], qh, qt, dp[mxN];
vector<int> adj[mxN];

inline void bfs(int s, int dist[mxN]) {
	qh=qt=0;
	memset(dist, 0x3F, 4*n);
	qu[qt++]=s;
	dist[s]=0;
	while(qh<qt) {
		int u=qu[qh++];
		for(int v : adj[u]) {
			if(dist[v]>n) {
				qu[qt++]=v;
				dist[v]=dist[u]+1;
			}
		}
	}
}

int cdp(int u) {
	if(!dp[u]) {
		dp[u]=da[u];
		for(int v : adj[u])
			if(da[v]==da[u]-1&&da[v]==db[v])
				dp[u]=min(cdp(v), dp[u]);
	}
	return dp[u];
}

int main() {
	n=in(), m=in(), s=in()-1, d=in()-1, a=in()-1, b=in()-1;
	while(m--) {
		int u=in()-1, v=in()-1;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	bfs(a, da);
	bfs(b, db);
	if(da[s]!=db[s]||da[d]!=db[d])
		out(max(min(da[d]-da[s], db[d]-db[s]), -1));
	else
		out(max(da[d]-da[s]-(cdp(d)<cdp(s)), -1));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4984 KB Output is correct
2 Correct 9 ms 5228 KB Output is correct
3 Correct 8 ms 5228 KB Output is correct
4 Correct 7 ms 5228 KB Output is correct
5 Correct 7 ms 5228 KB Output is correct
6 Correct 7 ms 5228 KB Output is correct
7 Correct 7 ms 5228 KB Output is correct
8 Correct 7 ms 5228 KB Output is correct
9 Correct 8 ms 5228 KB Output is correct
10 Correct 7 ms 5228 KB Output is correct
11 Correct 7 ms 5264 KB Output is correct
12 Correct 9 ms 5288 KB Output is correct
13 Correct 9 ms 5288 KB Output is correct
14 Correct 8 ms 5288 KB Output is correct
15 Correct 7 ms 5288 KB Output is correct
16 Correct 9 ms 5368 KB Output is correct
17 Correct 8 ms 5368 KB Output is correct
18 Correct 8 ms 5368 KB Output is correct
19 Correct 8 ms 5368 KB Output is correct
20 Correct 9 ms 5368 KB Output is correct
21 Correct 7 ms 5368 KB Output is correct
22 Correct 6 ms 5368 KB Output is correct
23 Correct 8 ms 5368 KB Output is correct
24 Correct 8 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7148 KB Output is correct
2 Correct 31 ms 8172 KB Output is correct
3 Correct 23 ms 8172 KB Output is correct
4 Correct 41 ms 8300 KB Output is correct
5 Correct 19 ms 8300 KB Output is correct
6 Correct 26 ms 8300 KB Output is correct
7 Correct 26 ms 8300 KB Output is correct
8 Correct 31 ms 8300 KB Output is correct
9 Correct 46 ms 8300 KB Output is correct
10 Correct 104 ms 12396 KB Output is correct
11 Correct 74 ms 12396 KB Output is correct
12 Correct 67 ms 12396 KB Output is correct
13 Correct 52 ms 12396 KB Output is correct
14 Correct 62 ms 12396 KB Output is correct
15 Correct 66 ms 12396 KB Output is correct
16 Correct 77 ms 12396 KB Output is correct
17 Correct 59 ms 12396 KB Output is correct
18 Correct 79 ms 12396 KB Output is correct
19 Correct 122 ms 12396 KB Output is correct
20 Correct 210 ms 14444 KB Output is correct
21 Correct 146 ms 14444 KB Output is correct
22 Correct 100 ms 14444 KB Output is correct
23 Correct 143 ms 14444 KB Output is correct
24 Correct 104 ms 14444 KB Output is correct
25 Correct 96 ms 14444 KB Output is correct
26 Correct 88 ms 14444 KB Output is correct
27 Correct 110 ms 14444 KB Output is correct
28 Correct 129 ms 14444 KB Output is correct
29 Correct 169 ms 14444 KB Output is correct
30 Correct 232 ms 15212 KB Output is correct
31 Correct 137 ms 15212 KB Output is correct
32 Correct 125 ms 15212 KB Output is correct
33 Correct 102 ms 15212 KB Output is correct
34 Correct 184 ms 15212 KB Output is correct
35 Correct 105 ms 15212 KB Output is correct
36 Correct 105 ms 15212 KB Output is correct
37 Correct 140 ms 15212 KB Output is correct
38 Correct 136 ms 15212 KB Output is correct
39 Correct 175 ms 15212 KB Output is correct
40 Correct 201 ms 15852 KB Output is correct
41 Correct 333 ms 17976 KB Output is correct