답안 #541827

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541827 2022-03-24T13:47:48 Z Lobo 007 (CEOI14_007) C++17
100 / 100
264 ms 34836 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n, m, a,b,s1,s2;
int d[maxn][5];
vector<int> g[maxn];

void bfs(int beg, int id) {
    for(int i = 1; i <= n; i++) {
        d[i][id] = inf;
    }
    d[beg][id] = 0;
    queue<int> q; q.push(beg);

    while(q.size()) {
        int u = q.front();
        q.pop();

        for(auto v : g[u]) {
            if(d[v][id] == inf) {
                d[v][id] = d[u][id]+1;
                q.push(v);
            }
        }
    }
}

void solve() {
    cin >> n >> m;

    cin >> a >> b >> s1 >> s2;

    for(int i = 1; i <= m; i++) {
        int u,v; cin >> u >> v;

        g[u].pb(v);
        g[v].pb(u);
    }
    //bfs partindo de a e de b

    bfs(a,1);
    bfs(b,2);
    bfs(s1,3);
    bfs(s2,4);

    if(d[a][3] == d[a][4] && d[b][3] == d[b][4]) {
        //ve qual vai poder escolher para onde vai primeiro
        
        int mna = inf;
        int mnb = inf;
        for(int i = 1; i <= n; i++) {
            //a->i, i->b

            if(d[i][1] + d[i][3] == d[a][3] && d[i][1] + d[i][4] == d[a][4]) {
                mna = min(mna,d[i][3]);
            }

            if(d[i][2] + d[i][3] == d[b][3] && d[i][2] + d[i][4] == d[b][4]) {
                mnb = min(mnb,d[i][3]);
            }
        }

        cout << max(d[b][3]-d[a][3]-(mnb<mna),(int) -1) << endl;

    }
    else {
        //o b vai pro menor dele e o a acompanha

        cout << max(min(d[b][3]-d[a][3],d[b][4]-d[a][4]),(int) -1) << endl;
    }



}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 4 ms 5032 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 5 ms 5036 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5032 KB Output is correct
12 Correct 3 ms 5032 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 4996 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 3 ms 5040 KB Output is correct
22 Correct 3 ms 5204 KB Output is correct
23 Correct 4 ms 5036 KB Output is correct
24 Correct 4 ms 5036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 8372 KB Output is correct
2 Correct 36 ms 9896 KB Output is correct
3 Correct 27 ms 8660 KB Output is correct
4 Correct 35 ms 9984 KB Output is correct
5 Correct 25 ms 8372 KB Output is correct
6 Correct 22 ms 8572 KB Output is correct
7 Correct 33 ms 9016 KB Output is correct
8 Correct 34 ms 9100 KB Output is correct
9 Correct 46 ms 10708 KB Output is correct
10 Correct 142 ms 23792 KB Output is correct
11 Correct 71 ms 12588 KB Output is correct
12 Correct 101 ms 14788 KB Output is correct
13 Correct 68 ms 13168 KB Output is correct
14 Correct 49 ms 11828 KB Output is correct
15 Correct 80 ms 14632 KB Output is correct
16 Correct 114 ms 15244 KB Output is correct
17 Correct 79 ms 14000 KB Output is correct
18 Correct 93 ms 14100 KB Output is correct
19 Correct 127 ms 18216 KB Output is correct
20 Correct 198 ms 27424 KB Output is correct
21 Correct 124 ms 18380 KB Output is correct
22 Correct 126 ms 16608 KB Output is correct
23 Correct 130 ms 18220 KB Output is correct
24 Correct 117 ms 18004 KB Output is correct
25 Correct 121 ms 17496 KB Output is correct
26 Correct 112 ms 16704 KB Output is correct
27 Correct 132 ms 18252 KB Output is correct
28 Correct 136 ms 18408 KB Output is correct
29 Correct 169 ms 21816 KB Output is correct
30 Correct 222 ms 28744 KB Output is correct
31 Correct 152 ms 20328 KB Output is correct
32 Correct 150 ms 18376 KB Output is correct
33 Correct 145 ms 18680 KB Output is correct
34 Correct 146 ms 19508 KB Output is correct
35 Correct 123 ms 18720 KB Output is correct
36 Correct 143 ms 19288 KB Output is correct
37 Correct 167 ms 21192 KB Output is correct
38 Correct 157 ms 20840 KB Output is correct
39 Correct 177 ms 21072 KB Output is correct
40 Correct 222 ms 27148 KB Output is correct
41 Correct 264 ms 34836 KB Output is correct