이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string to_string(pi p) {
return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}
template<class T> string to_string(T t) {
string s = "[";
bool f = false;
for(auto&e:t) {
if(f) s += ", ";
s += to_string(e);
f = true;
}
s += "]";
return s;
}
void dbg() { cerr << "]" << endl; }
template<class H, class...T> void dbg(H h, T... t) {
cerr << to_string(h);
if(sizeof...(t)) cerr << ", ";
dbg(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbg(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 2e5 + 7;
int p[N], h[N], c[N];
vi G[N];
multiset<pi> dfs(int u) {
multiset<pi> s;
for(int v:G[u]) {
auto t = dfs(v);
if(SZ(s) < SZ(t)) swap(s, t);
for(auto& e:t) s.insert(e);
}
s.insert({h[u], c[u]});
auto it = s.lower_bound({h[u] + 1, -INF});
int res = -c[u], lst = -1;
while(it != s.end() && res < 0) {
res += it -> S;
lst = it -> F;
it = s.erase(it);
}
if(res > 0 && lst != -1)
s.insert({lst, res});
return s;
}
int cycle[N], vis[N], incycle[N];
map<int, ll> mp[N];
multiset<pi> s[N];
signed main()
{
IO_OP;
int n;
cin >> n;
ll tot = 0;
for(int i = 1; i <= n; i++) {
cin >> p[i] >> h[i] >> c[i];
h[i] = 1e9 + 1 - h[i]; // Read carefully...
G[p[i]].PB(i);
tot += c[i];
}
for(int i = 1; i <= n; i++) if(!vis[i]) {
int u = i;
vi tt;
while(!vis[u]) {
vis[u] = 1;
tt.PB(u);
u = p[u];
}
if(cycle[u]) {
for(int v:tt) cycle[v] = cycle[u];
} else {
for(int v:tt) cycle[v] = u;
}
}
for(int i = 1; i <= n; i++) {
if(cycle[i] == i) {
int u = i;
do {
incycle[u] = 1;
u = p[u];
} while(u != i);
}
}
for(int i = 1; i <= n; i++) {
debug(i, cycle[i], incycle[i]);
}
for(int i = 1; i <= n; i++) if(incycle[i]) {
mp[cycle[i]][h[i]] += c[i];
auto& ss = s[cycle[i]];
for(int v:G[i]) if(!incycle[v]) {
auto tt = dfs(v);
if(SZ(tt) > SZ(ss)) swap(ss, tt);
for(auto& e:tt)
ss.insert(e);
}
}
ll ans = 0;
for(int i = 1; i <= n; i++) if(cycle[i] == i) {
mp[i][INF] += 0;
auto& ss = s[i];
auto it = ss.begin();
ll sum = 0, mx = 0;
for(auto[H, cost]:mp[i]) {
while(it != ss.end() && it -> F <= H) {
sum += it -> S;
it++;
}
mx = max(mx, sum + cost);
}
ans += mx;
}
cout << tot - ans << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:133:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
133 | for(auto[H, cost]:mp[i]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |