結果

問題 No.1813 Magical Stones
ユーザー 👑 NachiaNachia
提出日時 2022-01-14 21:31:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,308 bytes
コンパイル時間 1,066 ms
コンパイル使用メモリ 88,064 KB
実行使用メモリ 20,168 KB
最終ジャッジ日時 2023-08-12 17:45:07
合計ジャッジ時間 4,732 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 22 ms
19,796 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 21 ms
8,524 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 23 ms
20,168 KB
testcase_15 AC 125 ms
19,196 KB
testcase_16 AC 116 ms
19,196 KB
testcase_17 AC 106 ms
15,800 KB
testcase_18 AC 115 ms
19,200 KB
testcase_19 AC 110 ms
19,256 KB
testcase_20 AC 113 ms
18,968 KB
testcase_21 AC 113 ms
19,020 KB
testcase_22 AC 117 ms
19,224 KB
testcase_23 AC 111 ms
19,160 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 13 ms
6,756 KB
testcase_26 AC 6 ms
4,536 KB
testcase_27 AC 75 ms
15,684 KB
testcase_28 AC 82 ms
11,576 KB
testcase_29 AC 84 ms
14,876 KB
testcase_30 AC 79 ms
13,888 KB
testcase_31 AC 108 ms
18,640 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 4 ms
4,384 KB
testcase_35 AC 10 ms
4,380 KB
testcase_36 AC 4 ms
4,384 KB
testcase_37 AC 22 ms
5,068 KB
testcase_38 AC 21 ms
13,740 KB
testcase_39 AC 74 ms
16,528 KB
testcase_40 AC 4 ms
4,384 KB
testcase_41 AC 4 ms
4,384 KB
testcase_42 AC 10 ms
4,396 KB
testcase_43 AC 8 ms
4,380 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