結果

問題 No.19 ステージの選択
ユーザー どららどらら
提出日時 2015-06-03 23:32:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,109 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 80,084 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:28:41
合計ジャッジ時間 1,760 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0