結果
| 問題 |
No.19 ステージの選択
|
| ユーザー |
renjyaku_int
|
| 提出日時 | 2020-10-31 20:59:41 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 3,953 bytes |
| コンパイル時間 | 2,888 ms |
| コンパイル使用メモリ | 208,104 KB |
| 最終ジャッジ日時 | 2025-01-15 18:42:21 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
struct StronglyConnectedComponents
{
vector<vector<int>> g;
vector<vector<int>> to, from;
vector<int> comp, order, used;
StronglyConnectedComponents(vector<vector<int>> &g) : g(g), to(g.size()), from(g.size()), comp(g.size(), -1), used(g.size())
{
for (int i = 0; i < g.size(); i++)
{
for (auto x : g[i])
{
to[i].push_back(x);
from[x].push_back(i);
}
}
}
int operator[](int k)
{
return comp[k];
}
void dfs(int now)
{
if (used[now])
return;
used[now] = true;
for (int x : to[now])
dfs(x);
order.push_back(now);
}
void rdfs(int now, int cnt)
{
if (comp[now] != -1)
return;
comp[now] = cnt;
for (int x : from[now])
rdfs(x, cnt);
}
vector<vector<int>> build()
{
vector<vector<int>> t;
for (int i = 0; i < to.size(); i++)
dfs(i);
reverse(order.begin(), order.end());
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 v : to[i])
{
int x = comp[i], y = comp[v];
if (x == y)
continue;
t[x].push_back(y);
}
}
return t;
}
};
vector<double> l;
void dfs(int k, vector<vector<int>> &t, vector<vector<int>> &u, vector<bool> &used, double &ans)
{
used[k] = true;
rep(i,u[k].size()){
ans += l[u[k][i]]/2;
}
rep(i,t[k].size()){
dfs(t[k][i],t,u,used,ans);
}
return;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> g(n);
vector<int> s(n);
l.resize(n);
rep(i, n)
{
cin >> l[i] >> s[i];
s[i]--;
g[s[i]].push_back(i);
}
StronglyConnectedComponents scc(g);
vector<vector<int>> t = scc.build();
//debug(scc.comp);
vector<vector<int>> u(t.size()); //u[i]:iに含まれる成分
rep(i, n)
{
u[scc[i]].push_back(i);
}
vector<bool> used(t.size(), false);
double ans = 0;
rep(i, t.size())
{
if (!used[i])
{
double temp = 100000000;
rep(j, u[i].size())
{
chmin(temp, l[u[i][j]]);
}
ans += temp / 2;
dfs(i, t, u, used, ans);
}
}
cout << fixed << setprecision(1);
cout<<ans<<"\n";
}
renjyaku_int