#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 1e6 + 20;
int to[maxn];
pair<int, int> edges[maxn * 2];
int adj_pt[maxn];
int par[maxn];
int len[maxn];
long long dp[maxn];
bool flag[maxn];
bool is_cycle[maxn];
int cycle[maxn];
bool head[maxn];
int sz;
void dfs1(int u) {
flag[u] = true;
for (int pt = adj_pt[u]; edges[pt].first == u; pt++) {
int id = edges[pt].second;
if (id == par[u]) {
continue;
}
int v = id ^ to[id] ^ u;
if (!flag[v]) {
par[v] = id;
dfs1(v);
}
else {
int cur = u;
while (cur != v) {
if (par[cur] == -1) {
break;
}
cur = par[cur] ^ to[par[cur]] ^ cur;
}
if (cur == v) {
cur = u;
while (cur != v) {
cycle[sz] = cur;
head[sz] = (par[cur] == cur);
sz++;
cur = par[cur] ^ to[par[cur]] ^ cur;
}
cycle[sz] = v;
head[sz] = (id == v);
sz++;
}
}
}
}
long long res;
void dfs2(int u, int p) {
dp[u] = 0;
long long mx1 = 0;
long long mx2 = 0;
for (int pt = adj_pt[u]; edges[pt].first == u; pt++) {
int id = edges[pt].second;
int v = id ^ to[id] ^ u;
if (v != p && !is_cycle[v]) {
dfs2(v, u);
dp[u] = max(dp[u], dp[v] + len[id]);
if (dp[v] + len[id] > mx1) {
mx2 = mx1;
mx1 = dp[v] + len[id];
}
else if (dp[v] + len[id] > mx2) {
mx2 = dp[v] + len[id];
}
}
}
res = max(res, mx1 + mx2);
}
long long calc_max(int u) {
is_cycle[u] = false;
dfs2(u, -1);
is_cycle[u] = true;
return dp[u];
}
long long solve() {
res = 0;
long long sum = 0;
for (int i = 0; i < sz; i++) {
is_cycle[cycle[i]] = true;
sum += (head[i] ? len[cycle[i]] : len[cycle[(i + 1) % sz]]);
}
long long dist = 0;
long long mx1 = calc_max(cycle[0]);
long long mx2 = mx1;
for (int i = 1; i < sz; i++) {
dist += (head[i - 1] ? len[cycle[i - 1]] : len[cycle[i]]);
long long max_depth = calc_max(cycle[i]);
res = max(res, dist + max_depth + mx1);
res = max(res, sum - dist + max_depth + mx2);
mx1 = max(mx1, max_depth - dist);
mx2 = max(mx2, max_depth + dist);
}
return res;
}
void just_do_it() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> to[i] >> len[i];
edges[i * 2 - 1].first = i;
edges[i * 2 - 1].second = i;
edges[i * 2].first = to[i];
edges[i * 2].second = i;
}
sort(edges + 1, edges + n * 2 + 1);
for (int i = 1; i <= n * 2; i++) {
if (edges[i].first != edges[i - 1].first) {
adj_pt[edges[i].first] = i;
}
}
long long res = 0;
for (int u = 1; u <= n; u++) {
if (!flag[u]) {
sz = 0;
par[u] = -1;
dfs1(u);
res += solve();
}
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1108 KB |
Output is correct |
2 |
Correct |
16 ms |
2772 KB |
Output is correct |
3 |
Correct |
9 ms |
1236 KB |
Output is correct |
4 |
Correct |
6 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3796 KB |
Output is correct |
2 |
Correct |
26 ms |
5588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
9868 KB |
Output is correct |
2 |
Correct |
78 ms |
14732 KB |
Output is correct |
3 |
Correct |
93 ms |
20700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
17804 KB |
Output is correct |
2 |
Correct |
146 ms |
34416 KB |
Output is correct |
3 |
Correct |
220 ms |
42444 KB |
Output is correct |
4 |
Correct |
203 ms |
52924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
321 ms |
40440 KB |
Output is correct |
2 |
Correct |
524 ms |
74664 KB |
Output is correct |
3 |
Correct |
204 ms |
34124 KB |
Output is correct |
4 |
Correct |
304 ms |
61632 KB |
Output is correct |
5 |
Correct |
258 ms |
61548 KB |
Output is correct |
6 |
Correct |
848 ms |
39904 KB |
Output is correct |
7 |
Correct |
314 ms |
81140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
327 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |