#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
struct dsu {
vector<int> p;
vector<int> sz;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
sz.assign(n, 1);
}
inline int get(int x) {
if (p[x] == x) {
return x;
} else {
return p[x] = get(p[x]);
}
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) {
return false;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
p[x] = y;
sz[y] += sz[x];
return true;
}
inline bool same(int x, int y) {
return (get(x) == get(y));
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> l(n);
vector<int> nxt(n);
vector<int> deg(n);
dsu uf(n);
for (int i = 0; i < n; i++) {
int j;
cin >> j >> l[i];
j--;
uf.unite(i, j);
nxt[i] = j;
deg[i]++;
deg[j]++;
}
vector<vector<int>> vs(n);
for (int i = 0; i < n; i++) {
vs[uf.get(i)].emplace_back(i);
}
long long ans = 0;
vector<long long> mx(n);
vector<int> cycle(n);
vector<int> was(n);
for (int i = 0; i < n; i++) {
if (i != uf.get(i)) {
continue;
}
int c = i;
while (!was[c]) {
was[c] = 1;
c = nxt[c];
}
cycle[c] = 1;
int u = nxt[c];
while (u != c) {
cycle[u] = 1;
u = nxt[u];
}
queue<int> que;
for (int j : vs[i]) {
if (deg[j] == 1) {
que.emplace(j);
}
}
long long t = 0;
while (!que.empty()) {
int v = que.front();
que.pop();
int to = nxt[v];
t = max(t, mx[to] + mx[v] + l[v]);
mx[to] = max(mx[to], mx[v] + l[v]);
if (!cycle[to]) {
que.emplace(to);
}
}
vector<int> x;
x.emplace_back(c);
u = nxt[c];
while (u != c) {
x.emplace_back(u);
u = nxt[u];
}
deque<pair<int, long long>> deq;
deq.emplace_back(0, 0);
long long y = l[c];
int sz = (int) x.size();
for (int jj = 1; jj < sz * 2; jj++) {
int j = jj % sz;
if (x[deq.front().first] == x[j]) {
deq.pop_front();
}
t = max(t, y + mx[x[j]] - deq.front().second);
long long z = y - mx[x[j]];
while (!deq.empty() && deq.back().second >= z) {
deq.pop_back();
}
y += l[x[j]];
deq.emplace_back(j, z);
}
ans += t;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 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 |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1228 KB |
Output is correct |
2 |
Correct |
9 ms |
2692 KB |
Output is correct |
3 |
Correct |
14 ms |
1612 KB |
Output is correct |
4 |
Incorrect |
5 ms |
972 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3632 KB |
Output is correct |
2 |
Correct |
24 ms |
6944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
13148 KB |
Output is correct |
2 |
Correct |
48 ms |
16452 KB |
Output is correct |
3 |
Correct |
65 ms |
19028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
25932 KB |
Output is correct |
2 |
Correct |
107 ms |
33852 KB |
Output is correct |
3 |
Correct |
119 ms |
43160 KB |
Output is correct |
4 |
Correct |
153 ms |
48180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
66964 KB |
Output is correct |
2 |
Correct |
260 ms |
76892 KB |
Output is correct |
3 |
Correct |
172 ms |
51140 KB |
Output is correct |
4 |
Correct |
220 ms |
68464 KB |
Output is correct |
5 |
Correct |
231 ms |
69732 KB |
Output is correct |
6 |
Execution timed out |
2076 ms |
61764 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
63032 KB |
Output is correct |
2 |
Incorrect |
222 ms |
63008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |