結果

問題 No.1813 Magical Stones
ユーザー 👑 NachiaNachia
提出日時 2022-01-14 21:29:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,296 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 20,128 KB
最終ジャッジ日時 2023-08-12 17:43:39
合計ジャッジ時間 7,965 ms
ジャッジサーバーID
(参考情報)
judge9 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 22 ms
20,128 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 RE -
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 21 ms
19,984 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 98 ms
15,844 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 RE -
testcase_35 AC 10 ms
4,376 KB
testcase_36 AC 3 ms
4,384 KB
testcase_37 WA -
testcase_38 AC 20 ms
13,760 KB
testcase_39 RE -
testcase_40 RE -
testcase_41 AC 4 ms
4,376 KB
testcase_42 AC 11 ms
4,376 KB
testcase_43 RE -
権限があれば一括ダウンロードができます

ソースコード

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[u] |= 1;
        typesc[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