結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー t98slidert98slider
提出日時 2023-08-04 22:57:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,006 bytes
コンパイル時間 1,685 ms
コンパイル使用メモリ 173,332 KB
実行使用メモリ 18,792 KB
最終ジャッジ日時 2024-04-22 21:18:32
合計ジャッジ時間 3,344 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 18 ms
6,940 KB
testcase_05 AC 36 ms
6,940 KB
testcase_06 AC 42 ms
7,424 KB
testcase_07 AC 22 ms
6,940 KB
testcase_08 AC 28 ms
6,944 KB
testcase_09 AC 43 ms
7,040 KB
testcase_10 AC 36 ms
6,944 KB
testcase_11 AC 31 ms
6,944 KB
testcase_12 AC 40 ms
6,944 KB
testcase_13 AC 32 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
6,940 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

struct SCC {
    int n, group_num;
    std::vector<std::vector<int>> &g;
    std::vector<int> ids;
    SCC(std::vector<std::vector<int>> &_g) : n(_g.size()), g(_g), ids(n), group_num(0) {
        int now_ord = 0;
        std::vector<int> visited, low(n), ord(n, -1);
        visited.reserve(n);
        auto dfs = [&](auto self, int v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.emplace_back(v);
            for(auto to : g[v]) {
                if (ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if (low[v] == ord[v]) {
                while (true) {
                    int u = visited.back();
                    visited.pop_back();
                    ord[u] = n;
                    ids[u] = group_num;
                    if (u == v) break;
                }
                group_num++;
            }
        };
        for (int i = 0; i < n; i++) {
            if (ord[i] == -1) dfs(dfs, i);
        }
        for (auto& x : ids) {
            x = group_num - 1 - x;
        }
    }
    const int operator[](int p) const {
        assert(0 <= p && p < n);
        return ids[p];
    }
    int operator[](int p) { 
        assert(0 <= p && p < n);
        return ids[p];
    }
    std::vector<std::vector<int>> groups(){
        std::vector<int> counts(group_num);
        for (auto x : ids) counts[x]++;
        std::vector<std::vector<int>> groups(group_num);
        for (int i = 0; i < group_num; i++) {
            groups[i].reserve(counts[i]);
        }
        for (int i = 0; i < n; i++) {
            groups[ids[i]].emplace_back(i);
        }
        return groups;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, u, v;
    cin >> n >> m;
    vector<int> in(n), out(n);
    vector<vector<int>> g(n);
    for(int i = 0; i < m; i++){
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        in[u]++;
        out[v]++;
    }
    ll s = 0, s1 = 0;
    SCC scc(g);
    vector<bool> dp(n);
    int ans = 0;
    for(auto &&vec : scc.groups()){
        bool f = false;
        for(auto &&v : vec){
            f |= dp[v];
            for(auto &&u : g[v]){
                dp[u] = true;
            }
        }
        ans += f ^ true;
    }
    for(int i = 0; i < n; i++){
        if(in[i] >= out[i]) s += in[i] - out[i];
        else s1 += out[i] - in[i];
    }
    cout << max((ll)(ans), max(0ll, max(s1, s) - 1)) << '\n';
}
0