#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
vector<int> l(n);
vector<int> e(n);
for (int i = 0; i < n; i++) {
int j;
cin >> j >> l[i];
j--;
e[i] = j;
g[i].emplace_back(i);
g[j].emplace_back(i);
}
for (int i = 0; i < n; i++) {
if (e[e[i]] == i) {
l[e[i]] = l[i] = max(l[e[i]], l[i]);
}
}
long long ans = 0;
vector<int> was(n);
vector<int> pv(n);
vector<int> cycle(n);
for (int i = 0; i < n; i++) {
if (was[i]) {
continue;
}
int c1 = -1, c2 = -1;
int sz = 0;
int dead = -1;
pv[i] = -1;
{ // dfs1
vector<pair<int, int>> que;
que.emplace_back(i, -1);
for (int qid = 0; qid < (int) que.size(); qid++) {
auto [v, pe] = que[qid];
was[v] = 1;
for (auto id : g[v]) {
if (id == pe) {
continue;
}
int to = v ^ id ^ e[id];
if (was[to]) {
if (c1 != -1) {
continue;
}
dead = id;
c1 = v;
c2 = to;
cycle[v]++;
if (pv[to] != -1) {
cycle[pv[to]]--;
}
continue;
}
pv[to] = v;
// dfs1(to, id);
que.emplace_back(to, id);
// cycle[v] += cycle[to];
}
// sz += cycle[v];
}
for (int qid = (int) que.size() - 1; qid >= 0; qid--) {
auto [v, pe] = que[qid];
for (auto id : g[v]) {
if (id == pe) {
continue;
}
int to = v ^ id ^ e[id];
if (to == pv[v]) {
continue;
}
cycle[v] += cycle[to];
}
sz += cycle[v];
}
}
vector<long long> d1(sz + 1), d2(sz + 1);
function<void(int, int, int, long long)> dfs2 = [&](int v, int p, int cnt, long long now) {
cnt += cycle[v];
d1[cnt] = max(d1[cnt], now);
for (auto id : g[v]) {
int to = v ^ id ^ e[id];
if (to == p || id == dead) {
continue;
}
dfs2(to, v, cnt, now + l[id]);
}
};
dfs2(c1, c2, 0, 0);
swap(d1, d2);
dfs2(c2, c1, 0, 0);
swap(d1, d2);
for (int j = 0; j < sz; j++) {
d1[j + 1] = max(d1[j + 1], d1[j]);
d2[j + 1] = max(d2[j + 1], d2[j]);
}
long long t = 0;
function<long long(int, int, long long)> dfs3 = [&](int v, int p, long long now) {
vector<long long> c;
for (auto id : g[v]) {
int to = v ^ id ^ e[id];
if (to == p || id == dead) {
continue;
}
c.emplace_back(dfs3(to, v, now + l[id]));
}
t = max(t, now);
if (c.empty()) {
return now;
}
sort(c.rbegin(), c.rend());
if (c.size() > 1) {
t = max(t, c[0] + c[1] - 2 * now);
}
return c[0];
};
dfs3(c1, c2, 0);
dfs3(c2, c1, 0);
debug(t);
debug(d1);
debug(d2);
for (int j = 1; j < sz; j++) {
t = max(t, d1[j] + d2[sz - j] + l[dead]);
}
ans += t;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
6 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
7 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
8 |
Runtime error |
2 ms |
588 KB |
Execution killed with signal 6 |
9 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
844 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
1820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
7420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
109 ms |
19024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
168 ms |
32964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
94580 KB |
Output is correct |
2 |
Runtime error |
665 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
328 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |