#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vll vector<ll>
#define PB push_back
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
const ll INF = (1 << 58);
const int MAXN = 1e6 + 7;
bool bio[MAXN];
int to[MAXN], w[MAXN];
vector<int> g[MAXN];
ll ans, ret;
vll dp, s;
int ciklus(int u){
if(bio[u]) return u;
bio[u] = 1;
int v = ciklus(to[u]);
if(v){
dp.PB(u);
s.PB(w[u]);
if(u == v) return 0;
return v;
}
bio[u] = 0;
return 0;
}
ll dfs(int u){
ll mx = 0;
bio[u] = 1;
for(int &x : g[u]){
if(bio[x]) continue;
ll ch = w[x] + dfs(x);
ret = max(ret, (ll)ch + (ll)mx);
mx = max(mx, ch);
}
return mx;
}
void resi(int u){
dp.clear(), s.clear();
ciklus(u);
int sz = s.size();
reverse(all(dp));
reverse(all(s));
for(ll &x : dp) x = (ll)dfs(x);
for(int i = sz - 1; i; i--) swap(s[i], s[i - 1]);
FOR(i, 1, sz) s[i] += s[i - 1];
ll m1 = -INF, m2 = -INF;
REP(i, sz){
ret = max(ret, (ll)dp[i] + (ll)s[i] + (ll)m1);
ret = max(ret, (ll)s[sz - 1] + (ll)m2 + (ll)dp[i] - (ll)s[i]);
m1 = max(m1, (ll)dp[i] - (ll)s[i]);
m2 = max(m2, (ll)dp[i] + (ll)s[i]);
}
}
void solve(){
int n;
cin >> n;
FOR(i, 1, n + 1) cin >> to[i] >> w[i], g[to[i]].PB(i);
FOR(i, 1, n + 1) if(!bio[i]) ret = 0, resi(i), ans += ret;
cout << ans << "\n";
}
int main(){
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
//cout.tie(NULL);
int t=1;
//cin >> t;
while(t--)solve();
return 0;
}
Compilation message
islands.cpp:12:19: warning: left shift count >= width of type [-Wshift-count-overflow]
12 | const ll INF = (1 << 58);
| ~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Incorrect |
13 ms |
23788 KB |
Output isn't correct |
4 |
Incorrect |
13 ms |
23772 KB |
Output isn't correct |
5 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
6 |
Incorrect |
13 ms |
23764 KB |
Output isn't correct |
7 |
Incorrect |
13 ms |
23856 KB |
Output isn't correct |
8 |
Incorrect |
12 ms |
23728 KB |
Output isn't correct |
9 |
Incorrect |
15 ms |
23764 KB |
Output isn't correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Incorrect |
14 ms |
23764 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
16 ms |
23824 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
23812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
24404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
26736 KB |
Output is correct |
2 |
Correct |
79 ms |
28308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
30704 KB |
Output is correct |
2 |
Correct |
146 ms |
38684 KB |
Output is correct |
3 |
Correct |
206 ms |
43368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
37092 KB |
Output is correct |
2 |
Correct |
315 ms |
51816 KB |
Output is correct |
3 |
Correct |
351 ms |
67948 KB |
Output is correct |
4 |
Correct |
470 ms |
72376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
659 ms |
36512 KB |
Output is correct |
2 |
Correct |
943 ms |
88132 KB |
Output is correct |
3 |
Correct |
566 ms |
47012 KB |
Output is correct |
4 |
Correct |
692 ms |
75184 KB |
Output is correct |
5 |
Correct |
650 ms |
73104 KB |
Output is correct |
6 |
Correct |
1000 ms |
51444 KB |
Output is correct |
7 |
Correct |
737 ms |
100496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
801 ms |
131072 KB |
Output is correct |
2 |
Correct |
731 ms |
95236 KB |
Output is correct |
3 |
Runtime error |
728 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |