結果

問題 No.1488 Max Score of the Tree
ユーザー MZKiMZKi
提出日時 2021-04-06 13:12:10
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,410 bytes
コンパイル時間 6,606 ms
コンパイル使用メモリ 272,740 KB
実行使用メモリ 5,328 KB
最終ジャッジ日時 2023-08-31 00:21:18
合計ジャッジ時間 7,722 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 RE -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long

#include "testlib.h"

class UnionFind {
public:
	vector < ll > par;
	vector < ll > siz;
	UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(ll sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1);
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	ll root(ll x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}
	bool unite(ll x, ll y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}
	bool same(ll x, ll y) {
		return root(x) == root(y);
	}
	ll size(ll x) {
		return siz[root(x)];
	}
};

int main(int argc, char* argv[]) {
    registerValidation(argc, argv);
    int n = inf.readInt(2, 100, "n");
    inf.readSpace();
    int k = inf.readInt(2, 100000, "k");
    inf.readEoln();
    
    UnionFind uf(n);
    map<pair<int, int>, int> mp;
    
    for(int i = 0; i < n-1; i++){
        int a = inf.readInt(1, 100000, "a");
        inf.readSpace();
        int b = inf.readInt(1, 100000, "b");
        inf.readSpace();
        inf.readInt(1, k);
        inf.readEoln();
        a--; b--;
        assert(a != b);
        int now = mp[{a, b}];
        assert(now == 0);
        mp[{a, b}]++;
        assert(uf.same(a, b) == false);
        uf.unite(a, b);
    }
    inf.readEof();
}
0