#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1001000;
vector<pair<int, int>> adj[N];
int deg[N];
bool cycle[N], vis[N];
ll dp[2][N], qs[N], a[N];
ll res = 0;
void dfs(int v, int p = -1) {
dp[0][v] = dp[1][v] = 0;
for (auto& x : adj[v]) {
if (x.first == p || cycle[x.first]) continue;
dfs(x.first, v);
dp[1][v] = max(dp[1][v], dp[0][x.first] + x.second);
if (dp[1][v] > dp[0][v]) swap(dp[0][v], dp[1][v]);
}
res = max(res, dp[0][v] + dp[1][v]);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
int a, x;
cin >> a >> x;
adj[i].push_back({ a, x });
adj[a].push_back({ i, x });
deg[i]++, deg[a]++;
}
// find cycle using kahn's algorithm
{
queue<int> q;
for (int i = 1;i <= n;i++) {
cycle[i] = 1;
if (deg[i] == 1) q.push(i);
}
while (!q.empty()) {
int v = q.front();
q.pop();
cycle[v] = 0;
for (auto& x : adj[v]) {
if (--deg[x.first] == 1) q.push(x.first);
}
}
}
ll ans = 0;
// for each component find the longest path and sum up to answer
for (int i = 1;i <= n;i++) {
if (!cycle[i] || vis[i]) continue;
vector<pair<int, int>> c;
c.push_back({ 0, 0 });
int now = i, p = -1;
while (1) {
bool ch = 0;
vis[now] = 1;
for (auto& x : adj[now]) {
if (!cycle[x.first] || x.first == p) continue;
c.push_back({ now, x.second });
if (!vis[x.first]) {
p = now;
ch = 1;
now = x.first;
break;
}
}
if (!ch) {
if ((int)c.size() == 2) { // parallel edge
multiset<int> s;
for (auto& x : adj[now]) {
if (!cycle[x.first]) continue;
s.insert(x.second);
}
s.erase(s.find(c.back().second));
c.push_back({ now, *s.begin() });
}
break;
}
}
// c is list of cycle in order
res = 0;
int sz = c.size() - 1;
for (int i = 1;i <= sz;i++) {
dfs(c[i].first);
qs[i] = qs[i - 1] + c[i - 1].second;
a[i] = dp[0][c[i].first];
}
ll mx = -1e18, mx2 = -1e18;
ll sk = c[sz].second;
for (int i = 1;i <= sz;i++) {
res = max(res, mx + qs[i] + a[i]);
res = max(res, mx2 + qs[sz] - qs[i] + a[i] + sk);
mx = max(mx, -qs[i] + a[i]);
mx2 = max(mx2, qs[i] + a[i]);
}
ans += res;
//cout << res << ' ';
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23896 KB |
Output is correct |
3 |
Correct |
10 ms |
23828 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23900 KB |
Output is correct |
6 |
Correct |
9 ms |
23900 KB |
Output is correct |
7 |
Correct |
10 ms |
24012 KB |
Output is correct |
8 |
Correct |
9 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23896 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
24156 KB |
Output is correct |
2 |
Correct |
11 ms |
24372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24972 KB |
Output is correct |
2 |
Correct |
20 ms |
26820 KB |
Output is correct |
3 |
Correct |
16 ms |
25460 KB |
Output is correct |
4 |
Correct |
16 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28120 KB |
Output is correct |
2 |
Correct |
36 ms |
31356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
38908 KB |
Output is correct |
2 |
Correct |
56 ms |
41168 KB |
Output is correct |
3 |
Correct |
66 ms |
46132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
52608 KB |
Output is correct |
2 |
Correct |
119 ms |
64664 KB |
Output is correct |
3 |
Correct |
121 ms |
69364 KB |
Output is correct |
4 |
Correct |
162 ms |
79960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
103216 KB |
Output is correct |
2 |
Correct |
526 ms |
109664 KB |
Output is correct |
3 |
Correct |
197 ms |
82756 KB |
Output is correct |
4 |
Correct |
227 ms |
102280 KB |
Output is correct |
5 |
Correct |
212 ms |
103096 KB |
Output is correct |
6 |
Correct |
679 ms |
95604 KB |
Output is correct |
7 |
Correct |
250 ms |
112248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
250 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |