結果

問題 No.1488 Max Score of the Tree
ユーザー maguromaguro
提出日時 2021-04-06 11:50:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,687 bytes
コンパイル時間 7,267 ms
コンパイル使用メモリ 312,704 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-30 23:07:23
合計ジャッジ時間 8,880 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
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>
#include "testlib.h"
using namespace std;

struct Union_find {
	vector<int> par; //親
	vector<int> siz; //根ノードiの木に含まれる要素数。iが根ノード出ない場合無意味な値となる。

	//n要素で初期化
	Union_find(int n) {
		par.resize(n);
		siz.resize(n);
		for(int i = 0;i < n;i++) {
			par[i] = i;
			siz[i] = 1;
		}
	}

	//木の根を求める
	int find(int x) {
		if(par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	//xとyの属する集合を併合
	void unite(int x,int y) {
		x = find(x);
		y = find(y);
		if(x == y) {
			return;
		}
		if(siz[x] < siz[y]) {
			swap(x,y);
		}
		par[y] = x;
		siz[x] += siz[y];
	}

	//xとyが同じ集合に属するか否か
	bool same(int x,int y) {
		return find(x) == find(y);
	}

	int size(int x) {
		return siz[find(x)];
	}
};

//A,空白,B,改行の順に読み込み(1 <= A,B <= 100)
int main() {
    registerValidation();
    int N = inf.readInt(2,100);
    inf.readSpace();
    int K = inf.readInt(1,100000);
    inf.readEoln();
    set<pair<int,int>> edges;
    Union_find uf(N + 1);
    for(int i = 0;i < N - 1;i++) {
        int a = inf.readInt(1,N);
        inf.readSpace();
        int b = inf.readInt(1,N);
        inf.readSpace();
        int c = inf.readInt(1,K);
        inf.readEoln();
        ensuref(a != b,"Tree can't contain loops");
        ensuref(!edges.count({a,b}),"Tree can't contain multiple edges between a pair of vertices");
        edges.insert({a,b});
        edges.insert({b,a});
        ensuref(!uf.same(a,b),"Tree can't contain cycles");
        uf.unite(a,b);
    }
    inf.readEof();
    return 0;
}
0