結果
問題 | No.19 ステージの選択 |
ユーザー | yuji9511 |
提出日時 | 2020-12-21 22:56:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 3,465 bytes |
コンパイル時間 | 2,554 ms |
コンパイル使用メモリ | 226,312 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 13:12:19 |
合計ジャッジ時間 | 3,627 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 1 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 1 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
ソースコード
/*** author: yuji9511 ***/ #include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; using lpair = pair<ll, ll>; using vll = vector<ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} using UnWeightedGraph = vector<vll>; template< typename G > struct SCC { const G &g; UnWeightedGraph gg, rg; vll comp, order, used; SCC(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) { for(int i = 0; i < g.size(); i++) { for(auto e : g[i]) { gg[i].emplace_back((int) e); rg[(int) e].emplace_back(i); } } } int operator[](int k) { return comp[k]; } void dfs(int idx) { if(used[idx]) return; used[idx] = true; for(int to : gg[idx]) dfs(to); order.push_back(idx); } void rdfs(int idx, int cnt) { if(comp[idx] != -1) return; comp[idx] = cnt; for(int to : rg[idx]) rdfs(to, cnt); } void build(UnWeightedGraph &t) { for(int i = 0; i < gg.size(); i++) dfs(i); reverse(begin(order), end(order)); int ptr = 0; for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++; t.resize(ptr); for(int i = 0; i < g.size(); i++) { for(auto &to : g[i]) { int x = comp[i], y = comp[to]; if(x == y) continue; t[x].push_back(y); } } } }; struct UnionFind { private: ll N; vector<ll> parent; vector<ll> num; vector<ll> diff_weight; public: UnionFind(ll n){ N = n; parent.resize(N); iota(parent.begin(), parent.end(), 0); num.assign(N, 1); diff_weight.assign(N, 0); } ll root(ll x){ if(x == parent[x]){ return x; }else{ ll r = root(parent[x]); diff_weight[x] += diff_weight[parent[x]]; return parent[x] = r; } } void unite(ll a, ll b, ll w = 0){ w += weight(a); w -= weight(b); a = root(a); b = root(b); if(a == b) return; parent[b] = a; ll sum = num[a] + num[b]; num[a] = sum; num[b] = sum; diff_weight[b] = w; } bool same(ll a, ll b){ return root(a) == root(b);} ll sz(ll x){ return num[root(x)];} ll weight(ll x){ root(x); return diff_weight[x]; } ll diff(ll a, ll b){ return weight(b) - weight(a); } }; void solve(){ ll N; cin >> N; vll L(N), S(N); UnWeightedGraph g(N); UnionFind uf(N); map<ll,bool> loop; rep(i,0,N){ cin >> L[i] >> S[i]; S[i]--; g[S[i]].push_back(i); uf.unite(i, S[i]); if(i == S[i]) loop[i] = true; } UnWeightedGraph tree; SCC scc(g); scc.build(tree); double ans = 0.0; vll min_val(N+1, INF); vll cnt(N+1, 0); rep(i,0,N) cnt[scc[i]]++; rep(i,0,N){ if(cnt[scc[i]] == 1 && loop[i] == false) continue; min_val[scc[i]] = min(min_val[scc[i]], L[i]); } rep(i,0,N){ ans += (double) L[i] / 2.0; } rep(i,0,N){ if(min_val[i] != INF){ ans +=(double) min_val[i] / 2.0; } } printf("%.1f\n", ans); } int main(){ cin.tie(0); ios::sync_with_stdio(false); solve(); }