結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー erbowlerbowl
提出日時 2023-08-04 23:32:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,013 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 212,448 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-04-22 21:57:09
合計ジャッジ時間 4,275 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 41 ms
6,944 KB
testcase_05 AC 82 ms
6,940 KB
testcase_06 AC 87 ms
6,940 KB
testcase_07 AC 51 ms
6,940 KB
testcase_08 AC 62 ms
6,944 KB
testcase_09 AC 96 ms
6,944 KB
testcase_10 AC 85 ms
6,944 KB
testcase_11 AC 68 ms
6,944 KB
testcase_12 AC 88 ms
6,944 KB
testcase_13 AC 63 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 WA -
testcase_16 AC 1 ms
6,940 KB
testcase_17 WA -
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 #

typedef long long ll;
typedef long double ld;

#include <bits/stdc++.h>
using namespace std;
#define int long long

// Union-Find
struct UnionFind {
    // core member
    vector<int> par;

    // constructor
    UnionFind() { }
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    // core methods
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
    
    // debug
    friend ostream& operator << (ostream &s, UnionFind uf) {
        map<int, vector<int>> groups;
        for (int i = 0; i < uf.par.size(); ++i) {
            int r = uf.root(i);
            groups[r].push_back(i);
        }
        for (const auto &it : groups) {
            s << "group: ";
            for (auto v : it.second) s << v << " ";
            s << endl;
        }
        return s;
    }
};
signed main(){
    ll n,m;
    std::cin >> n>>m;
    vector<ll> nyu(n),shu(n);
    UnionFind uf(n);
    vector<ll> jiko(n);
    for (int i = 0; i < m; i++) {
        ll u,v;
        
        std::cin >> u>>v;
        u--;v--;
        uf.merge(u,v);
        shu[u]++;
        nyu[v]++;
        if(u==v){
            jiko[u]++;
        }
    }
    set<ll> kan;
    map<ll,ll> ma;
    ll cnt = 0;
    set<ll> uni;
    for (int i = 0; i < n; i++) {
        uni.insert(uf.root(i));
        if(shu[i]-nyu[i]>0){
            ma[uf.root(i)] += shu[i]-nyu[i];
        }
        if(shu[i]==0&&nyu[i]==0)cnt++;
    }
    ll ans = 0;
    for (auto e : ma) {
        ans += e.second-1;
    }
    ans += max(0ll,(ll)uni.size()-(ll)ma.size()-cnt);
    std::cout << ans << std::endl;
};
0