#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
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 |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
412 KB |
Output is correct |
5 |
Correct |
3 ms |
540 KB |
Output is correct |
6 |
Correct |
2 ms |
540 KB |
Output is correct |
7 |
Correct |
3 ms |
540 KB |
Output is correct |
8 |
Correct |
2 ms |
540 KB |
Output is correct |
9 |
Correct |
3 ms |
540 KB |
Output is correct |
10 |
Incorrect |
3 ms |
648 KB |
Output isn't correct |
11 |
Correct |
2 ms |
648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
648 KB |
Output is correct |
2 |
Correct |
3 ms |
648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
680 KB |
Output is correct |
2 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1256 KB |
Output is correct |
2 |
Correct |
18 ms |
2540 KB |
Output is correct |
3 |
Correct |
10 ms |
2540 KB |
Output is correct |
4 |
Correct |
7 ms |
2540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3416 KB |
Output is correct |
2 |
Correct |
36 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
10184 KB |
Output is correct |
2 |
Correct |
97 ms |
10412 KB |
Output is correct |
3 |
Correct |
98 ms |
15928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
16624 KB |
Output is correct |
2 |
Correct |
168 ms |
27216 KB |
Output is correct |
3 |
Correct |
214 ms |
27216 KB |
Output is correct |
4 |
Correct |
234 ms |
37228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
328 ms |
37228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
585 ms |
43640 KB |
Output is correct |
2 |
Correct |
341 ms |
43652 KB |
Output is correct |
3 |
Correct |
361 ms |
43652 KB |
Output is correct |
4 |
Correct |
397 ms |
43652 KB |
Output is correct |
5 |
Correct |
473 ms |
52720 KB |
Output is correct |
6 |
Correct |
396 ms |
52720 KB |
Output is correct |
7 |
Correct |
877 ms |
52720 KB |
Output is correct |