Submission #58291

#TimeUsernameProblemLanguageResultExecution timeMemory
58291evpipis007 (CEOI14_007)C++14
100 / 100
389 ms18044 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;

const int len = 2e5+5, inf = 1e9;
int dis[4][len];
int n, m, s1, s2, t1, t2;
queue<int> myq;
vector<int> adj[len];

void bfs(int st, int t){
    for (int i = 1; i <= n; i++)
        dis[t][i] = -1;

    dis[t][st] = 0, myq.push(st);
    while (!myq.empty()){
        int u = myq.front();
        myq.pop();

        for (int j = 0; j < adj[u].size(); j++){
            int v = adj[u][j];
            if (dis[t][v] == -1)
                dis[t][v] = dis[t][u]+1, myq.push(v);
        }
    }
}

int main(){
    scanf("%d %d %d %d %d %d", &n, &m, &s1, &s2, &t1, &t2);
    for (int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].pb(b);
        adj[b].pb(a);
    }

    int a1, a2, b1, b2, w1, w2, c = 0, d = 0;
    bfs(t1, 0);
    a1 = dis[0][s1], b1 = dis[0][s2], w1 = b1-a1;

    bfs(t2, 1);
    a2 = dis[1][s1], b2 = dis[1][s2], w2 = b2-a2;

    if (w1 > w2)
        swap(a1, a2), swap(b1, b2), swap(w1, w2);

    bfs(s1, 2), bfs(s2, 3);
    for (int i = 1; i <= n; i++){
        if (dis[0][i] == dis[1][i] && dis[0][i]+dis[2][i] == dis[0][s1])
            c = max(c, dis[2][i]);
        if (dis[0][i] == dis[1][i] && dis[0][i]+dis[3][i] == dis[0][s2])
            d = max(d, dis[3][i]);
    }

    if (b1 == b2 && a1 == a2 && c < d-w1)
        printf("%d\n", max(w1-1, -1));
    else
        printf("%d\n", max(w1, -1));
    return 0;
}

/*
9 10
9 8 1 2
1 2
2 3
3 1
2 4
4 5
5 2
5 7
7 6
7 8
7 9

6 6
1 2 3 4
1 5
5 6
6 3
6 4
1 2
3 4

6 7
5 6 1 2
6 3
1 2
1 3
2 3
1 5
2 4
5 4
*/

Compilation message (stderr)

007.cpp: In function 'void bfs(int, int)':
007.cpp:26:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < adj[u].size(); j++){
                         ~~^~~~~~~~~~~~~~~
007.cpp: In function 'int main()':
007.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d %d %d %d", &n, &m, &s1, &s2, &t1, &t2);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...