//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <bitset>
#include <stack>
#include <algorithm>
#include <chrono>
#include <random>
using namespace std;
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define MP make_pair
typedef long long ll;
const double eps = 0.000001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 1;
const int M = 222;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int f[N], w[N];
int cyc_sz;
int cycle[N];
stack<int> st;
bitset<N> used;
inline void find_cycle(int x){
while (!st.empty()) st.pop();
cyc_sz = 0;
while (true){
st.push(x);
used[x] = true;
if (used[f[x]]){
while (!st.empty()){
int y = st.top(); st.pop();
cycle[cyc_sz++] = y;
if (y == f[x]) break;
}
return;
}
x = f[x];
}
}
int parent[N];
inline int find(const int x){
return (x == parent[x] ? x : parent[x] = find(parent[x]));
}
vector<int> g[N];
inline int A(const int &u, const int &v){
if (f[u] == v && f[v] != u) return w[u];
if (f[u] != v && f[v] == u) return w[v];
return max(w[u], w[v]);
}
int X; ll D;
inline void dfs(const int v, const int p, const ll d, const int c1, const int c2, const bool flag){
if (d > D) D = d, X = v;
for (int u : g[v]){
if (u != p && u != c1 && u != c2) dfs(u, v, d + A(u, v), c1, c2, flag);
}
if (flag) g[v].clear();
}
ll arr[N];
inline ll component(const int root){
find_cycle(root);
const int n = cyc_sz;
ll ans = 0;
for (int i = 0; i < n; i++){
int v = cycle[i];
D = -1;
dfs(v, v, 0, cycle[(i + n - 1) % n], cycle[(i + 1) % n], false);
ans = max(ans, D);
arr[v] = D;
D = -1;
dfs(X, X, 0, cycle[(i + n - 1) % n], cycle[(i + 1) % n], true);
ans = max(ans, D);
}
if (n == 2){
int u = cycle[0], v = cycle[1];
ans = max(ans, arr[u] + arr[v] + A(u, v));
return ans;
}
vector<ll> a(n);
a[0] = {arr[cycle[0]]};
ll sum = 0;
for (int i = 1; i < n; i++){
sum += A(cycle[i - 1], cycle[i]);
a[i] = arr[cycle[i]] + sum;
}
sum += A(cycle[0], cycle[n - 1]);
ans = max(ans, arr[cycle[0]] + *max_element(a.begin() + 1, a.end()));
for (int i = n - 2; i >= 0; i--) a[i] = max(a[i + 1], a[i]);
ll add = 0;
int v, u;
ll mx = 0;
ll sum2 = 0;
for (int i = 1; i < n; i++){
v = cycle[i], u = cycle[i - 1];
add -= A(v, u);
mx = max(mx, arr[cycle[i - 1]] + sum2 + sum);
ans = max(ans, arr[v] + add + mx);
if (i + 1 < n) ans = max(ans, arr[v] + add + a[i + 1]);
sum2 += A(v, u);
mx = max(mx, arr[cycle[i]] + sum2);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 1; i <= n; i++){
cin >> f[i] >> w[i];
if (rng() & 1) parent[find(i)] = find(f[i]); else
parent[find(f[i])] = find(i);
if (f[f[i]] != i){
g[i].push_back(f[i]);
g[f[i]].push_back(i);
}
}
bitset<N> comp;
ll ans = 0;
for (int i = 1; i <= n; i++){
if (!comp[find(i)]){
comp[find(i)] = true;
ans += component(i);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23884 KB |
Output is correct |
2 |
Correct |
13 ms |
23928 KB |
Output is correct |
3 |
Correct |
12 ms |
23884 KB |
Output is correct |
4 |
Correct |
12 ms |
23964 KB |
Output is correct |
5 |
Correct |
13 ms |
23960 KB |
Output is correct |
6 |
Correct |
12 ms |
23884 KB |
Output is correct |
7 |
Correct |
13 ms |
23884 KB |
Output is correct |
8 |
Correct |
13 ms |
23972 KB |
Output is correct |
9 |
Correct |
13 ms |
23884 KB |
Output is correct |
10 |
Correct |
12 ms |
23884 KB |
Output is correct |
11 |
Correct |
12 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23996 KB |
Output is correct |
2 |
Correct |
12 ms |
23984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24012 KB |
Output is correct |
2 |
Correct |
13 ms |
24140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24740 KB |
Output is correct |
2 |
Correct |
26 ms |
26004 KB |
Output is correct |
3 |
Correct |
27 ms |
25052 KB |
Output is correct |
4 |
Correct |
17 ms |
24432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
26892 KB |
Output is correct |
2 |
Correct |
53 ms |
29380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
35012 KB |
Output is correct |
2 |
Correct |
89 ms |
36696 KB |
Output is correct |
3 |
Correct |
100 ms |
40328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
44848 KB |
Output is correct |
2 |
Correct |
179 ms |
55160 KB |
Output is correct |
3 |
Correct |
206 ms |
59592 KB |
Output is correct |
4 |
Correct |
224 ms |
65988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
74992 KB |
Output is correct |
2 |
Correct |
786 ms |
82240 KB |
Output is correct |
3 |
Correct |
265 ms |
65364 KB |
Output is correct |
4 |
Correct |
334 ms |
80316 KB |
Output is correct |
5 |
Correct |
355 ms |
81184 KB |
Output is correct |
6 |
Correct |
1109 ms |
67836 KB |
Output is correct |
7 |
Correct |
401 ms |
89892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
399 ms |
131072 KB |
Output is correct |
2 |
Correct |
394 ms |
127140 KB |
Output is correct |
3 |
Correct |
432 ms |
111772 KB |
Output is correct |
4 |
Correct |
314 ms |
89752 KB |
Output is correct |
5 |
Correct |
345 ms |
95384 KB |
Output is correct |
6 |
Correct |
340 ms |
87312 KB |
Output is correct |
7 |
Correct |
1233 ms |
86456 KB |
Output is correct |