結果

問題 No.19 ステージの選択
ユーザー furafura
提出日時 2020-01-04 00:41:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 771 bytes
コンパイル時間 1,604 ms
コンパイル使用メモリ 173,828 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-02 07:48:33
合計ジャッジ時間 2,471 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

vector<vector<int>> find_cycles(const vector<int>& f){
	int n=f.size();
	vector<vector<int>> Res;
	vector<int> color(n,-1);
	rep(u,n) if(color[u]==-1) {
		int v=u;
		do{ color[v]=u; v=f[v]; }while(color[v]==-1);
		if(color[v]==u){
			vector<int> C;
			int w=v;
			do{ C.emplace_back(v); v=f[v]; }while(v!=w);
			Res.emplace_back(C);
		}
	}
	return Res;
}

int main(){
	int n; scanf("%d",&n);
	vector<int> cost(n),to(n);
	rep(u,n) scanf("%d%d",&cost[u],&to[u]), to[u]--;

	double ans=accumulate(cost.begin(),cost.end(),0)/2.0;
	for(auto C:find_cycles(to)){
		int mn=INF;
		for(int u:C) mn=min(mn,cost[u]);
		ans+=mn/2.0;
	}
	printf("%.1f\n",ans);

	return 0;
}
0