Submission #257824

#TimeUsernameProblemLanguageResultExecution timeMemory
257824doowey007 (CEOI14_007)C++14
0 / 100
365 ms24968 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)2e5 + 10;
vector<int> T[N];

int c = 0;
int d[4][N];
int n;

void bfs(int node){
    for(int i = 1; i <= n; i ++ )
        d[c][i] = (int)1e9;
    queue<int> bf;
    bf.push(node);
    d[c][node] = 0;
    while(!bf.empty()){
        node = bf.front();
        bf.pop();
        for(auto x : T[node]){
            if(d[c][x] > d[c][node] + 1){
                d[c][x] = d[c][node] + 1;
                bf.push(x);
            }
        }
    }
    c ++ ;
}

int main(){
    fastIO;
    int m;
    cin >> n >> m;
    int a, b;
    cin >> a >> b;
    int n0, n1;
    cin >> n0 >> n1;
    int u, v;
    for(int i = 0 ; i < m ; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        T[v].push_back(u);
    }
    bfs(a);
    bfs(b);
    bfs(n0);
    bfs(n1);
    int d0 = d[1][n0] - d[0][n0];
    int d1 = d[1][n1] - d[0][n1];
    if(d0 == d1 + 1){
        cout << max(-1,d1) << "\n";
    }
    else if(d1 == d0 + 1){
        cout << max(-1,d0) << "\n";
    }
    else{
        int cc = 0;
        int dd = 0;
        for(int i = 1; i <= n; i ++ ){
            if(d[0][i] + d[3][i] == d[0][n0] && d[0][i] + d[4][i] == d[0][n1]){
                cc = max(cc, d[0][i]);
            }
            if(d[1][i] + d[3][i] == d[1][n0] && d[1][i] + d[4][i] == d[1][n1]){
                dd = max(dd, d[1][i]);
            }
        }
        if(cc + d0 >= dd){
            cout << max(-1,d0) << "\n";
        }
        else{
            cout << max(-1,d0-1) << "\n";
        }
    }
    return 0;
}

Compilation message (stderr)

007.cpp: In function 'int main()':
007.cpp:69:62: warning: array subscript is above array bounds [-Warray-bounds]
             if(d[0][i] + d[3][i] == d[0][n0] && d[0][i] + d[4][i] == d[0][n1]){
                                                           ~~~^
007.cpp:72:62: warning: array subscript is above array bounds [-Warray-bounds]
             if(d[1][i] + d[3][i] == d[1][n0] && d[1][i] + d[4][i] == d[1][n1]){
                                                           ~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...