結果

問題 No.1639 最小通信路
ユーザー rianoriano
提出日時 2021-08-06 22:05:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,753 bytes
コンパイル時間 2,223 ms
コンパイル使用メモリ 174,516 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 03:27:58
合計ジャッジ時間 2,876 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 1 ms
4,348 KB
testcase_39 AC 1 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 1 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
#define double long double

const ll mod = 1000000007;


//Unionfind
struct unionfind {
	//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
	//そうでなければ親のid
	  vector<long long> r;
      vector<bool> act;
	  unionfind(long long N) {
		    r = vector<long long>(N, -1);
            act = vector<bool>(N,false);
	  }
	  long long root(long long x) {
		    if (r[x] < 0) return x;
		    return r[x] = root(r[x]);
	  }
	  bool unite(long long x, long long y) {
		    x = root(x);
		    y = root(y);
		    if (x == y) return false;
		    if (r[x] > r[y]) swap(x, y);
		    r[x] += r[y];
		    r[y] = x;
		    return true;
	  }
	  long long size(long long x) {
		    return max(-r[root(x)],0LL);
	  }
  
  // 2つのデータx, yが属する木が同じならtrueを返す
    bool same(long long x, long long y) { 
        long long rx = root(x);
        long long ry = root(y);
        if(rx==ry){
            act[rx] = true;
        }
        return rx == ry;
    }
    bool activate(ll x){
        long long rx = root(x);
        return act[rx];
    }
};
  //main関数内で unionfind friends(N+1);
  //などと宣言し  friends.unite(a,b);
  //のように使う
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    // ll N; cin >> N;
    // ll a[N],b[N];
    // ll cnt[N+1];
    // vector<ll> app(N+1);
    // rep(i,N+1) cnt[i] = 0;
    // rep(i,N){
    //     cin >> a[i] >> b[i];
    //     cnt[a[i]]++; cnt[b[i]]++;
    //     app[a[i]].push_back(i);
    //     app[b[i]].push_back(i);
    // }
    // unionfind balls(N+1);
    // rep(i,N){
    //     if(!balls.same(a[i],b[i])){
    //         balls.unite(a[i],b[i]);
    //     }
    // }
    // rep(i,N){
    //     if(!balls.activate(i+1)){
    //         cout << "No" << endl; return 0;
    //     }
    // }
    // cout << "Yes" << endl;
    // ll ans[N];
    // rep(i,N) ans[i] = -1;
    // queue<ll> em;
    // rep(i,N){
    //     if(app[i+1].size()==1){
    //         em[i]
    //     }
    // }
    // set<ll> col;
    // rep(i,N){
    //     if(cnt[a[i]]>cnt[b[i]]){
    //         cout << b[i] << endl;
    //         cnt[b[i]] = -2e9;
    //     }
    //     cout << a[i] << endl;
    //     cnt[a[i]] = -2e9;
    // }
    ll N; cin >> N;
    unionfind tws(N+1);
    rep(i,N*(N-1)/2){
        ll a,b; string c; cin >> a >> b >> c;
        if(tws.same(a,b)) continue;
        tws.unite(a,b);
        if(tws.size(a)==N){
            cout << c << endl; return 0;
        }
    }
    //cout << ans << endl;
}
0