結果

問題 No.1813 Magical Stones
ユーザー se1ka2se1ka2
提出日時 2022-01-14 23:04:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,364 bytes
コンパイル時間 1,353 ms
コンパイル使用メモリ 93,940 KB
実行使用メモリ 20,880 KB
最終ジャッジ日時 2023-08-12 21:50:37
合計ジャッジ時間 7,239 ms
ジャッジサーバーID
(参考情報)
judge2 / judge8
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 9 ms
11,252 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 41 ms
8,724 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 10 ms
10,952 KB
testcase_15 AC 259 ms
20,668 KB
testcase_16 WA -
testcase_17 AC 221 ms
18,424 KB
testcase_18 AC 246 ms
20,544 KB
testcase_19 AC 255 ms
20,692 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 3 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 165 ms
16,640 KB
testcase_28 AC 192 ms
12,980 KB
testcase_29 AC 196 ms
16,840 KB
testcase_30 AC 183 ms
15,284 KB
testcase_31 WA -
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 WA -
testcase_35 AC 20 ms
4,460 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 6 ms
4,376 KB
testcase_41 AC 6 ms
4,376 KB
testcase_42 AC 22 ms
4,416 KB
testcase_43 WA -
権限があれば一括ダウンロードができます

ソースコード

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 ans = 0;
    for(int u = 0; u < k; u++){
        if(h.g[u].size() == 0) ans++;
    }
    cout << ans << endl;
}
0