結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
どらら
|
| 提出日時 | 2015-06-03 23:32:35 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 2,109 bytes |
| コンパイル時間 | 823 ms |
| コンパイル使用メモリ | 85,412 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-23 10:02:54 |
| 合計ジャッジ時間 | 1,563 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
class UnionFind{
int n;
vector<int> data;
public:
UnionFind(int _n):n(_n),data(_n,-1){}
bool link(int a, int b){ //if new link, then true.
int ra=find_root(a);
int rb=find_root(b);
if(ra!=rb){
if(data[rb]<data[ra]) swap(ra,rb);
data[ra]+=data[rb];
data[rb]=ra;
}
return (ra!=rb);
}
bool check(int a, int b){ //if same set, then true.
return (find_root(a)==find_root(b));
}
int find_root(int a){
return ((data[a]<0)?a:(data[a]=find_root(data[a])));
}
};
vector<int> G[105];
int visited[105];
int hangen[105];
vector<int> L, S;
int solve(int v){
if(G[v].size() == 0){
return L[v];
}
visited[v] = 1;
int ret = 0;
rep(i,G[v].size()){
if(visited[G[v][i]] > 0) continue;
ret += solve(G[v][i]);
}
return ret + L[v];
}
int main(){
int N, l, s;
cin >> N;
UnionFind uf(N);
rep(i,N){
cin >> l >> s;
L.push_back(l);
S.push_back(s-1);
uf.link(s-1, i);
G[s-1].push_back(i);
hangen[i] = 1;
}
rep(i,N){
vector<int> node;
rep(j,N){
if(uf.find_root(j)==i) node.push_back(j);
}
if(node.size() == 0) continue;
int mx = 0, mx_i = -1;
rep(j,node.size()){
rep(k,105) visited[k] = 0;
int res = solve(node[j]);
if(mx == res){
if(L[mx_i] > L[node[j]]){
mx_i = node[j];
}
}
else if(mx < res){
mx = res;
mx_i = node[j];
}
}
hangen[mx_i] = 0;
}
double ret = 0;
rep(i,N){
if(hangen[i]==1) ret += L[i]/2.0;
else ret += L[i];
}
printf("%.1lf\n", ret);
return 0;
}
どらら