#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
#define int long long
struct root {
int u, len;
long long max_depth;
root(): u(0), len(0), max_depth(0) {};
root(int _u, int _len): u(_u), len(_len), max_depth(0) {};
};
const int maxn = 1e6 + 20;
vector<pair<int, int>> adj[maxn];
pair<int, int> par[maxn];
int len[maxn];
int depth[maxn];
pair<long long, int> max_depth[maxn];
bool flag[maxn];
bool is_cycle[maxn];
vector<root> cycle;
void dfs1(int u) {
flag[u] = true;
for (auto e: adj[u]) {
int v = e.first;
int id = e.second;
if (id == par[u].second) {
continue;
}
if (!flag[v]) {
depth[v] = depth[u] + 1;
par[v] = make_pair(u, id);
dfs1(v);
}
else {
if (depth[u] > depth[v]) {
int cur = u;
while (cur != v) {
cycle.emplace_back(cur, len[par[cur].second]);
cur = par[cur].first;
}
cycle.emplace_back(v, len[id]);
}
}
}
}
void dfs2(int u, int p) {
max_depth[u] = make_pair(0, u);
for (auto e: adj[u]) {
int v = e.first;
int id = e.second;
if (v != p && !is_cycle[v]) {
dfs2(v, u);
max_depth[u] = max(max_depth[u], make_pair(max_depth[v].first + len[id], max_depth[v].second));
}
}
}
long long solve() {
int n = cycle.size();
for (int i = 0; i < n; i++) {
is_cycle[cycle[i].u] = true;
}
long long res = 0;
for (int i = 0; i < n; i++) {
int u = cycle[i].u;
is_cycle[u] = false;
dfs2(u, -1);
cycle[i].max_depth = max_depth[u].first;
int v = max_depth[u].second;
dfs2(v, -1);
res = max(res, max_depth[v].first);
is_cycle[u] = true;
}
vector<long long> dist(n);
long long mx = cycle[0].max_depth;
for (int i = 1; i < n; i++) {
dist[i] = dist[i - 1] + cycle[i - 1].len;
res = max(res, dist[i] + cycle[i].max_depth + mx);
mx = max(mx, cycle[i].max_depth - dist[i]);
}
long long sum = dist[n - 1] + cycle[n - 1].len;
mx = cycle[0].max_depth;
for (int i = 1; i < n; i++) {
res = max(res, (sum - dist[i]) + cycle[i].max_depth + mx);
mx = max(mx, cycle[i].max_depth + dist[i]);
}
return res;
}
void just_do_it() {
int n;
cin >> n;
for (int u = 1; u <= n; u++) {
int v;
cin >> v >> len[u];
adj[u].emplace_back(v, u);
adj[v].emplace_back(u, u);
}
long long res = 0;
for (int u = 1; u <= n; u++) {
if (!flag[u]) {
cycle.clear();
par[u] = make_pair(-1, -1);
dfs1(u);
res += solve();
}
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23868 KB |
Output is correct |
3 |
Correct |
15 ms |
23948 KB |
Output is correct |
4 |
Correct |
14 ms |
23812 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
15 ms |
23812 KB |
Output is correct |
7 |
Correct |
16 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23736 KB |
Output is correct |
9 |
Correct |
15 ms |
23856 KB |
Output is correct |
10 |
Correct |
15 ms |
23808 KB |
Output is correct |
11 |
Correct |
18 ms |
23748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24020 KB |
Output is correct |
2 |
Correct |
14 ms |
24020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24080 KB |
Output is correct |
2 |
Correct |
15 ms |
24616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
25840 KB |
Output is correct |
2 |
Correct |
44 ms |
29800 KB |
Output is correct |
3 |
Correct |
32 ms |
26164 KB |
Output is correct |
4 |
Correct |
24 ms |
24980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
31776 KB |
Output is correct |
2 |
Correct |
62 ms |
36412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
46636 KB |
Output is correct |
2 |
Correct |
101 ms |
56688 KB |
Output is correct |
3 |
Correct |
141 ms |
70644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
65852 KB |
Output is correct |
2 |
Correct |
221 ms |
102724 KB |
Output is correct |
3 |
Correct |
231 ms |
118204 KB |
Output is correct |
4 |
Runtime error |
228 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
118852 KB |
Output is correct |
2 |
Runtime error |
552 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
424 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |