#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];
int depth[maxn];
long long dp[maxn];
bool flag[maxn];
bool is_cycle[maxn];
int cycle[maxn];
int lens[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]) {
depth[v] = depth[u] + 1;
par[v] = id;
dfs1(v);
}
else {
if (depth[u] > depth[v]) {
int cur = u;
while (cur != v) {
cycle[sz] = cur;
lens[sz] = len[par[cur]];
sz++;
cur = par[cur] ^ to[par[cur]] ^ cur;
}
cycle[sz] = v;
lens[sz] = len[id];
sz++;
}
}
}
}
void dfs2(int u, int p) {
dp[u] = 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]);
}
}
}
long long res;
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 += lens[i];
}
long long dist = 0;
long long mx1 = calc_max(cycle[0]);
long long mx2 = mx1;
for (int i = 1; i < sz; i++) {
dist += lens[i - 1];
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 = to[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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Runtime error |
127 ms |
107452 KB |
Execution killed with signal 11 |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
134 ms |
107568 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
129 ms |
107804 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
4636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
225 ms |
110740 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
110 ms |
19836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
307 ms |
32528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
536 ms |
131072 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |