#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
int n, weg[N];
vector<pii> g[N];
char bio[N];
vector<pii> cyc;
int dfs(int u, int p) {
bio[u] = 1;
for(pii e : g[u]) {
int v = e.X, w = e.Y;
if(w != p) {
if(bio[v]) {
cyc.PB({e.X, weg[e.Y]});
return v;
} else {
int res = dfs(v, w);
if(res) {
cyc.PB({e.X, weg[e.Y]});
return res != u ? res : 0;
}
}
}
}
bio[u] = 0;
return 0;
}
void mark(int u) {
bio[u] = 2;
for(pii e : g[u]) {
if(bio[e.X] != 2) {
mark(e.X);
}
}
}
ll dep[N];
ll depth(int u, int p) {
for(pii e : g[u]) {
int v = e.X, w = e.Y;
if(w != p && !bio[v]) {
dep[u] = max(dep[u], (ll) weg[w] + depth(v, w));
}
}
return dep[u];
}
ll in[N];
multiset<ll> s;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
int u;
scanf("%d%d", &u, weg + i);
g[i].PB({u, i});
g[u].PB({i, i});
}
ll ans = 0;
for(int i = 1; i <= n; ++i) {
if(bio[i]) { continue; }
cyc.clear();
s.clear();
dfs(i, 0);
for(pii e : cyc) {
depth(e.X, 0);
debug("%d(%d, %lld)\n", e.X, e.Y, dep[e.X]);
}
mark(i);
ll prf = 0;
for(pii e : cyc) {
int v = e.X, w = e.Y;
s.insert(dep[v] + prf);
prf += w;
}
ll prf_ = 0, res = 0, len = prf;
for(pii e : cyc) {
int v = e.X, w = e.Y;
s.erase(dep[v] + prf - len);
res = max(res, *prev(s.end()) + dep[v] - prf + len);
s.insert(dep[v] + prf);
prf += w;
}
ans += res;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:100:6: warning: unused variable 'prf_' [-Wunused-variable]
100 | ll prf_ = 0, res = 0, len = prf;
| ^~~~
islands.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d", &u, weg + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
26972 KB |
Output is correct |
2 |
Incorrect |
5 ms |
27084 KB |
Output isn't correct |
3 |
Correct |
4 ms |
26972 KB |
Output is correct |
4 |
Correct |
3 ms |
27064 KB |
Output is correct |
5 |
Correct |
3 ms |
26972 KB |
Output is correct |
6 |
Correct |
4 ms |
27076 KB |
Output is correct |
7 |
Correct |
3 ms |
26972 KB |
Output is correct |
8 |
Correct |
3 ms |
26972 KB |
Output is correct |
9 |
Correct |
3 ms |
26972 KB |
Output is correct |
10 |
Incorrect |
3 ms |
27100 KB |
Output isn't correct |
11 |
Correct |
4 ms |
26972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28252 KB |
Output is correct |
2 |
Correct |
33 ms |
31276 KB |
Output is correct |
3 |
Correct |
10 ms |
27996 KB |
Output is correct |
4 |
Incorrect |
6 ms |
27368 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
32812 KB |
Output is correct |
2 |
Correct |
85 ms |
35008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
39252 KB |
Output is correct |
2 |
Incorrect |
218 ms |
48084 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
49744 KB |
Output is correct |
2 |
Correct |
407 ms |
83368 KB |
Output is correct |
3 |
Incorrect |
803 ms |
92372 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
151 ms |
68172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
193 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |