結果

問題 No.19 ステージの選択
ユーザー kazumakazuma
提出日時 2017-06-25 21:48:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,534 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 174,924 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 23:32:34
合計ジャッジ時間 2,556 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Edge {
	int from, to;
	Edge(int f, int t) : from(f), to(t) {}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

vector<int> tarjan(const Graph& g) {
	int n = g.size(), idx = 0, k = 0, s = 0;
	vector<int> ord(n, -1), low(n), onS(n), cmp(n), stk(n);
	function<void(int)> dfs = [&](int v) {
		ord[v] = low[v] = idx++;
		stk[s++] = v;
		onS[v] = true;
		for (auto &e : g[v]) {
			int w = e.to;
			if (ord[w] == -1) {
				dfs(w);
				low[v] = min(low[v], low[w]);
			}
			else if (onS[w]) {
				low[v] = min(low[v], ord[w]);
			}
		}
		if (low[v] == ord[v]) {
			while (1) {
				int w = stk[--s];
				onS[w] = false;
				cmp[w] = k;
				if (w == v)break;
			}
			++k;
		}
	};
	for (int v = 0; v < n; ++v)
		if (ord[v] == -1) dfs(v);
	return cmp;
}

int main()
{
	cin.sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(1);
	int N, res = 0;
	cin >> N;
	Graph G(N);
	vector<int> L(N);
	Edges es;
	for (int i = 0, S; i < N; i++) {
		cin >> L[i] >> S; S--;
		G[S].emplace_back(S, i);
		res += L[i];
		es.emplace_back(S, i);
	}
	auto v = tarjan(G);
	int V = 0;
	for (int i = 0; i < N; i++) {
		V = max(V, v[i] + 1);
	}
	vector<int> val(V, 100);
	for (int i = 0; i < N; i++) {
		val[v[i]] = min(val[v[i]], L[i]);
	}
	Graph rDAG(V);
	for (auto e : es) {
		if (v[e.from] != v[e.to]) {
			rDAG[v[e.to]].emplace_back(v[e.to], v[e.from]);
		}
	}
	for (int i = 0; i < V; i++) {
		if (rDAG[i].empty()) {
			res += val[i];
		}
	}
	cout << res / 2.0 << endl;
	return 0;
}
0