結果

問題 No.1813 Magical Stones
ユーザー 👑 NachiaNachia
提出日時 2022-01-14 22:24:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,907 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 85,224 KB
実行使用メモリ 16,012 KB
最終ジャッジ日時 2024-04-30 15:43:20
合計ジャッジ時間 3,322 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 7 ms
8,960 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 13 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 7 ms
8,832 KB
testcase_15 AC 79 ms
14,848 KB
testcase_16 AC 86 ms
14,980 KB
testcase_17 AC 82 ms
16,012 KB
testcase_18 AC 84 ms
14,976 KB
testcase_19 AC 82 ms
14,852 KB
testcase_20 AC 84 ms
14,848 KB
testcase_21 AC 84 ms
14,972 KB
testcase_22 AC 83 ms
14,856 KB
testcase_23 AC 83 ms
14,852 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 9 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 55 ms
11,640 KB
testcase_28 AC 70 ms
11,440 KB
testcase_29 AC 66 ms
12,620 KB
testcase_30 AC 64 ms
11,796 KB
testcase_31 AC 75 ms
14,240 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 4 ms
6,940 KB
testcase_35 AC 9 ms
6,944 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 19 ms
6,940 KB
testcase_38 AC 8 ms
7,040 KB
testcase_39 AC 48 ms
11,196 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 4 ms
6,944 KB
testcase_42 AC 10 ms
6,940 KB
testcase_43 AC 7 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned int;
#define rep(i,n) for(int i=0; i<(n); i++)


pair<int, vector<int>> SCC(const vector<vector<int>>& E) {
    int n = E.size();
    vector<int> F(n, 0), St, I, perm, g;
    vector<vector<int>> R(n), G, res;
    rep(i, n) if (F[i] == 0) {
        St.push_back(i);
        while (St.size()) {
            int p = St.back();
            St.pop_back();
            if (p < 0) { I.push_back(-1 - p); continue; }
            if (F[p]) continue;
            F[p] = -1;
            St.push_back(-1 - p);
            for (int e : E[p]) St.push_back(e);
        }
    }
    rep(i, n) for (int e : E[i]) R[e].push_back(i);
    reverse(I.begin(), I.end());
    int res_n = 0;
    for (int s : I) if (F[s] == -1) {
        g = { s };
        F[s] = res_n;
        rep(i, g.size()) {
            int p = g[i];
            for (int e : R[p]) if (F[e] == -1) {
                F[e] = res_n;
                g.push_back(e);
            }
        }
        res_n++;
    }
    return make_pair(res_n, move(F));
}


int main() {
    int N; cin >> N;
    int M; cin >> M;
    vector<vector<int>> E(N);
    rep(i,M){
        int u,v; cin >> u >> v; u--; v--;
        E[u].push_back(v);
    }

    auto [nsc,SCid] = SCC(E);
    vector<int> typesc(nsc, 0);
    rep(u,N) for(int v : E[u]) if(SCid[u] != SCid[v]){
        typesc[SCid[u]] |= 1;
        typesc[SCid[v]] |= 2;
    }

    if(nsc == 1){ cout << "0\n"; return 0; }

    int typecnt[4] = {};
    for(int t : typesc) typecnt[t]++;

    int ans = max(typecnt[0] + typecnt[1], typecnt[0] + typecnt[2]);
    cout << ans << endl;
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0