Submission #51840

#TimeUsernameProblemLanguageResultExecution timeMemory
51840win11905Islands (IOI08_islands)C++11
87 / 100
877 ms52720 KiB
#include <bits/stdc++.h> #define long long long #define pii pair<long, long> #define x first #define y second using namespace std; const int N = 1e6+5; int n, deg[N]; long Answer, MaxAll[N]; pii par[N], MaxIn[N]; void solve(int p) { vector<pii> V; while(deg[p]) V.emplace_back(p, par[p].y), deg[p]--, p = par[p].x; int s = V.size(); deque<pii> Q; long dis = V[0].y, pdis = 0, mx = 0; for(auto x : V) mx = max(mx, MaxAll[x.x]); for(int i = 1; i < s; ++i) { while(!Q.empty() && Q.back().x < dis + MaxIn[V[i].x].x) Q.pop_back(); Q.push_back(pii(dis + MaxIn[V[i].x].x, i)); dis += V[i].y; } mx = max(mx, MaxIn[V[0].x].x + Q.front().x); for(int i = 0; i < s-1; ++i) { if(Q.front().y == i+1) Q.pop_front(); while(!Q.empty() && Q.back().x < dis + MaxIn[V[i].x].x) Q.pop_back(); Q.push_back(pii(dis + MaxIn[V[i].x].x, i)); dis += V[i].y, pdis += V[i].y; mx = max(mx, MaxIn[V[i+1].x].x + Q.front().x - pdis); } Answer += mx; } int main() { scanf("%d", &n); for(int i = 1, a, b; i <= n; ++i) { scanf("%d %d", &a, &b); par[i] = pii(a, b); deg[a]++; } queue<int> Q; for(int i = 1; i <= n; ++i) if(!deg[i]) Q.emplace(i); while(!Q.empty()) { int now = Q.front(); Q.pop(); MaxAll[now] = max(MaxAll[now], MaxIn[now].x + MaxIn[now].y); MaxAll[par[now].x] = max(MaxAll[par[now].x], MaxAll[now]); long ret = MaxIn[now].x + par[now].y; if(ret > MaxIn[par[now].x].x) swap(ret, MaxIn[par[now].x].x); if(ret > MaxIn[par[now].x].y) swap(ret, MaxIn[par[now].x].y); if(!--deg[par[now].x]) Q.emplace(par[now].x); } for(int i = 1; i <= n; ++i) if(deg[i]) solve(i); printf("%lld\n", Answer); }

Compilation message (stderr)

islands.cpp: In function 'int main()':
islands.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
islands.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...