結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー erbowlerbowl
提出日時 2023-08-04 22:52:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,364 bytes
コンパイル時間 2,656 ms
コンパイル使用メモリ 208,808 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-22 21:12:43
合計ジャッジ時間 4,813 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 45 ms
6,940 KB
testcase_05 AC 87 ms
6,940 KB
testcase_06 AC 94 ms
6,940 KB
testcase_07 AC 56 ms
6,944 KB
testcase_08 AC 66 ms
6,940 KB
testcase_09 AC 101 ms
6,940 KB
testcase_10 AC 95 ms
6,940 KB
testcase_11 AC 73 ms
6,944 KB
testcase_12 AC 100 ms
6,944 KB
testcase_13 AC 67 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 WA -
testcase_16 WA -
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);
    for (int i = 0; i < m; i++) {
        ll u,v;
        
        std::cin >> u>>v;
        u--;v--;
        uf.merge(u,v);
        shu[u]++;
        nyu[v]++;
    }
    ll p = 0;
    set<ll> ps,ms,zs;
    for (int i = 0; i < n; i++) {
        // std::cout << shu[i]<<" "<<nyu[i] << std::endl;
        if(shu[i]-nyu[i]>0){
            p += shu[i]-nyu[i];
            ps.insert(uf.root(i));
        }else if(shu[i]==nyu[i]&&shu[i]>0){
            zs.insert(uf.root(i));
        }else{
            ms.insert(uf.root(i));
        }
    }
    for (auto e : ps) {
        zs.erase(e);
        ms.erase(e);
    }
    for (auto e : ms) {
        zs.erase(e);
    }
    // for (auto e : zs) {
    //     std::cout << e << std::endl;
    // }
    // std::cout << ps.size()<<" "<<ms.size()<<" "<<zs.size() << std::endl;
    // std::cout << max(0ll,p-1)<<" "<<max(0ll,((ll)ps.size()+(ll)ms.size()-p)-1)<<" "<<zs.size()<< std::endl;
    std::cout << max(0ll,p-1)+max(0ll,((ll)ps.size()+(ll)ms.size()-p)-1)+zs.size() << std::endl;
};
0