Submission #59887

#TimeUsernameProblemLanguageResultExecution timeMemory
59887tmwilliamlin168007 (CEOI14_007)C++14
100 / 100
333 ms17976 KiB
#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)); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...