#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define w (a[e][1])
#define V() (e == u ? a[e][0] : e)
#define v V()
const int LIM = 1e6;
vector<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM];
ll b[LIM], d[LIM], s[LIM], best;
bitset<LIM> vis;
void dfs(int u){
vis[u] = 1, s[u] = d[u];
ll curr = 0;
for(int e : g[u]) if(e != r && !vis[v]){
p[v] = u;
d[v] = d[u] + (ll)w;
dfs(v);
s[u] = max(s[u], s[v]);
best = max(best, curr + s[v] - d[u]);
curr = max(curr, s[v] - d[u]);
}
}
void findCycle(int u, int par){
vis[u] = 1;
for(int e : g[u]) if(e != par){
if(vis[v]) return void(r = e + (vis[u] = 0));
findCycle(v, e);
if(r >= 0) break;
}
vis[u] = 0;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=0; i<n; ++i){
cin >> a[i][0] >> a[i][1]; --a[i][0];
g[a[i][0]].push_back(i);
g[i].push_back(i);
}
fill(p, p+n, -1);
ll ans = 0;
for(int z=0; z<n; ++z){
if(vis[z]) continue;
r = -1, best = 0;
findCycle(z, -1);
assert(r >= 0);
dfs(r);
function<void(int, int)> find = [&](int u, int last){
b[u] = 0;
for(int e : g[u])
if(e != r && v != p[u] && v != last) b[u] = max(b[u], s[v]);
if(u != r){
find(p[u], u);
b[u] = max(b[u], b[p[u]]);
}
};
find(a[r][0], -1);
int u = a[r][0], last = -1;
ll res = 0;
while(u != r){
ll mx = 0, ex = 0;
for(int e : g[u])
if(e != r && v != p[u] && v != last) mx = max(mx, s[v] - d[u]);
res = max(res, d[a[r][0]] - d[u] + mx + b[p[u]]);
last = u;
u = p[u];
}
res += a[r][1];
ans += max(best, res);
}
cout << ans;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:75:15: warning: unused variable 'ex' [-Wunused-variable]
75 | ll mx = 0, ex = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23776 KB |
Output is correct |
2 |
Correct |
14 ms |
23776 KB |
Output is correct |
3 |
Incorrect |
14 ms |
23884 KB |
Output isn't correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Incorrect |
14 ms |
23760 KB |
Output isn't correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Incorrect |
15 ms |
23832 KB |
Output isn't correct |
8 |
Correct |
16 ms |
23844 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
14 ms |
23844 KB |
Output is correct |
11 |
Correct |
15 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23884 KB |
Output is correct |
2 |
Correct |
15 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
23972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
28712 KB |
Output is correct |
2 |
Correct |
52 ms |
31812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
38896 KB |
Output is correct |
2 |
Correct |
111 ms |
44408 KB |
Output is correct |
3 |
Correct |
150 ms |
52100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
51788 KB |
Output is correct |
2 |
Correct |
238 ms |
72840 KB |
Output is correct |
3 |
Correct |
255 ms |
83472 KB |
Output is correct |
4 |
Correct |
303 ms |
96576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
86484 KB |
Output is correct |
2 |
Correct |
938 ms |
131072 KB |
Output is correct |
3 |
Correct |
317 ms |
78492 KB |
Output is correct |
4 |
Correct |
417 ms |
114292 KB |
Output is correct |
5 |
Correct |
443 ms |
114032 KB |
Output is correct |
6 |
Correct |
1179 ms |
82904 KB |
Output is correct |
7 |
Runtime error |
458 ms |
131076 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
384 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |