#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef pair <ll, ll> pii;
const int N = 1e6 + 7;
ll n, ro, sp, res;
ll a[N], b[N], dp[N][2][2];
int bio[N];
vector <pii> adj[N];
void Merge(pii& x, ll y) {
if (y >= x.F) x = {y, x.F};
else x = {x.F, max(x.S, y)};
}
void dfs(int x) {
pii p1 = {0, 0}, p2 = {0, 0};
for (auto y : adj[x]) {
if (y.F == ro) continue;
dfs(y.F);
Merge(p1, dp[y.F][1][0] + y.S);
Merge(p2, dp[y.F][1][1] + y.S);
}
if (x != sp) {
if (x != ro) dp[x][0][0] = p1.F + p1.S;
else dp[x][0][1] = p2.F + p2.S;
dp[x][1][0] = p1.F;
dp[x][1][1] = p2.F;
}
else {
ll alt = dp[ro][1][1] + b[ro];
dp[x][0][0] = max(p1.F + p1.S, alt + p1.F);
dp[x][1][0] = max(alt, p1.F);
dp[x][1][1] = p1.F;
}
res = max(res, max(dp[x][0][0], dp[x][0][1]));
}
int main () {
FIO;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
adj[a[i]].pb({i, b[i]});
}
ll sum = 0;
for (int i = 1; i <= n; i++) {
if (bio[i]) continue;
int x = a[i], y = i;
bio[i] = i;
while (!bio[x]) {
bio[x] = i;
y = x;
x = a[x];
}
if (bio[x] == i) {
ro = y;
sp = x;
res = 0;
dfs(ro);
sum += res;
}
}
cout << sum;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Incorrect |
8 ms |
31372 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
29276 KB |
Output isn't correct |
4 |
Correct |
7 ms |
31324 KB |
Output is correct |
5 |
Incorrect |
6 ms |
29276 KB |
Output isn't correct |
6 |
Correct |
7 ms |
29316 KB |
Output is correct |
7 |
Incorrect |
9 ms |
29276 KB |
Output isn't correct |
8 |
Incorrect |
6 ms |
31380 KB |
Output isn't correct |
9 |
Incorrect |
6 ms |
29276 KB |
Output isn't correct |
10 |
Correct |
7 ms |
31324 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
31320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
31320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
32092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
37420 KB |
Output is correct |
2 |
Incorrect |
26 ms |
38980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
51024 KB |
Output is correct |
2 |
Correct |
54 ms |
56964 KB |
Output is correct |
3 |
Incorrect |
69 ms |
67580 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
67960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
106044 KB |
Output is correct |
2 |
Runtime error |
384 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
223 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |