#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<pair<int,int> > adj[1000005];
int trip[1000005], w[1000005];
bool chk[1000005];
vector<int> cycle,len;
vector<ll> d;
int vn;
int dfs(int now, int pi) {
int ret = trip[now] = ++vn;
for (auto &e : adj[now]) {
if (e.second==pi)continue;
if (!trip[e.first]) {
ret = min(ret, dfs(e.first, e.second));
}
else if(!chk[e.first]){
ret=min(ret,trip[e.first]);
cycle.push_back(e.first);
chk[e.first] = true;
len.push_back(w[e.second]);
}
}
if (ret < trip[now]) {
chk[now] = true;
cycle.push_back(now);
len.push_back(w[pi]);
}
return ret;
}
ll val;
ll dfs2(int now) {
chk[now] = true;
ll ret=0;
for (auto &e : adj[now]) {
if (chk[e.first])continue;
ll temp=dfs2(e.first)+w[e.second];
val = max(val, ret + temp);
ret=max(ret,temp);
}
return ret;
}
ll calc(vector<ll> &d, vector<int> &len, int m) {
deque<int> dq;
vector<ll> a(m * 2);
ll sum = len[0];
for (int i = 1; i < m * 2; i++) {
a[i] = sum + d[i%m];
sum += len[i%m];
}
ll ret = 0;
sum = 0;
int ri;
ri = 0;
for (int i = 0; i < m; i++) {
for (int j = ri; j < i + m; j++) {
while (!dq.empty() && a[dq.back()] <= a[j])dq.pop_back();
dq.push_back(j);
}
ri = i + m;
while (!dq.empty() && dq.front() <= i)dq.pop_front();
ll temp = d[i] + a[dq.front()] - sum;
ret = max(ret, temp);
sum += len[i];
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a, b;
scanf("%d%d", &a, &b);
adj[i].emplace_back(a,i);
adj[a].emplace_back(i,i);
w[i] = b;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (!trip[i]) {
cycle.clear();
len.clear();
dfs(i, 0);
int m = cycle.size();
d = vector<ll>(m);
val = 0;
for (int i = 0; i < cycle.size(); i++) {
d[i] = dfs2(cycle[i]);
}
val = max(val,calc(d, len, m));
reverse(len.begin(), len.end() - 1);
reverse(d.begin(), d.end());
val = max(val, calc(d, len, m));
ans += val;
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:88:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < cycle.size(); i++) {
~~^~~~~~~~~~~~~~
islands.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:74:8: 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 |
28 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23952 KB |
Output is correct |
3 |
Correct |
25 ms |
24096 KB |
Output is correct |
4 |
Correct |
22 ms |
24096 KB |
Output is correct |
5 |
Correct |
21 ms |
24096 KB |
Output is correct |
6 |
Correct |
21 ms |
24096 KB |
Output is correct |
7 |
Correct |
23 ms |
24096 KB |
Output is correct |
8 |
Correct |
21 ms |
24096 KB |
Output is correct |
9 |
Correct |
21 ms |
24096 KB |
Output is correct |
10 |
Correct |
24 ms |
24096 KB |
Output is correct |
11 |
Correct |
21 ms |
24096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24292 KB |
Output is correct |
2 |
Correct |
22 ms |
24292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24292 KB |
Output is correct |
2 |
Correct |
23 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
24940 KB |
Output is correct |
2 |
Correct |
43 ms |
27252 KB |
Output is correct |
3 |
Correct |
34 ms |
27252 KB |
Output is correct |
4 |
Correct |
28 ms |
27252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
28412 KB |
Output is correct |
2 |
Correct |
69 ms |
30480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
35324 KB |
Output is correct |
2 |
Correct |
144 ms |
41112 KB |
Output is correct |
3 |
Correct |
160 ms |
49932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
49932 KB |
Output is correct |
2 |
Correct |
292 ms |
66324 KB |
Output is correct |
3 |
Correct |
323 ms |
74872 KB |
Output is correct |
4 |
Correct |
369 ms |
90156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
90156 KB |
Output is correct |
2 |
Correct |
1155 ms |
117648 KB |
Output is correct |
3 |
Correct |
402 ms |
117648 KB |
Output is correct |
4 |
Correct |
573 ms |
117648 KB |
Output is correct |
5 |
Correct |
524 ms |
117648 KB |
Output is correct |
6 |
Correct |
1473 ms |
117648 KB |
Output is correct |
7 |
Correct |
692 ms |
126500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
558 ms |
126500 KB |
Output is correct |
2 |
Correct |
580 ms |
126500 KB |
Output is correct |
3 |
Correct |
641 ms |
158540 KB |
Output is correct |
4 |
Correct |
763 ms |
158540 KB |
Output is correct |
5 |
Correct |
526 ms |
158540 KB |
Output is correct |
6 |
Correct |
523 ms |
158540 KB |
Output is correct |
7 |
Correct |
1412 ms |
158540 KB |
Output is correct |