Submission #793389

#TimeUsernameProblemLanguageResultExecution timeMemory
793389Valaki2007 (CEOI14_007)C++14
69.67 / 100
184 ms17552 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back const int maxn = 2e5; int n, m; vector<int> graph[1 + maxn]; int g, s, a, b; vector<int> dg, ds, da, db; vector<int> bfs(int start) { vector<int> res(1 + n, -1); res[start] = 0; queue<int> q; q.push(start); while(!q.empty()) { int cur = q.front(); q.pop(); for(int nei : graph[cur]) { if(res[nei] == -1) { res[nei] = res[cur] + 1; q.push(nei); } } } return res; } void solve() { cin >> n >> m; cin >> g >> s >> a >> b; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; graph[x].pb(y); graph[y].pb(x); } dg = bfs(g), ds = bfs(s), da = bfs(a), db = bfs(b); if((da[s] < da[g]) || (db[s] < db[g])) { cout << "-1\n"; return; } int add_a = da[s] - 1 - da[g], add_b = db[s] - 1 - db[g]; bool extra_case = true; if(add_a != add_b) { add_a++, add_b++; extra_case = false; } int to_add = min(add_a, add_b); if(!extra_case) { cout << to_add << "\n"; return; } to_add++; da[g] += to_add; db[g] += to_add; for(int i = 1; i <= n; i++) { if(i != g) { dg[i] += to_add; } } int closest_g = n + 1, closest_s = n + 1; if(da[s] != db[s] || da[g] != db[g]) { //exit(1); } if(da[s] != da[g] || db[s] != db[g]) { exit(1); } for(int cur = 1; cur <= n; cur++) { /*int max_dab = max(da[cur], db[cur]), min_dab = min(da[cur], db[cur]); if((max_dab + closest_g == max(da[g], db[g])) && (min_dab + closest_g == min(da[g], db[g]))) { closest_g = min(closest_g, max_dab); } if((max_dab + closest_s == max(da[s], db[s]))) { closest_s = min(closest_s, max_dab); }*/ int max_dab = max(da[cur], db[cur]); int min_dab = max(da[cur], db[cur]); if(max_dab + dg[cur] == max(da[g], db[g])) { if(min_dab + dg[cur] == min(da[g], db[g])) { closest_g = min(closest_g, max_dab); } } if(max_dab + ds[cur] == max(da[s], db[s])) { if(min_dab + ds[cur] == min(da[s], db[s])) { closest_s = min(closest_s, max_dab); } } } if(closest_s < closest_g) { to_add--; } cout << to_add << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...