# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
503750 |
2022-01-08T20:45:06 Z |
tabr |
Islands (IOI08_islands) |
C++17 |
|
501 ms |
131076 KB |
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
vector<int> l(n);
vector<int> e(n);
for (int i = 0; i < n; i++) {
int j;
cin >> j >> l[i];
j--;
e[i] = j;
g[i].emplace_back(i);
g[j].emplace_back(i);
}
for (int i = 0; i < n; i++) {
if (e[e[i]] == i) {
l[e[i]] = l[i] = max(l[e[i]], l[i]);
}
}
long long ans = 0;
vector<int> was(n);
vector<int> pv(n);
vector<int> cycle(n);
vector<int> cnt2(n);
vector<long long> now2(n);
for (int i = 0; i < n; i++) {
if (was[i]) {
continue;
}
int c1 = -1, c2 = -1;
int sz = 0;
int dead = -1;
function<void(int, int)> dfs1 = [&](int v, int pe) {
was[v] = 1;
for (auto id : g[v]) {
if (id == pe) {
continue;
}
int to = v ^ id ^ e[id];
if (was[to]) {
if (c1 != -1) {
continue;
}
dead = id;
c1 = v;
c2 = to;
debug(c1, c2);
cycle[v]++;
if (pv[to] != -1) {
cycle[pv[to]]--;
}
continue;
}
pv[to] = v;
dfs1(to, id);
cycle[v] += cycle[to];
}
sz += cycle[v];
};
pv[i] = -1;
dfs1(i, -1);
vector<long long> d1(sz + 1), d2(sz + 1);
function<void(int, int)> dfs2 = [&](int v, int p) {
cnt2[v] += cycle[v];
d1[cnt2[v]] = max(d1[cnt2[v]], now2[v]);
for (auto id : g[v]) {
int to = v ^ id ^ e[id];
if (to == p || id == dead) {
continue;
}
cnt2[to] = cnt2[v];
now2[to] = now2[v] + l[id];
dfs2(to, v);
}
};
now2[c1] = cnt2[c1] = 0;
dfs2(c1, c2);
swap(d1, d2);
now2[c2] = cnt2[c2] = 0;
dfs2(c2, c1);
swap(d1, d2);
for (int j = 0; j < sz; j++) {
d1[j + 1] = max(d1[j + 1], d1[j]);
d2[j + 1] = max(d2[j + 1], d2[j]);
}
long long t = 0;
function<long long(int, int)> dfs3 = [&](int v, int p) {
vector<long long> c;
for (auto id : g[v]) {
int to = v ^ id ^ e[id];
if (to == p || id == dead) {
continue;
}
now2[to] = now2[v] + l[id];
c.emplace_back(dfs3(to, v));
}
t = max(t, now2[v]);
if (c.empty()) {
return now2[v];
}
sort(c.rbegin(), c.rend());
if (c.size() > 1) {
t = max(t, c[0] + c[1] - 2 * now2[v]);
}
return c[0];
};
now2[c1] = 0;
dfs3(c1, c2);
now2[c2] = 0;
dfs3(c2, c1);
debug(t);
debug(d1);
debug(d2);
for (int j = 1; j < sz; j++) {
t = max(t, d1[j] + d2[sz - j] + l[dead]);
}
ans += t;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
224 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1852 KB |
Output is correct |
2 |
Correct |
48 ms |
5880 KB |
Output is correct |
3 |
Correct |
17 ms |
2136 KB |
Output is correct |
4 |
Correct |
8 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
8168 KB |
Output is correct |
2 |
Correct |
59 ms |
12004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
21628 KB |
Output is correct |
2 |
Correct |
141 ms |
31872 KB |
Output is correct |
3 |
Correct |
160 ms |
48148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
37380 KB |
Output is correct |
2 |
Correct |
283 ms |
80896 KB |
Output is correct |
3 |
Correct |
323 ms |
92624 KB |
Output is correct |
4 |
Correct |
421 ms |
123400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
501 ms |
98708 KB |
Output is correct |
2 |
Runtime error |
442 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
352 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |