結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー Fu_LFu_L
提出日時 2024-01-23 23:55:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,424 bytes
コンパイル時間 3,651 ms
コンパイル使用メモリ 211,316 KB
実行使用メモリ 15,048 KB
最終ジャッジ日時 2024-01-23 23:55:37
合計ジャッジ時間 5,949 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 17 ms
6,676 KB
testcase_05 AC 31 ms
6,676 KB
testcase_06 AC 34 ms
6,676 KB
testcase_07 AC 21 ms
6,676 KB
testcase_08 AC 24 ms
6,676 KB
testcase_09 AC 36 ms
6,676 KB
testcase_10 AC 33 ms
6,676 KB
testcase_11 AC 27 ms
6,676 KB
testcase_12 AC 35 ms
6,676 KB
testcase_13 AC 25 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 WA -
testcase_18 AC 24 ms
6,676 KB
testcase_19 AC 19 ms
15,048 KB
testcase_20 AC 7 ms
6,676 KB
testcase_21 AC 11 ms
9,216 KB
testcase_22 AC 17 ms
13,644 KB
testcase_23 AC 17 ms
13,364 KB
testcase_24 AC 14 ms
11,520 KB
testcase_25 AC 14 ms
11,008 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 17 ms
11,904 KB
testcase_29 WA -
testcase_30 AC 9 ms
7,808 KB
testcase_31 AC 19 ms
14,392 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 5 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
struct UnionFind {
    UnionFind(int N)
        : n(N), data(N, -1) {}
    int merge(int a, int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        int x = leader(a), y = leader(b);
        if(x == y) return x;
        if(-data[x] < -data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return x;
    }
    bool same(int a, int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        return leader(a) == leader(b);
    }
    int leader(int a) {
        assert(0 <= a and a < n);
        if(data[a] < 0) return a;
        return data[a] = leader(data[a]);
    }
    int size(int a) {
        assert(0 <= a and a < n);
        return -data[leader(a)];
    }
    vector<vector<int>> groups() {
        vector<int> leader_buf(n), group_size(n);
        for(int i = 0; i < n; ++i) {
            leader_buf[i] = leader(i);
            ++group_size[leader_buf[i]];
        }
        vector<vector<int>> result(n);
        for(int i = 0; i < n; ++i) {
            result[i].reserve(group_size[i]);
        }
        for(int i = 0; i < n; ++i) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(remove_if(result.begin(), result.end(), [&](const vector<int>& v) { return v.empty(); }), result.end());
        return result;
    }

   private:
    int n;
    vector<int> data;
};
int main(void) {
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    vector<int> deg(n);
    rep(i, 0, m) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        uf.merge(u, v);
        deg[u]++;
        deg[v]--;
    }
    vector<vector<int>> g = uf.groups();
    int ans = 0;
    bool flag = false;
    for(const vector<int>& v : g) {
        if((int)v.size() > 1) {
            if(flag) {
                ans++;
            } else {
                flag = true;
            }
        }
        int cnt = 0;
        for(const int x : v) {
            cnt += abs(deg[x]);
        }
        if(cnt >= 4) ans += (cnt - 2) / 2;
    }
    cout << ans << '\n';
}
0