結果

問題 No.19 ステージの選択
ユーザー koyumeishikoyumeishi
提出日時 2015-09-08 03:15:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,553 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 81,048 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:29:54
合計ジャッジ時間 1,859 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

class UnionFindTree{
	typedef struct {
		int parent;
		int rank;
	}base_node;
	
	vector<base_node> node;
public:
	UnionFindTree(int n){
		node.resize(n);
		for(int i=0; i<n; i++){
			node[i].parent=i;
			node[i].rank=0;
		}
	}

	int find(int x){
		if(node[x].parent == x) return x;
		else{
			return node[x].parent = find(node[x].parent);
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		x = find(node[x].parent);
		y = find(node[y].parent);
		if(x==y) return;
		if(node[x].rank < node[y].rank){
			node[x].parent = y;
		}else if(node[x].rank > node[y].rank){
			node[y].parent = x;
		}else{
			node[x].rank++;
			unite(x,y);
		}
	}
};


int main(){
	int n;
	cin >> n;
	vector<long long> L(n), S(n);

	UnionFindTree uft(n);
	long long sum = 0;
	for(int i=0; i<n; i++){
		cin >> L[i] >> S[i];
		S[i]--;
		sum += L[i];

		uft.unite(i, S[i]);
	}

	long long hoge = 0;

	vector<bool> visit(n, false);
	for(int i=0; i<n; i++){
		if(visit[uft.find(i)]) continue;

		int pos = i;
		while(visit[pos] == false){
			visit[pos] = true;
			pos = S[pos];
		}
		int start = pos;
		long long cost = L[pos];
		pos = S[pos];
		while(pos != start){
			cost = min(cost, L[pos]);
			pos = S[pos];
		}
		hoge += cost;

		visit[uft.find(pos)] = true;
	}

	double ans = sum/2.0 + hoge/2.0;
	printf("%.1f\n", ans);


	return 0;
}
0