답안 #153478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
153478 2019-09-14T09:14:52 Z Mercenary 007 (CEOI14_007) C++14
100 / 100
354 ms 24816 KB
#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 <= m ; ++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

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);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10104 KB Output is correct
2 Correct 13 ms 10104 KB Output is correct
3 Correct 11 ms 10104 KB Output is correct
4 Correct 11 ms 10104 KB Output is correct
5 Correct 9 ms 10104 KB Output is correct
6 Correct 11 ms 10104 KB Output is correct
7 Correct 10 ms 10104 KB Output is correct
8 Correct 11 ms 10104 KB Output is correct
9 Correct 10 ms 10104 KB Output is correct
10 Correct 10 ms 10104 KB Output is correct
11 Correct 10 ms 10104 KB Output is correct
12 Correct 10 ms 10104 KB Output is correct
13 Correct 11 ms 10148 KB Output is correct
14 Correct 10 ms 10104 KB Output is correct
15 Correct 11 ms 10104 KB Output is correct
16 Correct 11 ms 10104 KB Output is correct
17 Correct 10 ms 10104 KB Output is correct
18 Correct 11 ms 10104 KB Output is correct
19 Correct 11 ms 10104 KB Output is correct
20 Correct 11 ms 10104 KB Output is correct
21 Correct 11 ms 10104 KB Output is correct
22 Correct 11 ms 10204 KB Output is correct
23 Correct 11 ms 10104 KB Output is correct
24 Correct 11 ms 10104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 11444 KB Output is correct
2 Correct 52 ms 12024 KB Output is correct
3 Correct 43 ms 11468 KB Output is correct
4 Correct 56 ms 12024 KB Output is correct
5 Correct 39 ms 11384 KB Output is correct
6 Correct 38 ms 11512 KB Output is correct
7 Correct 44 ms 11640 KB Output is correct
8 Correct 44 ms 11796 KB Output is correct
9 Correct 60 ms 12204 KB Output is correct
10 Correct 179 ms 20600 KB Output is correct
11 Correct 90 ms 12920 KB Output is correct
12 Correct 109 ms 13656 KB Output is correct
13 Correct 90 ms 13180 KB Output is correct
14 Correct 79 ms 12668 KB Output is correct
15 Correct 128 ms 13724 KB Output is correct
16 Correct 136 ms 13944 KB Output is correct
17 Correct 131 ms 13600 KB Output is correct
18 Correct 113 ms 13604 KB Output is correct
19 Correct 153 ms 15992 KB Output is correct
20 Correct 253 ms 21496 KB Output is correct
21 Correct 193 ms 15224 KB Output is correct
22 Correct 139 ms 14464 KB Output is correct
23 Correct 197 ms 15072 KB Output is correct
24 Correct 182 ms 15048 KB Output is correct
25 Correct 178 ms 14800 KB Output is correct
26 Correct 176 ms 14748 KB Output is correct
27 Correct 207 ms 15096 KB Output is correct
28 Correct 222 ms 15200 KB Output is correct
29 Correct 261 ms 17372 KB Output is correct
30 Correct 322 ms 22156 KB Output is correct
31 Correct 299 ms 15992 KB Output is correct
32 Correct 278 ms 15196 KB Output is correct
33 Correct 205 ms 15352 KB Output is correct
34 Correct 246 ms 15684 KB Output is correct
35 Correct 268 ms 15352 KB Output is correct
36 Correct 212 ms 15552 KB Output is correct
37 Correct 245 ms 16356 KB Output is correct
38 Correct 230 ms 16204 KB Output is correct
39 Correct 245 ms 16120 KB Output is correct
40 Correct 291 ms 20088 KB Output is correct
41 Correct 354 ms 24816 KB Output is correct