#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
const int MOD = 1e9 + 7;
const int MX = 1e6 + 10;
const ll INF = 1e18;
int N; vpi adj[MX]; pi nxt[MX]; ll ans = 0;
int vis[MX]; ll label[MX]; ll cycLen[MX];
int numCyc = 0; bool comp[MX]; vi cyc[MX];
int depth[MX]; ll dist[MX]; int lift[MX][20];
int numTree = 0; vi tree[MX]; ll val[MX];
// LCA with Binary Jump ~ Get the maximum diameter
ll DFS2(int X, int P, int curTree) {
tree[curTree].pb(X);
ll maxDist = 0;
EACH(Y, adj[X]) {
if (Y.f != P && !comp[Y.f]) {
depth[Y.f] = depth[X] + 1;
dist[Y.f] = dist[X] + Y.s;
lift[Y.f][0] = X;
ckmax(maxDist, DFS2(Y.f, X, numTree) + Y.s);
}
}
adj[X].clear();
return maxDist;
}
int jump(int X, int len) {
F0R(i, 20) {
if (len & (1 << i)) {
X = lift[X][i];
}
}
return X;
}
int LCA(int A, int B) {
if (depth[A] < depth[B]) {
swap(A, B);
}
A = jump(A, depth[A] - depth[B]);
if (A == B) return A;
R0F(i, 20) {
if (lift[A][i] != lift[B][i]) {
A = lift[A][i]; B = lift[B][i];
}
}
return lift[A][0];
}
ll DIST(int A, int B) {
return dist[A] + dist[B] - 2 * dist[LCA(A, B)];
}
int furthestAway(int X, int ind) {
pl res = {-1, -1};
EACH(Y, tree[ind]) {
ckmax(res, {DIST(X, Y), Y});
}
return res.s;
}
ll getDiam(int ind) {
int ST = furthestAway(tree[ind][0], ind);
int EN = furthestAway(ST, ind);
ll res = 0; ckmax(res, DIST(ST, EN)); return res;
}
// DFS in Functional Graph ~ Generate cycles
void DFS1(int X) {
int cur = X;
while (vis[X] == 0) {
vis[X] = cur;
X = nxt[X].f;
}
ll curDist = 0; if (vis[X] == cur) numCyc++;
while (vis[X] == cur) {
vis[X] = N + cur;
label[X] = curDist;
comp[X] = true;
cyc[numCyc].pb(X);
curDist += nxt[X].s;
X = nxt[X].f;
}
if (vis[X] == N + cur) {
cycLen[numCyc] = curDist;
}
while (vis[X] == N + cur) {
vis[X] = 2 * N + cur;
val[X] = DFS2(X, 0, ++numTree);
X = nxt[X].f;
}
}
ll getMaxDist(int ind) {
ll res = 0;
EACH(X, cyc[ind]) {
ckmax(res, getDiam(++numTree));
}
// case 1: i < j, val[j] + label[j] + val[i] - label[i]
ll cumMax = -INF;
FOR(i, 1, sz(cyc[ind])) {
int A = cyc[ind][i];
int B = cyc[ind][i - 1];
ckmax(cumMax, val[B] - label[B]);
ckmax(res, val[A] + label[A] + cumMax);
}
// case 2: i < j, cyclen[ind] + val[j] - label[j] + val[i] + label[i]
cumMax = -INF;
FOR(i, 1, sz(cyc[ind])) {
int A = cyc[ind][i];
int B = cyc[ind][i - 1];
ckmax(cumMax, val[B] + label[B]);
ckmax(res, cycLen[ind] + val[A] - label[A] + cumMax);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N; FOR(i, 1, N + 1) { int A, L; cin >> A >> L; adj[i].pb({A, L}), adj[A].pb({i, L}); nxt[i] = {A, L}; }
FOR(i, 1, N + 1) {
if (!vis[i]) {
DFS1(i);
}
}
FOR(j, 1, 20) {
FOR(i, 1, N + 1) {
lift[i][j] = lift[lift[i][j - 1]][j - 1];
}
}
// FOR(i, 1, numTree + 1) {
// getDiam(i);
// }
numTree = 0;
FOR(i, 1, numCyc + 1) {
ans += getMaxDist(i);
}
cout << ans;
}
Compilation message
islands.cpp: In function 'll getMaxDist(int)':
islands.cpp:166:10: warning: unused variable 'X' [-Wunused-variable]
166 | EACH(X, cyc[ind]) {
| ^
islands.cpp:45:31: note: in definition of macro 'EACH'
45 | #define EACH(a, x) for (auto& a : x)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
70720 KB |
Output is correct |
2 |
Correct |
42 ms |
70844 KB |
Output is correct |
3 |
Correct |
42 ms |
70872 KB |
Output is correct |
4 |
Correct |
44 ms |
70700 KB |
Output is correct |
5 |
Correct |
47 ms |
70724 KB |
Output is correct |
6 |
Correct |
42 ms |
70812 KB |
Output is correct |
7 |
Correct |
44 ms |
70784 KB |
Output is correct |
8 |
Correct |
42 ms |
70752 KB |
Output is correct |
9 |
Correct |
43 ms |
70724 KB |
Output is correct |
10 |
Correct |
43 ms |
70828 KB |
Output is correct |
11 |
Correct |
43 ms |
70828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
70992 KB |
Output is correct |
2 |
Correct |
48 ms |
71116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
71172 KB |
Output is correct |
2 |
Correct |
46 ms |
71496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
73452 KB |
Output is correct |
2 |
Correct |
74 ms |
77192 KB |
Output is correct |
3 |
Correct |
59 ms |
74572 KB |
Output is correct |
4 |
Correct |
52 ms |
72580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
79944 KB |
Output is correct |
2 |
Correct |
107 ms |
89316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
105628 KB |
Output is correct |
2 |
Correct |
203 ms |
110008 KB |
Output is correct |
3 |
Correct |
233 ms |
117104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
200 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
361 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
377 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |