#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_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
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;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
const int N = 1e6 + 7;
const ll INF = 1e18 + 7;
//vector <ii> tree[N];
bool inc[N];
int in[N];
int n;
int to[N], len[N];
struct Queue {
int qu[N];
int l, r; // r - number of added, l - number of used
void clear() {
l = r = 0;
}
int size() {
return r - l;
}
int pop() {
assert(size());
l++;
return qu[l - 1];
}
void push(int x) {
qu[r] = x;
r++;
}
} q;
ll h[N], diam[N];
void bfs() {
for (int i = 1; i <= n; ++i) {
if (in[i] == 0) q.push(i);
inc[i] = 1;
}
while (q.size()) {
int u = q.pop();
inc[u] = 0;
int v = to[u];
assert(in[v] > 0);
in[v]--;
//tree[v].app(mp(u, len[u]));
ckmax(diam[u], h[u]);
ckmax(diam[v], diam[u]);
ckmax(diam[v], h[v] + h[u] + len[u]);
ckmax(h[v], h[u] + len[u]);
if (in[v] == 0) {
q.push(v);
}
}
FORN (u, n) {
ckmax(diam[u], h[u]);
//debug(u);
//debug(diam[u]);
//debug(h[u]);
}
}
/*
void dfs1(int u, int p) {
for (auto e : tree[u]) {
int v = e.x;
dfs1(v, u);
ckmax(diam[u], diam[v]);
ckmax(diam[u], h[u] + h[v] + e.y);
ckmax(h[u], h[v] + e.y);
}
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];
in[to[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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
844 KB |
Output is correct |
2 |
Correct |
12 ms |
1652 KB |
Output is correct |
3 |
Correct |
8 ms |
1084 KB |
Output is correct |
4 |
Correct |
3 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
2252 KB |
Output is correct |
2 |
Correct |
23 ms |
3392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
7276 KB |
Output is correct |
2 |
Correct |
46 ms |
6516 KB |
Output is correct |
3 |
Correct |
62 ms |
10948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
11980 KB |
Output is correct |
2 |
Correct |
114 ms |
19324 KB |
Output is correct |
3 |
Correct |
120 ms |
14812 KB |
Output is correct |
4 |
Correct |
155 ms |
27488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
13016 KB |
Output is correct |
2 |
Correct |
271 ms |
30812 KB |
Output is correct |
3 |
Correct |
178 ms |
24872 KB |
Output is correct |
4 |
Correct |
228 ms |
37024 KB |
Output is correct |
5 |
Correct |
238 ms |
36880 KB |
Output is correct |
6 |
Correct |
438 ms |
32224 KB |
Output is correct |
7 |
Correct |
242 ms |
42732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
32620 KB |
Output is correct |
2 |
Correct |
243 ms |
48016 KB |
Output is correct |
3 |
Correct |
250 ms |
49068 KB |
Output is correct |
4 |
Correct |
245 ms |
28980 KB |
Output is correct |
5 |
Correct |
238 ms |
49860 KB |
Output is correct |
6 |
Correct |
242 ms |
47032 KB |
Output is correct |
7 |
Correct |
395 ms |
47924 KB |
Output is correct |