# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
66080 | ikura355 | 007 (CEOI14_007) | C++14 | 534 ms | 18820 KiB |
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>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 1e9;
int n,m,a,b,s1,s2;
vector<int> way[maxn];
int d[2][maxn];
int q[maxn];
int p[maxn], wow[maxn];
int sssp(int k, int u) {
int l = 1, r = 0;
for(int i=1;i<=n;i++) d[k][i] = inf;
d[k][u] = 0; q[++r] = u;
while(l<=r) {
int u = q[l++];
for(auto v : way[u]) {
if(d[k][v] > d[k][u] + 1) {
d[k][v] = d[k][u] + 1; q[++r] = v;
}
}
}
}
bool cmp(int x, int y) {
if(d[0][x]!=d[0][y]) return d[0][x] < d[0][y];
if(d[1][x]!=d[1][y]) return d[1][x] < d[1][y];
return x<y;
}
int main() {
scanf("%d%d",&n,&m);
scanf("%d%d%d%d",&a,&b,&s1,&s2);
for(int i=1;i<=m;i++) {
int u,v; scanf("%d%d",&u,&v);
way[u].push_back(v); way[v].push_back(u);
}
sssp(0,s1); sssp(1,s2);
if(d[0][a]>d[0][b] || d[1][a]>d[1][b]) return !printf("-1");
int res = min(d[0][b]-d[0][a], d[1][b]-d[1][a]);
// printf("res = %d\n",res);
if(d[0][a] == d[1][a] && d[0][b] == d[1][b]) {
for(int i=1;i<=n;i++) p[i] = i;
sort(&p[1],&p[n+1],cmp);
for(int i=1;i<=n;i++) {
int u = p[i];
for(auto v : way[u]) {
if(d[0][v]==d[0][u]+1 && d[1][v]==d[1][u]+1) wow[v] = max(wow[v], wow[u]+1);
}
// printf("wow %d = %d\n",u,wow[u]);
}
if(wow[a]+res<wow[b]) res = res-1;
}
printf("%d",max(0,res));
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |