結果

問題 No.19 ステージの選択
ユーザー atn112323atn112323
提出日時 2016-05-06 11:35:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,684 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 69,308 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:30:40
合計ジャッジ時間 1,698 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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]]++;
	}
	int 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;
		}
	}
	cout << res / 2 << "." << (res % 2 ? "5" : "0") << endl;
	return 0;
}
0