結果

問題 No.19 ステージの選択
ユーザー atn112323atn112323
提出日時 2016-05-06 11:31:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,664 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 70,092 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 13:30:11
合計ジャッジ時間 1,703 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 2 ms
5,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

class UnionFindTree {
public:
	explicit UnionFindTree(int size) {
		size_ = size;
		parent_ = new int[size];
		rank_ = new int[size];
		init();
	}
	~UnionFindTree() {
		delete parent_;
		delete rank_;
	}
	void init() {
		for (int i = 0; i < size_; i++) {
			parent_[i] = i;
			rank_[i] = 0;
		}
	}
	int find(int x) {
		if (parent_[x] == x) {
			return x;
		} else {
			return parent_[x] = find(parent_[x]);
		}
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x != y) {
			if (rank_[x] < rank_[y]) {
				parent_[x] = y;
			} else {
				parent_[y] = x;
				if (rank_[x] == rank_[y]) {
					rank_[x]++;
				}
			}
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}

private:
	int size_;
	int* parent_;
	int* rank_;
};

const int INF = 1000000;

int N;
int L[100], S[100];
bool used[100];
int cnt[100];

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> S[i];
		S[i]--;
	}
	UnionFindTree uft(N);
	for (int i = 0; i < N; i++) {
		uft.unite(i, S[i]);
		cnt[S[i]]++;
	}
	double res = 0;
	for (int i = 0; i < N; i++) {
		if (used[i]) {
			continue;
		}
		queue<int> Q;
		for (int j = i; j < N; j++) {
			if (uft.same(i, j)) {
				res += L[j];
				used[j] = true;
				if (cnt[j] == 0) {
					Q.push(j);
				}
			}
		}
		while (!Q.empty()) {
			int idx = Q.front();
			Q.pop();
			if (--cnt[S[idx]] == 0) {
				Q.push(S[idx]);
			}
		}
		int m = INF;
		for (int j = i; j < N; j++) {
			if (uft.same(i, j) && cnt[j] > 0) {
				m = min(m, L[j]);
			}
		}
		if (m < INF) {
			res += m;
		}
	}
	res /= 2.0;
	cout << res << endl;
	return 0;
}
0