#include "bits/stdc++.h"
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
const int nax = 1e6;
int n;
int p[nax+5];
bool vis[nax+5];
long long c[nax+5];
long long val[nax+5];
vector<pii> adj[nax+5];
vector<int> cyc[nax+5];
vector<pii> edg;
void gp(int u, int par, int ls=0) {
p[u] = par;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e == ls) continue;
if(p[v] == 0) {
gp(v, par, e);
} else {
cyc[par].push_back(e);
}
}
}
bool fc(int u, int go, int ls) {
if(u == go) return true;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(e == ls) continue;
bool ok = fc(v, go, e);
if(ok) {
vis[u] = true;
vis[v] = true;
if(edg[e].fi != u) swap(edg[e].fi, edg[e].se);
cyc[p[go]].push_back(e);
return true;
}
}
return false;
}
long long trav(int u) {
if(val[u] != -1) return val[u];
vis[u] = true;
long long res = 0;
for(pii t : adj[u]) {
int v = t.fi;
int e = t.se;
if(!vis[v]) {
res = max(res, c[e] + trav(v));
}
}
return val[u] = res;
}
int main () {
scanf("%d", &n);
edg.pb({0,0});
for(int i=1; i<=n; i++) {
long long l, j;
scanf("%lld %lld", &j, &l);
c[i] = l;
adj[j].pb({i,i});
adj[i].pb({j,i});
edg.pb({i,j});
val[i] = -1;
}
long long ans = 0;
for(int i=1; i<=n; i++) {
if(p[i] == 0) {
gp(i,i);
cyc[i].pop_back();
fc(edg[cyc[i][0]].se, edg[cyc[i][0]].fi, cyc[i][0]);
long long opt = 0;
//priority_queue<long long> pq;
deque<pair<long long, int>> dq;
long long dpq = 0;
long long sum = 0;
int sz = cyc[i].size();
for(int j=0; j<sz; j++) {
int e = cyc[i][j];
swap(edg[e].fi, edg[e].se);
sum += c[e];
if(j+1 < sz) {
long long temp = sum + trav(edg[e].se);
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp,i});
}
}
for(int j=0; j<sz; j++) {
int e = cyc[i][j];
int u = edg[e].fi;
opt = max(opt, trav(u) + dq.front().fi + dpq);
dpq -= c[e];
long long temp = trav(u) + sum - c[e] - dpq;
while(!dq.empty() && dq.front().se == j) dq.pop_front();
while(!dq.empty() && dq.back().fi <= temp) dq.pop_back();
dq.push_back({temp,j});
//cerr << temp+dpq << endl;
}
sum = 0;
dpq = 0;
dq.clear();
// for(int j=sz-1; j>=0; j--) {
// int e = cyc[i][j];
// sum += c[e];
// if(j > 0) pq.push(sum + trav(edg[e].fi));
// }
// for(int j=sz-1; j>=0; j--) {
// int e = cyc[i][j];
// int u = edg[e].se;
// opt = max(opt, trav(u) + pq.top() + dpq);
// dpq -= c[e];
// pq.push(trav(u) + sum - c[e] - dpq);
// }
ans += opt;
}
}
printf("%lld", ans);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%lld %lld", &j, &l);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47188 KB |
Output is correct |
2 |
Incorrect |
33 ms |
47228 KB |
Output isn't correct |
3 |
Correct |
26 ms |
47256 KB |
Output is correct |
4 |
Correct |
24 ms |
47268 KB |
Output is correct |
5 |
Correct |
23 ms |
47236 KB |
Output is correct |
6 |
Correct |
24 ms |
47188 KB |
Output is correct |
7 |
Correct |
24 ms |
47256 KB |
Output is correct |
8 |
Correct |
28 ms |
47176 KB |
Output is correct |
9 |
Correct |
25 ms |
47268 KB |
Output is correct |
10 |
Incorrect |
27 ms |
47216 KB |
Output isn't correct |
11 |
Correct |
25 ms |
47188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47360 KB |
Output is correct |
2 |
Correct |
24 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47316 KB |
Output is correct |
2 |
Correct |
26 ms |
47640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
48464 KB |
Output is correct |
2 |
Incorrect |
49 ms |
51180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
52688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
106 ms |
62520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
75368 KB |
Output is correct |
2 |
Incorrect |
287 ms |
101088 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
355 ms |
114960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
371 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |