結果

問題 No.1639 最小通信路
ユーザー SSRS
提出日時 2021-08-06 21:32:36
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 170,404 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-17 01:23:32
合計ジャッジ時間 2,871 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct unionfind{
	vector<int> p;
	unionfind(int N){
		p = vector<int>(N, -1);
	}
	int root(int x){
		if (p[x] < 0){
			return x;
		} else {
			p[x] = root(p[x]);
			return p[x];
		}
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x != y){
			if (p[x] < p[y]){
				swap(x, y);
			}
			p[y] += p[x];
			p[x] = y;
		}
	}
};
int main(){
  int N;
  cin >> N;
  unionfind UF(N);
  int cnt = 0;
  for (int i = 0; i < N * (N - 1) / 2; i++){
    int a, b;
    string C;
    cin >> a >> b >> C;
    a--;
    b--;
    if (!UF.same(a, b)){
      UF.unite(a, b);
      cnt++;
      if (cnt == N - 1){
        cout << C << endl;
        break;
      }
    }
  }
}
0