結果

問題 No.19 ステージの選択
ユーザー cielciel
提出日時 2015-04-24 17:44:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,805 bytes
コンパイル時間 567 ms
コンパイル使用メモリ 59,520 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:28:24
合計ジャッジ時間 1,565 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;

typedef vector<vector<int> > Graph;

void visit(const Graph &g, int v, vector< vector<int> >& scc,
    stack<int> &S, vector<bool> &inS,
    vector<int> &low, vector<int> &num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v); inS[v] = true;
  FOR(e, g[v]) {
    int w = *e/*->dst*/;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top(); S.pop(); inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}
void stronglyConnectedComponents(const Graph& g,
    vector< vector<int> >& scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  REP(u, n) if (num[u] == 0)
    visit(g, u, scc, S, inS, low, num, time);
}

int main(){
	int V;
	scanf("%d",&V);
	Graph g(V);
	vector<double> L(V);
	vector<int>A(V);
	
	double R=0;
	for(int i=0;i<V;i++){
		int s;
		scanf("%lf%d",&L[i],&s);
		R+=L[i];
		g[s-1].push_back(i);
		A[i]=s-1;
	}
	vector< vector<int> > scc;
	stronglyConnectedComponents(g,scc);

	vector<double>mn(scc.size());
	fill(mn.begin(),mn.end(),1e9);
	vector<int>com(V),deg(scc.size());
	for(int i=0;i<scc.size();i++){
		for(auto &e:scc[i])com[e]=i;
	}

	for(int i=0;i<V;i++)if(com[A[i]]!=com[i])deg[com[i]]++;
	for(int i=0;i<V;i++)mn[com[i]]=min(mn[com[i]],L[i]);
	for(int i=0;i<scc.size();i++)if(!deg[i])R+=mn[i];
	printf("%.1f\n",R/2);
}
0