#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();
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);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
612 KB |
Output is correct |
4 |
Correct |
3 ms |
612 KB |
Output is correct |
5 |
Correct |
2 ms |
612 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
2 ms |
612 KB |
Output is correct |
8 |
Correct |
2 ms |
612 KB |
Output is correct |
9 |
Correct |
2 ms |
612 KB |
Output is correct |
10 |
Incorrect |
2 ms |
656 KB |
Output isn't correct |
11 |
Correct |
2 ms |
656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
656 KB |
Output is correct |
2 |
Correct |
3 ms |
656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
656 KB |
Output is correct |
2 |
Correct |
5 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1236 KB |
Output is correct |
2 |
Correct |
23 ms |
2516 KB |
Output is correct |
3 |
Correct |
11 ms |
2516 KB |
Output is correct |
4 |
Correct |
9 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
3432 KB |
Output is correct |
2 |
Correct |
36 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
10052 KB |
Output is correct |
2 |
Correct |
101 ms |
10496 KB |
Output is correct |
3 |
Correct |
96 ms |
15836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
16708 KB |
Output is correct |
2 |
Correct |
182 ms |
27220 KB |
Output is correct |
3 |
Correct |
182 ms |
27220 KB |
Output is correct |
4 |
Correct |
254 ms |
37208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
323 ms |
37208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
43728 KB |
Output is correct |
2 |
Correct |
378 ms |
43728 KB |
Output is correct |
3 |
Correct |
348 ms |
43728 KB |
Output is correct |
4 |
Correct |
622 ms |
43728 KB |
Output is correct |
5 |
Correct |
345 ms |
52828 KB |
Output is correct |
6 |
Correct |
350 ms |
52828 KB |
Output is correct |
7 |
Correct |
818 ms |
52828 KB |
Output is correct |