結果
| 問題 |
No.1639 最小通信路
|
| コンテスト | |
| ユーザー |
ぷら
|
| 提出日時 | 2021-08-06 21:48:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 19 ms / 2,000 ms |
| コード長 | 1,660 bytes |
| コンパイル時間 | 2,451 ms |
| コンパイル使用メモリ | 210,820 KB |
| 最終ジャッジ日時 | 2025-01-23 15:02:38 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 43 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
vector<int> size;
UnionFind(int n) {
par.resize(n);
size.resize(n,1);
for(int i = 0; i < n; i++) {
par[i] = i;
}
}
int find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
int consize(int x) {
return size[find(x)];
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return;
}
if(size[x] < size[y]) {
par[x] = y;
size[y] += size[x];
}
else {
par[y] = x;
size[x] += size[y];
}
}
};
bool f(pair<string,pair<int,int>>x,pair<string,pair<int,int>>y) {
if(x.first.size() < y.first.size()) {
return true;
}
if(x.first.size() > y.first.size()) {
return false;
}
if(x.first <= y.first) {
return true;
}
return false;
}
int main() {
int N;
cin >> N;
vector<pair<string,pair<int,int>>>tmp(N*(N-1)/2);
for(int i = 0; i < N*(N-1)/2; i++) {
cin >> tmp[i].second.first >> tmp[i].second.second >> tmp[i].first;
tmp[i].second.first--;
tmp[i].second.second--;
}
sort(tmp.begin(),tmp.end(),f);
UnionFind uf(N);
for(int i = 0; i < tmp.size(); i++) {
uf.unite(tmp[i].second.first,tmp[i].second.second);
if(uf.consize(0) == N) {
cout << tmp[i].first << endl;
return 0;
}
}
}
ぷら