結果

問題 No.1813 Magical Stones
ユーザー 👑 NachiaNachia
提出日時 2022-01-14 21:31:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 2,308 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 88,312 KB
実行使用メモリ 20,296 KB
最終ジャッジ日時 2024-04-30 12:52:39
合計ジャッジ時間 3,783 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 20 ms
20,296 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 19 ms
8,468 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 20 ms
20,136 KB
testcase_15 AC 109 ms
19,304 KB
testcase_16 AC 101 ms
19,436 KB
testcase_17 AC 94 ms
16,136 KB
testcase_18 AC 104 ms
19,432 KB
testcase_19 AC 107 ms
19,392 KB
testcase_20 AC 107 ms
19,312 KB
testcase_21 AC 108 ms
19,296 KB
testcase_22 AC 112 ms
19,320 KB
testcase_23 AC 103 ms
19,432 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 12 ms
7,124 KB
testcase_26 AC 5 ms
6,944 KB
testcase_27 AC 65 ms
15,816 KB
testcase_28 AC 77 ms
11,948 KB
testcase_29 AC 78 ms
15,028 KB
testcase_30 AC 73 ms
14,116 KB
testcase_31 AC 91 ms
18,832 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 5 ms
6,940 KB
testcase_35 AC 10 ms
6,944 KB
testcase_36 AC 3 ms
6,944 KB
testcase_37 AC 23 ms
6,944 KB
testcase_38 AC 18 ms
13,780 KB
testcase_39 AC 70 ms
16,676 KB
testcase_40 AC 4 ms
6,940 KB
testcase_41 AC 4 ms
6,944 KB
testcase_42 AC 10 ms
6,944 KB
testcase_43 AC 7 ms
6,940 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++)


vector<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());
    for (int s : I) if (F[s] == -1) {
        g = { s };
        F[s] = G.size();
        rep(i, g.size()) {
            int p = g[i];
            for (int e : R[p]) if (F[e] == -1) {
                F[e] = G.size();
                g.push_back(e);
            }
        }
        G.emplace_back(move(g));
    }
    I.assign(G.size(), 0);
    rep(i, n) for (int e : E[i]) if (F[i] != F[e]) I[F[e]]++;
    rep(i, G.size()) if (I[i] == 0) perm.push_back(i);
    rep(i, perm.size()) for (int p : G[i]) {
        for (int e : E[p]) if (--I[F[e]] == 0) perm.push_back(F[e]);
    }
    for (auto p : perm) res.emplace_back(move(G[p]));
    return res;
}


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 SCs = SCC(E);

    vector<int> SCid(N, -1);
    rep(i,SCs.size()) for(int p : SCs[i]) SCid[p] = i;

    int nsc = SCs.size();
    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