結果

問題 No.1813 Magical Stones
ユーザー se1ka2se1ka2
提出日時 2022-01-15 00:12:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 2,502 bytes
コンパイル時間 1,185 ms
コンパイル使用メモリ 94,724 KB
実行使用メモリ 21,212 KB
最終ジャッジ日時 2024-04-30 18:53:24
合計ジャッジ時間 6,448 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 10 ms
11,572 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 43 ms
9,216 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 11 ms
11,708 KB
testcase_15 AC 258 ms
21,212 KB
testcase_16 AC 260 ms
21,076 KB
testcase_17 AC 240 ms
18,432 KB
testcase_18 AC 260 ms
21,084 KB
testcase_19 AC 254 ms
21,092 KB
testcase_20 AC 264 ms
21,108 KB
testcase_21 AC 260 ms
21,064 KB
testcase_22 AC 260 ms
21,128 KB
testcase_23 AC 261 ms
21,068 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 25 ms
7,296 KB
testcase_26 AC 9 ms
6,944 KB
testcase_27 AC 165 ms
17,140 KB
testcase_28 AC 194 ms
13,568 KB
testcase_29 AC 196 ms
17,244 KB
testcase_30 AC 185 ms
15,980 KB
testcase_31 AC 241 ms
20,816 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 8 ms
6,940 KB
testcase_35 AC 21 ms
6,944 KB
testcase_36 AC 4 ms
6,944 KB
testcase_37 AC 50 ms
6,940 KB
testcase_38 AC 19 ms
9,856 KB
testcase_39 AC 166 ms
18,172 KB
testcase_40 AC 6 ms
6,944 KB
testcase_41 AC 7 ms
6,940 KB
testcase_42 AC 22 ms
6,944 KB
testcase_43 AC 15 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;

struct Graph
{
    int n;
    std::vector<std::vector<int>> g;
    
    Graph(){}
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

struct SCC      //StronglyConnectedComponents
{
    int n;
    int k;
    std::vector<std::vector<int>> g, rg;
    std::vector<bool> used;
    std::vector<int> cmp;
    std::vector<int> vs;
    
    SCC(){}
    
    SCC(int n) : n(n){
        g.resize(n);
        rg.resize(n);
        used.resize(n);
        cmp.resize(n);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
        rg[to].push_back(from);
    }
    
    void dfs(int u){
        used[u] = true;
        for(int v : g[u]){
            if(!used[v]) dfs(v);
        }
        vs.push_back(u);
    }
    
    void rdfs(int u, int k){
        used[u] = true;
        cmp[u] = k;
        for(int v : rg[u]){
            if(!used[v]) rdfs(v, k);
        }
    }
    
    int decomposition(){
        for(int i = 0; i < n; i++) used[i] = false;
        for(int i = 0; i < n; i++){
            if(!used[i]) dfs(i);
        }
        for(int i = 0; i < n; i++) used[i] = false;
        k = 0;
        for(int i = n - 1; i >= 0; i--){
            if(!used[vs[i]]){
                rdfs(vs[i], k);
                k++;
            }
        }
        return k;
    }
    
    int decomposition(Graph &dag){
        k = decomposition();
        dag.n = k;
        dag.g.resize(k);
        std::map<std::pair<int, int>, bool> mp;
        for(int u = 0; u < n; u++){
            for(int v : g[u]){
                if(!mp[std::pair<int, int> (cmp[u], cmp[v])] && cmp[u] != cmp[v]){
                    mp[std::pair<int, int> (cmp[u], cmp[v])] = true;
                    dag.add_edge(cmp[v], cmp[u]);
                }
            }
        }
        return k;
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    SCC g(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g.add_edge(a, b);
    }
    Graph h;
    int k = g.decomposition(h);
    if(k == 1){
        cout << 0 << endl;
        return 0;
    }
    int d[100005]{0};
    for(int u = 0; u < k; u++){
        for(int v : h.g[u]) d[v]++;
    }
    int c0 = 0, c1 = 0;
    for(int u = 0; u < k; u++){
        if(h.g[u].size() == 0) c0++;
        if(d[u] == 0) c1++;
    }
    cout << max(c0, c1) << endl;
}
0