#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define int ll
using namespace std;
const int N = 1e6+1;
int n;
vector<pair<int, ll>> adj[N];
map<pii, int> edge;
map<pii, ll> we;
bool vis[N], vis2[N];
vector<int> stck;
vector<int> cyc;
int sz = 0;
void dfs(int node, int par=0) {
sz++;
vis[node] = 1;
stck.push_back(node);
for (auto [ch, cost] : adj[node]) {
if (!cyc.size() && edge[{node, ch}] == 2) {
cyc.push_back(node);
cyc.push_back(ch);
}
if (vis[ch] && ch != par) {
if (!cyc.size()) {
bool st = 0;
for (int cur : stck) {
if (cur == ch) st = 1;
if (st) cyc.push_back(cur);
}
}
}
else if (!vis[ch])
dfs(ch, node);
}
stck.pop_back();
}
pii remedge;
int mx[N];
ll mxdep[N];
void farthest_dp(int node, int par=0) {
mx[node] = node;
mxdep[node] = 0;
for (auto [ch, cost] : adj[node]) {
if (vis2[ch] || ch == par) continue;
if (make_pair(node, ch) != remedge && make_pair(ch, node) != remedge) {
farthest_dp(ch, node);
if (mxdep[ch] + cost > mxdep[node]) {
mxdep[node] = mxdep[ch] + cost;
mx[node] = mx[ch];
}
}
}
}
ll solve_for(int node)
{
if (vis[node]) return 0;
sz = 0;
cyc.clear();
dfs(node);
const ll L = cyc.size();
// cerr << "#" << node << ": ";
// cerr << L << ": ";
// for(int i:cyc)cerr<<i<<' ';
// cerr<<"| ";
// remove an edge from the cycle and find diameter
if (L > 2)
remedge = {cyc[0], cyc[1]};
else
remedge = {-1, -1};
farthest_dp(node);
int fr = mx[node];
farthest_dp(fr);
ll diam = mxdep[fr];
// a[i] = max depth of tree going from i not in cycle direction
// prefix sums
ll a[L] = {};
memset(vis2, 0, sizeof vis2);
for (int i = 0; i < L; i++) {
vis2[cyc[(i+1)%L]] = 1;
vis2[cyc[(i-1+L)%L]] = 1;
farthest_dp(cyc[i]);
a[i] = mxdep[cyc[i]];
vis2[cyc[(i+1)%L]] = 0;
vis2[cyc[(i-1+L)%L]] = 0;
}
// for (int i = 0; i < L; i++)
// cerr << a[i] << ' ';
// cerr << "| ";
// cost of path(i,j) = a[i] + a[j] + (p[i] - p[j]);
// maintain maximum a[j] - p[j], and the answer for suffix i:
//
ll prvmx = 0;
ll sol = 0;
ll pref[L] = {};
pref[0] = 0;
for (int i = 1; i < L; i++)
pref[i] = pref[i-1] + we[{cyc[i], cyc[i-1]}];
const ll LL = pref[L-1];
if (L == 2) {
sol = *max_element(a, a+L) + we[{cyc[0], cyc[1]}];
goto RET;
}
prvmx = a[0];
for (int i = 1; i < L; i++) {
sol = max(sol, a[i] + pref[i] + prvmx);
prvmx = max(prvmx, a[i] - pref[i]);
}
prvmx = a[0];
for (int i = 1; i < L; i++) {
sol = max(sol, LL + a[i] - pref[i] + prvmx);
prvmx = max(prvmx, a[i] + pref[i]);
}
RET:
return max(diam, sol);
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int u;
ll w;
cin >> u >> w;
adj[i].push_back({u, w});
adj[u].push_back({i, w});
edge[{i,u}]++, edge[{u,i}]++;
we[{i, u}] = we[{u, i}] = max(we[{i, u}], w);
}
ll sum = 0;
for (int i = 1; i <= n; i++) {
ll x = solve_for(i);
// if (x) cerr << x << '\n';
sum += x;
}
cout << sum;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
24920 KB |
Output is correct |
2 |
Correct |
11 ms |
24924 KB |
Output is correct |
3 |
Incorrect |
11 ms |
24924 KB |
Output isn't correct |
4 |
Correct |
10 ms |
24924 KB |
Output is correct |
5 |
Correct |
11 ms |
24924 KB |
Output is correct |
6 |
Correct |
11 ms |
24920 KB |
Output is correct |
7 |
Incorrect |
11 ms |
24924 KB |
Output isn't correct |
8 |
Incorrect |
11 ms |
24924 KB |
Output isn't correct |
9 |
Incorrect |
12 ms |
24920 KB |
Output isn't correct |
10 |
Correct |
11 ms |
24904 KB |
Output is correct |
11 |
Correct |
12 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
25176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
25588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
30288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
45392 KB |
Output is correct |
2 |
Incorrect |
109 ms |
61440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
94724 KB |
Output is correct |
2 |
Correct |
323 ms |
113724 KB |
Output is correct |
3 |
Runtime error |
279 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
459 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
327 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |