제출 #83989

#제출 시각아이디문제언어결과실행 시간메모리
83989Saboon007 (CEOI14_007)C++17
0 / 100
318 ms102156 KiB
#include <bits/stdc++.h>
#define MP make_pair
#define F first
#define PB push_back
#define S second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 2e5 + 4;

vector <int> g[maxn];
int dis[maxn][2];

void bfs (int src, bool wh) {
	queue <int> q;
	q.push(src);
	dis[src][wh] = 0;
	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto u : g[v]) {
			if (dis[u][wh] == -1) {
				dis[u][wh] = dis[v][wh] + 1;
				q.push(u);
			}
		}
	}
}

int main (){
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	int s, t, a, b;
	cin >> s >> t >> a >> b;
	for (int e = 0; e < m; e ++) {
		int v, u;
		cin >> v >> u;
		g[v].PB(u);
		g[u].PB(v);
	}

	memset (dis, -1, sizeof dis);
	bfs (a, 0);
	bfs (b, 1);
	int ans;
	if (dis[t][0] < dis[s][0])
		ans = -1;
	else
		ans = dis[t][0] - dis[s][0];
	
	if (dis[t][1] < dis[s][1])
		ans = -1;
	else
		ans = min (ans, dis[t][1] - dis[s][1]);
	return cout << ans << endl, 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...