/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
#define order curr
using namespace std;
typedef long long ll;
const int N_MAX = 1000000;
int N;
struct Edge {
int to;
int len;
};
vector <Edge> adj[N_MAX + 2];
Edge par[N_MAX + 2];
int depth[N_MAX + 2];
tuple <int, int, int> backEdge;
int curr[N_MAX + 2];
void dfs (int u) {
depth[u] = 0;
while (u != 0) {
if (curr[u] < (int) adj[u].size()) {
Edge e = adj[u][curr[u]++];
if (depth[e.to] == -1) {
par[e.to] = Edge{u, e.len};
depth[e.to] = depth[u] + 1;
u = e.to;
} else if (depth[e.to] > depth[u]) {
backEdge = make_tuple(u, e.to, e.len);
}
} else {
u = par[u].to;
}
}
}
bitset <N_MAX + 2> root;
ll maxAny[N_MAX + 2];
ll maxDown[N_MAX + 2];
queue <int> q;
//int order[N_MAX + 2];
void compute (int s) {
q.push(s);
depth[s] = 0;
int cnt = 0;
while (q.empty() == false) {
int u = q.front(); q.pop();
order[++cnt] = u;
for (Edge e : adj[u]) {
if (depth[e.to] == -1 && root[e.to] == false) {
q.push(e.to);
depth[e.to] = depth[u] + 1;
}
}
}
for (int i = cnt; i >= 1; i--) {
int u = order[i];
ll maxDown1 = 0, maxDown2 = 0;
for (Edge e : adj[u]) {
if (depth[u] < depth[e.to] && root[e.to] == false) {
maxAny[u] = max(maxAny[u], maxAny[e.to]);
maxDown[u] = max(maxDown[u], maxDown[e.to] + e.len);
ll here = maxDown[e.to] + e.len;
if (here >= maxDown1) {
maxDown2 = maxDown1;
maxDown1 = here;
} else if (here > maxDown2) {
maxDown2 = here;
}
}
}
maxAny[u] = max(maxAny[u], maxDown1 + maxDown2);
}
}
int roots[N_MAX + 2];
int group[N_MAX + 2];
int cntRoots;
int cntGroups;
multiset <ll> s;
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int u = 1; u <= N; u++) {
Edge e;
cin >> e.to >> e.len;
assert(u != e.to);
adj[u].push_back(Edge{e.to, e.len});
adj[e.to].push_back(Edge{u, e.len});
}
fill(depth + 1, depth + N + 1, -1);
for (int u = 1; u <= N; u++) {
if (depth[u] == -1) {
dfs(u);
int v1, v2, len; tie(v1, v2, len) = backEdge;
swap(v1, v2);
par[v2] = Edge{v1, len};
cntGroups++;
int v = v1;
while (v != v2) {
root[v] = true;
roots[++cntRoots] = v;
group[cntRoots] = cntGroups;
v = par[v].to;
}
root[v] = true;
roots[++cntRoots] = v;
group[cntRoots] = cntGroups;
}
}
fill(depth + 1, depth + N + 1, -1);
for (int u = 1; u <= N; u++) {
if (root[u] == true) {
compute(u);
}
}
ll answer = 0;
for (int g = 1, i = 1; g <= cntGroups; g++) {
int j = i;
while (j < cntRoots && group[j + 1] == g) {
j++;
}
ll totalLen = 0;
ll maxLen = 0;
for (int k = i; k <= j; k++) {
int u = roots[k];
totalLen += par[u].len;
maxLen = max(maxLen, maxAny[u]);
}
ll len = 0;
for (int k = j; k > i; k--) {
int u = roots[k];
len += par[u].len;
s.insert(maxDown[u] + len);
}
ll lazy = 0;
for (int k = i, u = roots[i]; k <= j; k++) {
assert(s.empty() == false);
maxLen = max(maxLen, *s.rbegin() + lazy + maxDown[u]);
s.insert(maxDown[u] - lazy);
lazy += par[u].len;
u = par[u].to;
assert(s.find((maxDown[u] + totalLen) - lazy) != s.end());
s.erase(s.find((maxDown[u] + totalLen) - lazy));
}
answer += maxLen;
s.clear();
i = j + 1;
}
cout << answer << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
13 ms |
23768 KB |
Output is correct |
5 |
Correct |
13 ms |
23752 KB |
Output is correct |
6 |
Correct |
13 ms |
23796 KB |
Output is correct |
7 |
Correct |
12 ms |
23752 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
14 ms |
23768 KB |
Output is correct |
10 |
Correct |
15 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23884 KB |
Output is correct |
2 |
Correct |
13 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23984 KB |
Output is correct |
2 |
Correct |
17 ms |
24188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
24916 KB |
Output is correct |
2 |
Correct |
34 ms |
27296 KB |
Output is correct |
3 |
Correct |
22 ms |
25300 KB |
Output is correct |
4 |
Correct |
18 ms |
24552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
28364 KB |
Output is correct |
2 |
Correct |
62 ms |
31672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
38584 KB |
Output is correct |
2 |
Correct |
158 ms |
42112 KB |
Output is correct |
3 |
Correct |
171 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
52200 KB |
Output is correct |
2 |
Correct |
253 ms |
69424 KB |
Output is correct |
3 |
Correct |
430 ms |
73800 KB |
Output is correct |
4 |
Correct |
449 ms |
88920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
90492 KB |
Output is correct |
2 |
Correct |
893 ms |
116112 KB |
Output is correct |
3 |
Correct |
335 ms |
81228 KB |
Output is correct |
4 |
Correct |
487 ms |
109608 KB |
Output is correct |
5 |
Correct |
499 ms |
110312 KB |
Output is correct |
6 |
Correct |
1084 ms |
89204 KB |
Output is correct |
7 |
Correct |
750 ms |
125268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
327 ms |
86348 KB |
Output is correct |
2 |
Correct |
344 ms |
86348 KB |
Output is correct |
3 |
Runtime error |
552 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |