This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 250000 + 5;
int n , m , s , d , a , b;
vector<int> adj[maxn];
int ds[maxn] , dd[maxn] , da[maxn] , db[maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> n >> m >> s >> d >> a >> b;
for(int i = 1 ; i <= n ; ++i){
int u , v;cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
auto BFS = [&](int u , int d[maxn]){
queue<int> q;
fill_n(d,maxn,-1);
d[u] = 0;
q.push(u);
while(q.size()){
int u = q.front();q.pop();
for(int c : adj[u]){
if(d[c] == -1){
d[c] = d[u] + 1;
q.push(c);
}
}
}
};
BFS(s , ds);BFS(d , dd);BFS(a , da);BFS(b , db);
if(dd[a] + 1 <= ds[a] || dd[b] + 1 <= ds[b]){
return cout << -1 , 0;
}
int res = 0;
if(dd[a] - ds[a] == dd[b] - ds[b]){
int sres = 0 , dres = 0;
for(int i = 1 ; i <= n ; ++i){
if(ds[a] == da[i] + ds[i] && ds[b] == db[i] + ds[i]){
sres = max(sres , ds[i]);
}
if(dd[a] == da[i] + dd[i] && dd[b] == db[i] + dd[i]){
dres = max(dres , dd[i]);
}
}
// cout << sres << " " << dres << endl;
cout << dd[a] - ds[a] - (sres + dd[a] - ds[a] < dres);
}else{
cout << min(dd[a] - ds[a] , dd[b] - ds[b]);
}
}
Compilation message (stderr)
007.cpp: In function 'int main()':
007.cpp:54:9: warning: unused variable 'res' [-Wunused-variable]
int res = 0;
^~~
007.cpp:26:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:27:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |