結果

問題 No.1639 最小通信路
ユーザー 👑 CleyL
提出日時 2023-06-26 08:58:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,083 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 75,324 KB
最終ジャッジ日時 2025-02-15 02:21:04
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#line 1 "CP/_library/cpp/data-structure/union-find.cpp"
struct UnionFind {
  int n;
  int ce; //connected element
  vector<int> par;
  vector<int> size_;
  UnionFind(int n_) : n(n_), ce(n_), par((size_t)n_), size_((size_t)n_,1){
    for(int i = 0; n > i; i++)par[i] = i;
  }

  int root(int x){
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    ce--;
    size_[rb] += size_[ra];
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};

int main(){
  int n;cin>>n;
  UnionFind A(n);
  for(int i = 0; n*(n-1)/2 > i; i++){
    int a,b;string c;cin>>a>>b>>c;
    a--;b--;
    A.unite(a,b);
    if(A.ce == 1){
      cout << c << endl;
      return 0;
    }
  }
}
0