結果

問題 No.2565 はじめてのおつかい
ユーザー *yudai25n*yudai25n
提出日時 2023-12-02 15:44:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,337 bytes
コンパイル時間 4,478 ms
コンパイル使用メモリ 266,648 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2023-12-02 15:44:52
合計ジャッジ時間 9,335 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 86 ms
9,984 KB
testcase_04 AC 69 ms
9,984 KB
testcase_05 WA -
testcase_06 AC 51 ms
6,676 KB
testcase_07 AC 27 ms
6,676 KB
testcase_08 AC 25 ms
6,676 KB
testcase_09 AC 46 ms
6,676 KB
testcase_10 AC 30 ms
6,676 KB
testcase_11 AC 66 ms
7,040 KB
testcase_12 AC 19 ms
6,676 KB
testcase_13 AC 30 ms
6,676 KB
testcase_14 AC 48 ms
6,676 KB
testcase_15 AC 51 ms
6,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 53 ms
6,676 KB
testcase_22 WA -
testcase_23 AC 36 ms
6,676 KB
testcase_24 WA -
testcase_25 AC 55 ms
6,676 KB
testcase_26 AC 37 ms
6,676 KB
testcase_27 AC 58 ms
6,676 KB
testcase_28 AC 67 ms
6,676 KB
testcase_29 AC 69 ms
7,424 KB
testcase_30 WA -
testcase_31 AC 57 ms
6,676 KB
testcase_32 AC 34 ms
6,676 KB
testcase_33 AC 71 ms
6,784 KB
testcase_34 AC 57 ms
6,676 KB
testcase_35 WA -
testcase_36 AC 61 ms
6,784 KB
testcase_37 AC 34 ms
6,676 KB
testcase_38 AC 77 ms
6,676 KB
testcase_39 AC 48 ms
6,676 KB
testcase_40 AC 73 ms
7,424 KB
testcase_41 AC 67 ms
7,680 KB
testcase_42 AC 37 ms
6,676 KB
testcase_43 AC 52 ms
6,676 KB
testcase_44 AC 27 ms
6,676 KB
testcase_45 AC 15 ms
6,676 KB
testcase_46 AC 84 ms
6,676 KB
testcase_47 AC 41 ms
6,676 KB
testcase_48 AC 32 ms
6,676 KB
testcase_49 AC 19 ms
6,676 KB
testcase_50 AC 64 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 44 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i,n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i,l,r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using LP = pair<long long,long long>;
using SP = pair<string,string>;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using mint1 = modint1000000007;
using mint9 = modint998244353;
const int INF = 1e9;
const long long LINF = 4e18;
const int di[] = {0,1,0,-1,-1,1,-1,1};
const int dj[] = {1,0,-1,0,1,1,-1,-1};
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
void YesNo (bool ok , const string &s , const string &t) {if(ok) cout << s << endl; else cout << t << endl;} 


//-----def end-----//

 class UGraph {
     private:
         vector<int> dist_bfs (int s) {
             vector<int> res(n , INF);
             queue<int> qe;
             qe.push(s);
             res[s] = 0;
             while(!qe.empty()) {
                 int pos = qe.front(); qe.pop();
                 for(int u : g[pos]) {
                     if(res[u] != INF) continue;
                     res[u] = res[pos]+1;
                     qe.push(u);
                 }
             }
             return res;
         }
         bool bipartite_bfs () {
             vector<int> color(n , -1);
             bool isBipartite = true;
             for(int s = 0; s < n; s++) {
                 if(color[s] != -1) continue;
                 queue<int> qe;
                 qe.push(s);
                 color[s] = 0;
                 while(!qe.empty()) {
                     int pos = qe.front(); qe.pop();
                     for(int u : g[pos]) {
                         if(color[u] != -1) {
                             if(color[u] == color[pos]) isBipartite = false;
                             continue;
                         }
                         color[u] = 1-color[pos];
                         qe.push(u);
                     }
                 }
             }
             return isBipartite;
         }
     public:
         int n , m;
         vector<vector<int>> g;
         UGraph (int nn) {
             g = vector<vector<int>>(nn);
             n = nn;
         }
         void merge (int u , int v) {
             g[u].push_back(v);
            //g[v].push_back(u);
         }
         vector<int> dist(int s) {
             return dist_bfs(s);
         }
         bool bipartite () {
             return bipartite_bfs();
         }
 };
 


int main () {
    int n , m;
    cin >> n >> m;
    UGraph g(n);
    rep(i , m) {
        int u , v;
        cin >> u >> v;
        u--; v--;
        g.merge(u , v);
    }
    auto d0 = g.dist(0) , dn2 = g.dist(n-2) , dn1 = g.dist(n-1);
    int res = INF;
    chmin(res , d0[n-2]+dn2[n-1]+dn1[0]);
    chmin(res , d0[n-1]+dn1[n-2]+dn2[0]);
    if(res == INF) res = -1;
    cout << res << endl;
	return 0;
}
0