結果

問題 No.1639 最小通信路
ユーザー erbowl
提出日時 2022-09-01 22:30:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,218 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 196,760 KB
最終ジャッジ日時 2025-02-07 00:26:14
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// #include <boost/multiprecision/cpp_int.hpp>

// namespace mp = boost::multiprecision;

    // mp::cpp_int x = 1;


struct UnionFind {
    vector<int> par;

    UnionFind() { }
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(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)];
    }
};

signed main(){
    ll n;
    std::cin >> n;
    UnionFind uf(n);
    for (int i = 0; i < n*(n-1)/2; i++) {
        ll a,b;
        string c;
        std::cin >> a>>b>>c;
        a--;b--;
        if(uf.issame(a,b))continue;
        uf.merge(a,b);
        if(uf.size(a)==n){
            std::cout << c << std::endl;
            break;
        }
    }
}
0