結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー monnumonnu
提出日時 2023-08-04 23:29:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,612 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 170,544 KB
実行使用メモリ 6,296 KB
最終ジャッジ日時 2023-08-17 18:46:40
合計ジャッジ時間 3,851 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 40 ms
4,380 KB
testcase_05 AC 81 ms
4,624 KB
testcase_06 AC 88 ms
4,700 KB
testcase_07 AC 51 ms
4,376 KB
testcase_08 AC 61 ms
4,436 KB
testcase_09 AC 95 ms
4,952 KB
testcase_10 AC 88 ms
4,568 KB
testcase_11 AC 68 ms
4,400 KB
testcase_12 AC 93 ms
4,588 KB
testcase_13 AC 64 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 5 ms
6,156 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,612 KB
testcase_22 AC 4 ms
6,020 KB
testcase_23 AC 4 ms
5,936 KB
testcase_24 AC 4 ms
5,396 KB
testcase_25 AC 4 ms
5,152 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 6 ms
6,168 KB
testcase_28 AC 5 ms
5,460 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 6 ms
6,296 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class UnionFind
{
    int n;
    vector<int> parent;
    vector<int> edgeCnt;

public:
    UnionFind(int n_) : n(n_)
    {
        parent.resize(n);
        edgeCnt.resize(n, 0);
        iota(parent.begin(), parent.end(), 0);
    }
    int find(int x)
    {
        int y = parent[x];
        if (y != parent[y])
        {
            y = find(y);
        }
        return parent[x] = y;
    }
    void unite(int a, int b)
    {
        int x = find(a);
        int y = find(b);
        if (x != y)
        {
            parent[y] = x;
            edgeCnt[x] = edgeCnt[y] + 1;
        }
        else
        {
            edgeCnt[x]++;
        }
    }
    bool same(int a, int b)
    {
        return find(a) == find(b);
    }
    int EdgeCnt(int i)
    {
        return edgeCnt[i];
    }
};

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> u(M), v(M);
    vector<int> inDegree(N, 0);
    vector<int> outDegree(N, 0);
    UnionFind uf(N);
    for (int i = 0; i < M; i++)
    {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
        inDegree[v[i]]++;
        outDegree[u[i]]++;
        uf.unite(u[i], v[i]);
    }

    vector<int> cnt(N, 0);
    for (int i = 0; i < N; i++)
    {
        cnt[uf.find(i)] += abs(inDegree[i] - outDegree[i]);
    }
    int ans = -1;
    for (int i = 0; i < N; i++)
    {
        if (uf.find(i) == i)
        {
            if (uf.EdgeCnt(i) > 0)
            {
                ans += max(0, cnt[i] / 2 - 1);
                ans++;
            }
        }
    }
    cout << ans << endl;
}
0