結果

問題 No.19 ステージの選択
ユーザー hotpepsi
提出日時 2015-06-08 22:09:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,088 bytes
コンパイル時間 660 ms
コンパイル使用メモリ 67,464 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 10:03:05
合計ジャッジ時間 1,586 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>

using namespace std;

typedef vector<int> IntVec;

int Level[100];
int Dependencies[100];
IntVec Edges[100];
int Visited[100];
int Cleared[100];

int dfs(int a) {
	int s = Level[a] / 2;
	Visited[a] = 1;
	for (int b : Edges[a]) {
		if (!Visited[b] && !Cleared[b]) {
			s += dfs(b);
		}
	}
	return s;
}

int main(int argc, char *argv[])
{
	string s;
	getline(cin, s);
	int N = atoi(s.c_str());
	for (int i = 0; i < N; ++i) {
		getline(cin, s);
		stringstream ss(s);
		int L, S;
		ss >> L >> S;
		Level[i] = L * 2;
		Dependencies[i] = S - 1;
		Edges[S - 1].push_back(i);
	}

	int tot = 0;
	int cl = 0;
	while (cl < N) {
		int n = -1;
		int s = -1;
		for (int i = 0; i < N; ++i) {
			if (!Cleared[i]) {
				memset(Visited, 0, sizeof(Visited));
				int e = dfs(i) - Level[i] / 2;
				if (e > s) {
					n = i;
					s = e;
				}
			}
		}
		tot += Cleared[Dependencies[n]] ? Level[n] / 2 : Level[n];
		Cleared[n] = 1;
		++cl;
	}
	printf("%.1f\n", (double)tot * 0.5);
	return 0;
}
0