#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<unsigned, unsigned>>> g;
pair<bool, unsigned> find_cycle(unsigned u, vector<unsigned> &c, vector<bool> &vis, unsigned p = -1)
{
if (vis[u])
return {1, u};
vis[u] = 1;
for (auto const &[v, w] : g[u])
if (v != p)
{
auto const [b, x] = find_cycle(v, c, vis, u);
if (b)
{
c.push_back(u);
return {u != x, x};
}
}
return {0, UINT_MAX};
}
// Returns (longest path inside subtree, longest path to root)
pair<uint64_t, uint64_t> longest_tree_path(unsigned u, unsigned p = -1)
{
pair<uint64_t, uint64_t> ans = {0, 0};
for (auto const &[v, w] : g[u])
{
if (v != p)
{
auto const [sl, rl] = longest_tree_path(v, u);
ans.first = max(ans.first, max(sl, ans.second + rl + w));
ans.second = max(ans.second, rl + w);
}
}
return ans;
}
uint64_t longest_simple_path(unsigned u, vector<bool> &cvis)
{
vector<unsigned> c;
find_cycle(u, c, cvis);
if (c.empty())
return longest_tree_path(u).first;
size_t const m = c.size();
vector<uint64_t> root_paths(c.size());
uint64_t ans = 0;
vector<unsigned> pre_dis(c.size()), next_dis(c.size());
for (size_t i = 0; i < m; i++)
{
auto it = lower_bound(g[c[i]].begin(), g[c[i]].end(), make_pair(c[(i + 1) % m], UINT_MAX), greater<pair<unsigned, unsigned>>());
next_dis[i] = it->second;
g[c[i]].erase(it);
auto jt = lower_bound(g[c[i]].begin(), g[c[i]].end(), make_pair(c[(i - 1 + m) % m], UINT_MAX), greater<pair<unsigned, unsigned>>());
pre_dis[i] = jt->second;
g[c[i]].erase(jt);
}
for (size_t i = 0; i < m; i++)
{
auto const [sl, rl] = longest_tree_path(c[i]);
ans = max(ans, sl);
root_paths[i] = rl;
}
int64_t dp = 0, prefix_sum = 0;
for (size_t i = 0; i < m; i++)
{
ans = max(ans, root_paths[i] + prefix_sum + dp);
dp = max(dp, (int64_t)root_paths[i] - (int64_t)prefix_sum);
prefix_sum += next_dis[i];
}
dp = 0, prefix_sum = 0;
for (size_t i = m; i; i--)
{
ans = max(ans, root_paths[i % m] + prefix_sum + dp);
dp = max(dp, (int64_t)root_paths[i % m] - (int64_t)prefix_sum);
prefix_sum += pre_dis[i % m];
}
return ans;
}
void mark_component(unsigned u, vector<bool> &vis)
{
vis[u] = 1;
for (auto const &[v, w] : g[u])
if (!vis[v])
mark_component(v, vis);
}
bool edge_cmp(pair<unsigned, unsigned> const &a, pair<unsigned, unsigned> const &b)
{
return a.first == b.first;
}
int main()
{
size_t n;
cin >> n;
g = vector<vector<pair<unsigned, unsigned>>>(n);
for (size_t i = 0; i < n; i++)
{
unsigned u, w;
cin >> u >> w;
g[i].emplace_back(u - 1, w);
g[u - 1].emplace_back(i, w);
}
for (unsigned i = 0; i < n; i++)
{
sort(g[i].begin(), g[i].end(), greater<pair<unsigned, unsigned>>());
g[i].resize(unique(g[i].begin(), g[i].end(), edge_cmp) - g[i].begin());
}
vector<bool> vis(n, 0), cvis(n, 0);
uint64_t max_distance = 0;
for (unsigned i = 0; i < n; i++)
{
if (!vis[i])
{
mark_component(i, vis);
max_distance += longest_simple_path(i, cvis);
}
}
cout << max_distance << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
68 ms |
8928 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
14028 KB |
Output is correct |
2 |
Correct |
152 ms |
21752 KB |
Output is correct |
3 |
Incorrect |
202 ms |
31692 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
303 ms |
25260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
697 ms |
63100 KB |
Output is correct |
2 |
Incorrect |
1119 ms |
102852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
791 ms |
117448 KB |
Output is correct |
2 |
Correct |
780 ms |
87880 KB |
Output is correct |
3 |
Runtime error |
773 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |