#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;
ll dd[1000005];
vector<ll> d;
int r[1000005];
int vn;
void dfs(int now, int pi) {
r[now] = trip[now] = ++vn;
for (auto &e : adj[now]) {
if (e.second==pi)continue;
if (!trip[e.first]) {
dfs(e.first, e.second);
r[now] = min(r[now], r[e.first]);
}
else if(!chk[e.first]){
r[now] = min(r[now], trip[e.first]);
cycle.push_back(e.first);
chk[e.first] = true;
len.push_back(w[e.second]);
}
}
if (r[now] < trip[now]) {
chk[now] = true;
cycle.push_back(now);
len.push_back(w[pi]);
}
}
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:90:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < cycle.size(); i++) {
~~^~~~~~~~~~~~~~
islands.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:76: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 |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
24036 KB |
Output is correct |
3 |
Correct |
21 ms |
24036 KB |
Output is correct |
4 |
Correct |
21 ms |
24036 KB |
Output is correct |
5 |
Correct |
21 ms |
24036 KB |
Output is correct |
6 |
Correct |
21 ms |
24036 KB |
Output is correct |
7 |
Correct |
23 ms |
24036 KB |
Output is correct |
8 |
Correct |
21 ms |
24036 KB |
Output is correct |
9 |
Correct |
21 ms |
24036 KB |
Output is correct |
10 |
Correct |
25 ms |
24036 KB |
Output is correct |
11 |
Correct |
22 ms |
24052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24096 KB |
Output is correct |
2 |
Correct |
21 ms |
24096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
24096 KB |
Output is correct |
2 |
Correct |
24 ms |
24352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
24992 KB |
Output is correct |
2 |
Correct |
44 ms |
27168 KB |
Output is correct |
3 |
Correct |
36 ms |
27168 KB |
Output is correct |
4 |
Correct |
28 ms |
27168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
28348 KB |
Output is correct |
2 |
Correct |
70 ms |
30592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
35688 KB |
Output is correct |
2 |
Correct |
133 ms |
40764 KB |
Output is correct |
3 |
Correct |
163 ms |
48612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
48612 KB |
Output is correct |
2 |
Correct |
281 ms |
65188 KB |
Output is correct |
3 |
Correct |
333 ms |
73084 KB |
Output is correct |
4 |
Correct |
388 ms |
86600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
86600 KB |
Output is correct |
2 |
Correct |
1137 ms |
113756 KB |
Output is correct |
3 |
Correct |
413 ms |
113756 KB |
Output is correct |
4 |
Correct |
530 ms |
113756 KB |
Output is correct |
5 |
Correct |
524 ms |
113756 KB |
Output is correct |
6 |
Correct |
1430 ms |
113756 KB |
Output is correct |
7 |
Correct |
567 ms |
121004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
553 ms |
121004 KB |
Output is correct |
2 |
Correct |
567 ms |
121004 KB |
Output is correct |
3 |
Correct |
620 ms |
146712 KB |
Output is correct |
4 |
Correct |
664 ms |
146712 KB |
Output is correct |
5 |
Correct |
514 ms |
146712 KB |
Output is correct |
6 |
Correct |
534 ms |
146712 KB |
Output is correct |
7 |
Correct |
1480 ms |
146712 KB |
Output is correct |