結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
koyumeishi
|
| 提出日時 | 2015-09-08 03:15:18 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,553 bytes |
| コンパイル時間 | 800 ms |
| コンパイル使用メモリ | 81,480 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-23 10:04:03 |
| 合計ジャッジ時間 | 1,807 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
class UnionFindTree{
typedef struct {
int parent;
int rank;
}base_node;
vector<base_node> node;
public:
UnionFindTree(int n){
node.resize(n);
for(int i=0; i<n; i++){
node[i].parent=i;
node[i].rank=0;
}
}
int find(int x){
if(node[x].parent == x) return x;
else{
return node[x].parent = find(node[x].parent);
}
}
bool same(int x, int y){
return find(x) == find(y);
}
void unite(int x, int y){
x = find(node[x].parent);
y = find(node[y].parent);
if(x==y) return;
if(node[x].rank < node[y].rank){
node[x].parent = y;
}else if(node[x].rank > node[y].rank){
node[y].parent = x;
}else{
node[x].rank++;
unite(x,y);
}
}
};
int main(){
int n;
cin >> n;
vector<long long> L(n), S(n);
UnionFindTree uft(n);
long long sum = 0;
for(int i=0; i<n; i++){
cin >> L[i] >> S[i];
S[i]--;
sum += L[i];
uft.unite(i, S[i]);
}
long long hoge = 0;
vector<bool> visit(n, false);
for(int i=0; i<n; i++){
if(visit[uft.find(i)]) continue;
int pos = i;
while(visit[pos] == false){
visit[pos] = true;
pos = S[pos];
}
int start = pos;
long long cost = L[pos];
pos = S[pos];
while(pos != start){
cost = min(cost, L[pos]);
pos = S[pos];
}
hoge += cost;
visit[uft.find(pos)] = true;
}
double ans = sum/2.0 + hoge/2.0;
printf("%.1f\n", ans);
return 0;
}
koyumeishi