#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
#include <stack>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
const int N = 1e6 + 7;
const ll INF = 1e18 + 7;
vector <ii> g[N];
bool inc[N];
int pw[N];
int n;
void bfs() {
queue <int> q;
for (int i = 1; i <= n; ++i) {
pw[i] = g[i].size();
if (pw[i] == 1) q.push(i);
inc[i] = 1;
}
while (q.size()) {
int u = q.front(); q.pop();
inc[u] = 0;
for (auto e : g[u]) {
int v = e.f;
--pw[v];
if (pw[v] == 1) q.push(v);
}
}
}
int to[N], len[N];
ll h[N], diam[N];
void dfs1(int u, int p) {
for (auto e : g[u]) {
int v = e.f;
if (v == p || inc[v]) continue;
dfs1(v, u);
ckmax(diam[u], diam[v]);
ckmax(diam[u], h[u] + h[v] + e.s);
ckmax(h[u], h[v] + e.s);
}
ckmax(diam[u], h[u]);
}
ll pref[N];
int r[N];
int cl[N];
int s[N];
ll solve(int n) {
ll ans = 0;
for (int i = 0; i < n; ++i)
ans = max(ans, diam[cl[i]]);
for (int i = 1; i < n; ++i) pref[i] = pref[i - 1] + len[cl[i - 1]];
int sz = 0;
for (int i = 0; i < n; ++i) r[i] = n;
for (int i = 0; i < n; ++i) {
while (sz && h[cl[s[sz - 1]]] - pref[s[sz - 1]] <= h[cl[i]] - pref[i]) {
r[s[sz - 1]] = i;
sz--;
}
s[sz++] = i;
}
int ptr = 0; ll cur = 0;
auto get = [&] (int i) {
if (i < n)
return cl[i];
else
return cl[i - n];
};
for (int i = 1; i < 2 * n - 1; ++i) {
cur += len[get(i - 1)];
ptr = max(ptr, i - n + 1);
while (r[ptr] != n && r[ptr] < i) ptr = r[ptr];
ans = max(ans, h[get(i)] + cur + h[get(ptr)] - pref[ptr]);
}
return ans;
}
bool used[N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> to[i] >> len[i];
g[i].app(mp(to[i], len[i])); g[to[i]].app(mp(i, len[i]));
}
bfs();
for (int i = 1; i <= n; ++i) {
if (inc[i]) {
dfs1(i, i);
}
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
if (!used[i] && inc[i]) {
int u = i;
int len = 0;
while (!used[u]) {
used[u] = 1;
cl[len++] = u;
u = to[u];
}
ans += solve(len);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23908 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23840 KB |
Output is correct |
5 |
Correct |
14 ms |
23868 KB |
Output is correct |
6 |
Correct |
13 ms |
23864 KB |
Output is correct |
7 |
Correct |
14 ms |
23888 KB |
Output is correct |
8 |
Correct |
14 ms |
23888 KB |
Output is correct |
9 |
Correct |
16 ms |
23872 KB |
Output is correct |
10 |
Correct |
16 ms |
23840 KB |
Output is correct |
11 |
Correct |
14 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23928 KB |
Output is correct |
2 |
Correct |
16 ms |
23888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
24012 KB |
Output is correct |
2 |
Correct |
15 ms |
24192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24908 KB |
Output is correct |
2 |
Correct |
29 ms |
26616 KB |
Output is correct |
3 |
Correct |
24 ms |
25312 KB |
Output is correct |
4 |
Correct |
19 ms |
24524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
27652 KB |
Output is correct |
2 |
Correct |
49 ms |
30424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
37912 KB |
Output is correct |
2 |
Correct |
90 ms |
37188 KB |
Output is correct |
3 |
Correct |
119 ms |
43648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
49168 KB |
Output is correct |
2 |
Correct |
199 ms |
61376 KB |
Output is correct |
3 |
Correct |
195 ms |
57068 KB |
Output is correct |
4 |
Correct |
245 ms |
73968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
79852 KB |
Output is correct |
2 |
Correct |
680 ms |
91904 KB |
Output is correct |
3 |
Correct |
320 ms |
77036 KB |
Output is correct |
4 |
Correct |
384 ms |
96564 KB |
Output is correct |
5 |
Correct |
373 ms |
97940 KB |
Output is correct |
6 |
Correct |
1071 ms |
88256 KB |
Output is correct |
7 |
Correct |
391 ms |
102604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
384 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |