#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]), mx = max(mx, MaxIn[x.x].x + MaxIn[x.x].y);
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();
int p = par[now].x, w = par[now].y;
MaxAll[now] = max(MaxAll[now], MaxIn[now].x + MaxIn[now].y);
MaxAll[p] = max(MaxAll[p], MaxAll[now]);
long ret = MaxIn[now].x + w;
if(ret > MaxIn[p].x) swap(ret, MaxIn[p].x);
if(ret > MaxIn[p].y) swap(ret, MaxIn[p].y);
if(!--deg[p]) Q.emplace(p);
}
for(int i = 1; i <= n; ++i) if(deg[i]) solve(i);
printf("%lld\n", Answer);
}
Compilation message
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 time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
612 KB |
Output is correct |
9 |
Correct |
2 ms |
612 KB |
Output is correct |
10 |
Correct |
3 ms |
652 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
652 KB |
Output is correct |
2 |
Correct |
3 ms |
652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
704 KB |
Output is correct |
2 |
Correct |
4 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1260 KB |
Output is correct |
2 |
Correct |
15 ms |
2540 KB |
Output is correct |
3 |
Correct |
16 ms |
2540 KB |
Output is correct |
4 |
Correct |
6 ms |
2540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
3448 KB |
Output is correct |
2 |
Correct |
44 ms |
5188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
10064 KB |
Output is correct |
2 |
Correct |
105 ms |
10380 KB |
Output is correct |
3 |
Correct |
113 ms |
15876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
16636 KB |
Output is correct |
2 |
Correct |
210 ms |
27284 KB |
Output is correct |
3 |
Correct |
178 ms |
27284 KB |
Output is correct |
4 |
Correct |
257 ms |
37100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
37100 KB |
Output is correct |
2 |
Correct |
486 ms |
44140 KB |
Output is correct |
3 |
Correct |
395 ms |
44140 KB |
Output is correct |
4 |
Correct |
382 ms |
53140 KB |
Output is correct |
5 |
Correct |
354 ms |
54036 KB |
Output is correct |
6 |
Correct |
792 ms |
54036 KB |
Output is correct |
7 |
Correct |
524 ms |
61784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
556 ms |
61784 KB |
Output is correct |
2 |
Correct |
403 ms |
61784 KB |
Output is correct |
3 |
Correct |
559 ms |
61784 KB |
Output is correct |
4 |
Correct |
436 ms |
61784 KB |
Output is correct |
5 |
Correct |
344 ms |
61784 KB |
Output is correct |
6 |
Correct |
363 ms |
61784 KB |
Output is correct |
7 |
Correct |
679 ms |
61784 KB |
Output is correct |