# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
396287 |
2021-04-29T16:44:08 Z |
Berted |
Islands (IOI08_islands) |
C++14 |
|
1806 ms |
131072 KB |
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#define ll long long
#define pii pair<int, int>
#define pip pair<int, pii>
#define fst first
#define snd second
using namespace std;
const int SZ = 1000000;
int N;
pip edge[2 * SZ + 5]; int szz = 0;
int H[SZ + 5]; vector<int> vis;
vector<pii> cyc;
ll DP[SZ + 5];
deque<pair<ll, int>> dq;
ll M[2], ans = 0;
int DFS1(int u, pii p)
{
int ret = H[u] + 1;
vis.push_back(u);
//cerr << "D1: " << u << " " << H[u] << "\n";
for (int i = lower_bound(edge, edge + szz, make_pair(u, make_pair(-1, -1))) - edge; i < szz && edge[i].fst == u; i++)
{
const pii& v = edge[i].snd;
if (p == v) {p = {-1, -1}; continue;}
if (H[v.fst] == -1)
{
H[v.fst] = H[u] + 1;
ret = DFS1(v.fst, {u, v.snd});
}
else if (H[v.fst] < H[u]) {ret = H[v.fst];}
if (ret <= H[u])
{
cyc.push_back({u, v.snd});
break;
}
}
return ret;
}
ll DFS2(int u)
{
ll ret = 0;
for (int i = lower_bound(edge, edge + szz, make_pair(u, make_pair(-1, -1))) - edge; i < szz && edge[i].fst == u; i++)
{
const pii& v = edge[i].snd;
if (H[v.fst] == -1)
{
H[v.fst] = H[u] + 1;
ret = max(ret, DFS2(v.fst));
DP[u] = max(DP[u], DP[v.fst] + v.snd);
}
}
M[0] = 0; M[1] = 0;
for (int i = lower_bound(edge, edge + szz, make_pair(u, make_pair(-1, -1))) - edge; i < szz && edge[i].fst == u; i++)
{
const pii& v = edge[i].snd;
if (H[v.fst] > H[u])
{
if (DP[v.fst] + v.snd > M[0]) {M[1] = M[0]; M[0] = DP[v.fst] + v.snd;}
else if (DP[v.fst] + v.snd > M[1]) {M[1] = DP[v.fst] + v.snd;}
}
}
return max(ret, M[0] + M[1]);
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++)
{
int u, c; cin >> u >> c; u--;
edge[szz++] = {u, {i, c}};
edge[szz++] = {i, {u, c}};
H[i] = -1;
}
sort(edge, edge + szz);
for (int k = 0; k < N; k++)
{
if (H[k] == -1)
{
ll rr = 0; H[k] = 0;
DFS1(k, {-1, -1});
for (const auto &u : vis) H[u] = -1;
vis.clear();
for (const auto &u : cyc) H[u.fst] = 0;
for (const auto &u : cyc) rr = max(rr, DFS2(u.fst));
//cerr << k << ": " << rr << " " << cyc.size() << "\n";
ll s = 0, t = 0;
for (int i = 1; i < cyc.size(); i++)
{
//cerr << "id: " << i << " " << cyc[i].fst << " - " << DP[cyc[i].fst] << "\n";
s += cyc[i].snd;
while (dq.size() && dq.back().fst <= s + DP[cyc[i].fst])
{
//cerr << "Popped\n";
dq.pop_back();
}
//cerr << "Pushed: " << s + DP[cyc[i].fst] << ", " << i << "\n";
dq.push_back({s + DP[cyc[i].fst], i});
}
s += cyc[0].snd;
for (int i = 0; i < cyc.size(); i++)
{
if (dq.front().snd == i) dq.pop_front();
//cerr << i << " " << " " << cyc[i].fst << " " << dq.front().fst << " " << t << " " << DP[cyc[i].fst] << "\n";
rr = max(rr, dq.front().fst + t + DP[cyc[i].fst]);
while (dq.size() && dq.back().fst + t <= s + DP[cyc[i].fst]) {dq.pop_back();}
dq.push_back({s + DP[cyc[i].fst] - t, i});
t -= cyc[(i + 1) % cyc.size()].snd;
}
dq.clear();
cyc.clear();
//cerr << k << ": " << rr << "\n";
ans += rr;
}
}
cout << ans << "\n";
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:105:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for (int i = 0; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1100 KB |
Output is correct |
2 |
Correct |
30 ms |
3768 KB |
Output is correct |
3 |
Correct |
18 ms |
1236 KB |
Output is correct |
4 |
Correct |
8 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
4908 KB |
Output is correct |
2 |
Correct |
56 ms |
6208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
10904 KB |
Output is correct |
2 |
Correct |
135 ms |
17792 KB |
Output is correct |
3 |
Correct |
157 ms |
29760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
15924 KB |
Output is correct |
2 |
Correct |
306 ms |
46612 KB |
Output is correct |
3 |
Correct |
352 ms |
53300 KB |
Output is correct |
4 |
Correct |
410 ms |
77100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
591 ms |
31920 KB |
Output is correct |
2 |
Correct |
891 ms |
100240 KB |
Output is correct |
3 |
Correct |
443 ms |
30168 KB |
Output is correct |
4 |
Correct |
603 ms |
77464 KB |
Output is correct |
5 |
Correct |
571 ms |
76816 KB |
Output is correct |
6 |
Correct |
1806 ms |
37364 KB |
Output is correct |
7 |
Correct |
602 ms |
116904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
413 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |