結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
|
| 提出日時 | 2025-05-12 19:12:12 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,714 bytes |
| コンパイル時間 | 2,203 ms |
| コンパイル使用メモリ | 212,624 KB |
| 実行使用メモリ | 7,844 KB |
| 最終ジャッジ日時 | 2025-05-12 19:12:16 |
| 合計ジャッジ時間 | 3,403 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
pair<vector<int>,vector<vector<int>>> FunctionalGraph(vector<int> &A){
//N頂点N辺有向出次数全て1のグラフでreturn{cycle所属番号(-1は未所属),cycle頂点集合}.
int N = A.size(),nowc = 0;
vector<int> belong(N,-1);
vector<vector<int>> rev(N),cycle;
for(int i=0; i<N; i++) rev.at(A.at(i)).push_back(i);
vector<bool> already(N),visited(N);
for(int i=0; i<N; i++){
if(already.at(i)) continue;
int pos = i;
while(!visited.at(pos)) visited.at(pos) = true,pos = A.at(pos);
int memo = pos;
cycle.push_back({pos}); belong.at(pos) = nowc; pos = A.at(pos);
while(pos != memo){
cycle.at(nowc).push_back(pos); belong.at(pos) = nowc;
pos = A.at(pos);
}
already.at(pos) = true;
queue<int> Q; Q.push(pos);
while(Q.size()){
int pos2 = Q.front(); Q.pop();
for(auto to : rev.at(pos2)){
if(already.at(to)) continue;
already.at(to) = true; Q.push(to);
}
}
nowc++;
}
return {belong,cycle};
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
vector<int> L(N),A(N);
for(int i=0; i<N; i++) cin >> L.at(i) >> A.at(i),A.at(i)--;
auto [belong,cycle] = FunctionalGraph(A);
int answer = 0;
for(int i=0; i<N; i++) if(belong.at(i) == -1) answer += L.at(i);
for(auto c : cycle){
int sum = 0,mins = 1001001001;
for(auto pos : c) sum += L.at(pos),mins = min(mins,L.at(pos));
answer += sum+mins;
}
cout << answer/2 << "." << (answer%2?5:0) << endl;
}