#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef TEST
#define getchar_unlocked _getchar_nolock
#endif
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ll ans, ed, lpath[1000005], ldpath[1000005];
int N, st, rn, he[1000005], ign[1000005], le[1000005];
bitset<1000005> oncyc, vis;
vector<int> comp, path[1000005];
vector<ll> wt[1000005];
vector<tuple<int, ll, int> > adj[1000005];
void dfs(int n, int e = -1) {
he[n] = rn;
vis[n] = 1;
for (auto u : adj[n]) {
if (g2(u) != e) {
if (vis[g0(u)]) {
st = g0(u);
ed = n;
ign[rn] = g2(u);
le[rn] = g1(u);
} else dfs(g0(u), g2(u));
}
}
}
bool find_path(int n, int e = -1) {
if (n == ed) {
path[he[n]].pb(n);
oncyc[n] = 1;
return 1;
}
for (auto u : adj[n])
if (g2(u) != ign[he[n]] && g2(u) != e && find_path(g0(u), g2(u))) {
path[he[n]].pb(n);
wt[he[n]].pb(g1(u));
oncyc[n] = 1;
return 1;
}
return 0;
}
void find_diam(int n, int e = -1, int d = 0) {
if (d > ed) {
ed = d;
st = n;
}
for (auto u : adj[n])
if ((!oncyc[g0(u)] || g0(u) == rn) && g2(u) != e)
find_diam(g0(u), g2(u), d + g1(u));
}
main() {
oncyc.reset();
vis.reset();
//ios::sync_with_stdio(0);
//cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> rn >> ans;
adj[i].eb(rn, ans, i);
adj[rn].eb(i, ans, i);
}
ans = 0;
for (int i = 1; i <= N; i++) {
if (vis[i]) continue;
comp.pb(i);
rn = i;
dfs(i);
wt[i].pb(0);
find_path(st);
}
for (int i = 1; i <= N; i++) {
if (oncyc[i]) {
rn = i;
ed = st = 0;
find_diam(i);
ldpath[i] = ed;
ed = 0;
find_diam(st);
lpath[he[i]] = max(lpath[he[i]], ed);
}
}
ll total;
for (int i : comp) {
total = le[i];
wt[i].pb(0);
for (int j = 0; j < wt[i].size() - 1; j++)
wt[i][j + 1] += wt[i][j];
total += wt[i].back();
ll fs = ldpath[path[i][0]] - wt[i][0], snd = ldpath[path[i][0]] + wt[i][0];
for (int j = 1; j < path[i].size(); j++) {
int curr = path[i][j];
lpath[i] = max(lpath[i], fs + ldpath[curr] + wt[i][j]);
lpath[i] = max(lpath[i], snd + ldpath[curr] - wt[i][j] + total);
fs = max(fs, ldpath[curr] - wt[i][j]);
snd = max(snd, ldpath[curr] + wt[i][j]);
}
ans += lpath[i];
}
cout << ans << '\n';
}
Compilation message
islands.cpp:76:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
76 | main() {
| ^
islands.cpp: In function 'int main()':
islands.cpp:111:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int j = 0; j < wt[i].size() - 1; j++)
| ~~^~~~~~~~~~~~~~~~~~
islands.cpp:115:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for (int j = 1; j < path[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
71020 KB |
Output is correct |
2 |
Correct |
43 ms |
71020 KB |
Output is correct |
3 |
Correct |
45 ms |
71148 KB |
Output is correct |
4 |
Correct |
45 ms |
71020 KB |
Output is correct |
5 |
Correct |
44 ms |
71020 KB |
Output is correct |
6 |
Correct |
45 ms |
71020 KB |
Output is correct |
7 |
Correct |
51 ms |
71168 KB |
Output is correct |
8 |
Correct |
45 ms |
71148 KB |
Output is correct |
9 |
Correct |
43 ms |
71020 KB |
Output is correct |
10 |
Correct |
45 ms |
71020 KB |
Output is correct |
11 |
Correct |
47 ms |
71148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
71148 KB |
Output is correct |
2 |
Correct |
45 ms |
71244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
71404 KB |
Output is correct |
2 |
Correct |
53 ms |
71660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
72812 KB |
Output is correct |
2 |
Correct |
94 ms |
76140 KB |
Output is correct |
3 |
Incorrect |
68 ms |
73068 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
77800 KB |
Output is correct |
2 |
Correct |
131 ms |
82732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
91120 KB |
Output is correct |
2 |
Correct |
235 ms |
99676 KB |
Output is correct |
3 |
Correct |
298 ms |
110932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
407 ms |
106476 KB |
Output is correct |
2 |
Runtime error |
468 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
913 ms |
131072 KB |
Output is correct |
2 |
Runtime error |
949 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
851 ms |
131076 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |