# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
397070 |
2021-05-01T09:30:34 Z |
Berted |
Islands (IOI08_islands) |
C++14 |
|
677 ms |
90192 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();
//cerr << "Relaxing: " << u << "\n";
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 (deg[v.fst] && (!cyc.size() || cyc.back().fst != v.fst)) {nex = {v.fst, v.snd};}
else if (!deg[v.fst])
{
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 (nex.fst == -1 || (cyc.size() && nex.fst == cyc.front().fst))
{
for (const auto &v : adj[u])
{
if (cyc.front().fst == v.fst) {nex = {-1, v.snd}; break;}
}
}
//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++)
{
//cerr << "id: " << i << " " << cyc[i].fst << " - " << DP[cyc[i].fst] << "\n";
swap(cyc[i - 1].snd, cyc[i].snd);
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++)
{
deg[cyc[i].fst] = 0;
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:104: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]
104 | 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 |
Incorrect |
15 ms |
23756 KB |
Output isn't correct |
2 |
Incorrect |
13 ms |
23776 KB |
Output isn't correct |
3 |
Incorrect |
13 ms |
23848 KB |
Output isn't correct |
4 |
Correct |
13 ms |
23796 KB |
Output is correct |
5 |
Incorrect |
13 ms |
23732 KB |
Output isn't correct |
6 |
Incorrect |
13 ms |
23728 KB |
Output isn't correct |
7 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
8 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
9 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
10 |
Correct |
13 ms |
23812 KB |
Output is correct |
11 |
Correct |
13 ms |
23760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
23956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
24860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
27460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
91 ms |
36244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
153 ms |
46304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
348 ms |
83032 KB |
Output is correct |
2 |
Incorrect |
677 ms |
85868 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
90052 KB |
Output is correct |
2 |
Correct |
367 ms |
90192 KB |
Output is correct |
3 |
Incorrect |
406 ms |
90164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |