結果

問題 No.1639 最小通信路
ユーザー Hydrogen332
提出日時 2024-10-27 10:03:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,300 bytes
コンパイル時間 2,133 ms
コンパイル使用メモリ 197,308 KB
最終ジャッジ日時 2025-02-25 00:45:42
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)
#define all(a) (a).begin(), (a).end()

struct UnionFind {
    vector<int> par, siz;
    int v, group;
    
    UnionFind(int n) {
        par = vector<int>(n, -1);
        siz = vector<int>(n, 1);
        v = n;
        group = n;
    }
    
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        
        if (x == y) return false;
        
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        group--;
        return true;
    }
    
    int size(int x) {
        return siz[root(x)];
    }
};

int main() {
    cin.tie(nullptr);
    
    int N;
    cin >> N;
    
    int edge = N * (N - 1) / 2;
    UnionFind uf(N);
    
    rep(e, 0, edge) {
        int a, b;
        string C;
        cin >> a >> b >> C;
        a--, b--;
        
        if (!uf.same(a, b)) {
            uf.unite(a, b);
            if (uf.group == 1) {
                cout << C << '\n';
                break;
            }
        }
    }
}
0