結果

問題 No.1813 Magical Stones
ユーザー se1ka2se1ka2
提出日時 2022-01-15 00:12:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,502 bytes
コンパイル時間 1,286 ms
コンパイル使用メモリ 93,828 KB
実行使用メモリ 20,920 KB
最終ジャッジ日時 2023-08-13 00:00:36
合計ジャッジ時間 7,361 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 10 ms
11,284 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,624 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 45 ms
9,004 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 10 ms
11,416 KB
testcase_15 AC 271 ms
20,864 KB
testcase_16 AC 266 ms
20,920 KB
testcase_17 AC 237 ms
18,360 KB
testcase_18 AC 268 ms
20,820 KB
testcase_19 AC 270 ms
20,868 KB
testcase_20 AC 266 ms
20,844 KB
testcase_21 AC 269 ms
20,720 KB
testcase_22 AC 266 ms
20,820 KB
testcase_23 AC 264 ms
20,796 KB
testcase_24 AC 3 ms
4,384 KB
testcase_25 AC 26 ms
6,964 KB
testcase_26 AC 9 ms
4,560 KB
testcase_27 AC 173 ms
17,028 KB
testcase_28 AC 200 ms
13,716 KB
testcase_29 AC 201 ms
17,196 KB
testcase_30 AC 189 ms
16,004 KB
testcase_31 AC 251 ms
20,620 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 7 ms
4,384 KB
testcase_35 AC 20 ms
4,380 KB
testcase_36 AC 4 ms
4,384 KB
testcase_37 AC 47 ms
5,812 KB
testcase_38 AC 18 ms
9,768 KB
testcase_39 AC 166 ms
17,952 KB
testcase_40 AC 7 ms
4,420 KB
testcase_41 AC 7 ms
4,384 KB
testcase_42 AC 21 ms
4,388 KB
testcase_43 AC 13 ms
4,620 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