# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
397079 |
2021-05-01T09:51:52 Z |
Berted |
Islands (IOI08_islands) |
C++14 |
|
1034 ms |
105708 KB |
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <bitset>
#include <fstream>
#define ll long long
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;
int N, deg[1000001];
vector<pii> adj[1000001];
int d1 = -1;
vector<pii> cyc;
ll DP[1000001], D2[1000001];
deque<pair<ll, int>> dq;
ll tmp[2], ans = 0;
int main()
{
//freopen("islands.in", "r", stdin);
//ifstream fin("islands.in");
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--;
adj[u].push_back({i, c}); deg[u]++;
adj[i].push_back({u, c}); deg[i]++;
}
vector<int> S;
for (int i = 0; i < N; i++)
{
if (deg[i] == 1) S.push_back(i);
}
while (S.size())
{
int u = S.back(); S.pop_back();
deg[u] = 0; tmp[0] = tmp[1] = 0;
for (const auto &v : adj[u])
{
if (deg[v.fst])
{
deg[v.fst]--;
if (deg[v.fst] < 2) S.push_back(v.fst);
}
else
{
DP[u] = max(DP[u], DP[v.fst] + v.snd);
D2[u] = max(D2[u], D2[v.fst]);
if (DP[v.fst] + v.snd > tmp[0]) {tmp[1] = tmp[0]; tmp[0] = DP[v.fst] + v.snd;}
else if (DP[v.fst] + v.snd > tmp[1]) {tmp[1] = DP[v.fst] + v.snd;}
}
}
D2[u] = max(D2[u], tmp[0] + tmp[1]);
//cerr << u << " " << DP[u] << " " << D2[u] << "\n";
}
for (int k = 0; k < N; k++)
{
if (deg[k])
{
int u = k; ll rr = 0;
while (u != -1)
{
//cerr << "Cur: " << u << "\n";
tmp[0] = tmp[1] = 0;
pii nex = {-1, -1};
for (const auto &v : adj[u])
{
if (cyc.size() && v == cyc.back()) continue;
if (deg[v.fst]) {nex = v;}
else
{
DP[u] = max(DP[u], DP[v.fst] + v.snd);
D2[u] = max(D2[u], D2[v.fst]);
if (DP[v.fst] + v.snd > tmp[0]) {tmp[1] = tmp[0]; tmp[0] = DP[v.fst] + v.snd;}
else if (DP[v.fst] + v.snd > tmp[1]) {tmp[1] = DP[v.fst] + v.snd;}
}
}
D2[u] = max(D2[u], tmp[0] + tmp[1]); rr = max(rr, D2[u]);
if (cyc.size() && cyc.front().fst == nex.fst) {nex.fst = -1;}
else if (nex.fst == -1)
{
for (const auto &v : adj[u])
if (v == cyc.back()) {nex = {-1, v.snd};}
}
//cerr << "Pushed: " << u << ", " << nex.snd << "\n";
cyc.push_back({u, nex.snd});
u = nex.fst;
}
//cerr << k << ": " << rr << " " << cyc.size() << "\n";
ll s = 0, t = 0;
for (int i = 1; i < cyc.size(); i++)
{
swap(cyc[0].snd, cyc[i].snd);
s += cyc[i].snd;
while (dq.size() && dq.back().fst <= s + DP[cyc[i].fst]) {dq.pop_back();}
dq.push_back({s + DP[cyc[i].fst], i});
}
s += cyc[0].snd;
for (int i = 0; i < cyc.size(); i++)
{
deg[cyc[i].fst] = 0;
if (dq.front().snd == i) dq.pop_front();
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();
ans += rr;
}
}
cout << ans << "\n";
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:103: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]
103 | for (int i = 1; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
islands.cpp:112: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]
112 | for (int i = 0; i < cyc.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23816 KB |
Output is correct |
2 |
Correct |
14 ms |
23756 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23816 KB |
Output is correct |
5 |
Correct |
14 ms |
23820 KB |
Output is correct |
6 |
Correct |
15 ms |
23812 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
13 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23796 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23824 KB |
Output is correct |
2 |
Correct |
15 ms |
23832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23856 KB |
Output is correct |
2 |
Correct |
16 ms |
24140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
24896 KB |
Output is correct |
2 |
Correct |
31 ms |
26564 KB |
Output is correct |
3 |
Correct |
23 ms |
25360 KB |
Output is correct |
4 |
Correct |
21 ms |
24456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
27788 KB |
Output is correct |
2 |
Correct |
51 ms |
30684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
37436 KB |
Output is correct |
2 |
Correct |
94 ms |
37564 KB |
Output is correct |
3 |
Correct |
118 ms |
43012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
47516 KB |
Output is correct |
2 |
Correct |
197 ms |
58308 KB |
Output is correct |
3 |
Correct |
204 ms |
55256 KB |
Output is correct |
4 |
Correct |
265 ms |
66672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
84464 KB |
Output is correct |
2 |
Correct |
683 ms |
85556 KB |
Output is correct |
3 |
Correct |
288 ms |
72584 KB |
Output is correct |
4 |
Correct |
369 ms |
88752 KB |
Output is correct |
5 |
Correct |
378 ms |
90168 KB |
Output is correct |
6 |
Correct |
1006 ms |
82012 KB |
Output is correct |
7 |
Correct |
408 ms |
105708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
75972 KB |
Output is correct |
2 |
Correct |
362 ms |
75968 KB |
Output is correct |
3 |
Correct |
393 ms |
76180 KB |
Output is correct |
4 |
Correct |
400 ms |
81744 KB |
Output is correct |
5 |
Correct |
392 ms |
99328 KB |
Output is correct |
6 |
Correct |
359 ms |
94092 KB |
Output is correct |
7 |
Correct |
1034 ms |
95160 KB |
Output is correct |