# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
650699 |
2022-10-14T18:08:53 Z |
Spade1 |
Islands (IOI08_islands) |
C++14 |
|
344 ms |
63908 KB |
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1e6 + 2;
pii nxt[N];
int deg[N];
bool mark[N];
ll diam[N], dist[N];
queue<int> q;
ll dp[N], qs[N];
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
int b, w; cin >> b >> w;
nxt[i] = {b, w};
deg[b]++;
}
for (int i = 1; i <= n; ++i) if (deg[i] == 0) q.push(i);
while (!q.empty()) {
int cur = q.front(); q.pop();
auto [prt, w] = nxt[cur];
diam[prt] = max(diam[prt], max(diam[cur], dist[cur]+dist[prt]+w));
dist[prt] = max(dist[prt], dist[cur]+w);
deg[prt]--;
if (deg[prt] == 0) q.push(prt);
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
vector<pll> v;
ll cur = 0;
if (deg[i] != 0 && !mark[i]) {
int curr = i;
do {
mark[curr] = 1;
cur = max(cur, diam[curr]);
v.pb({dist[curr], nxt[curr].nd});
curr = nxt[curr].st;
} while (curr != i);
}
else continue;
int m = v.size();
for (int j = 0; j <= m+1; ++j) qs[j] = 0, dp[j] = 0;
for (int j = 1; j <= m; ++j) {
qs[j] = qs[j-1] + v[j-1].nd;
dp[j] = qs[j-1] + v[j-1].st;
}
for (int j = m-1; j > 0; --j) dp[j] = max(dp[j], dp[j+1]);
for (int j = 1; j < m; ++j) {
cur = max(cur, v[j-1].st - qs[j-1] + dp[j+1]);
}
for (int j = 0; j <= m+1; ++j) dp[j] = 0;
for (int j = m; j >= 1; --j) {
dp[j] = qs[j-1] + v[j-1].st;
}
for (int j = 2; j <= m; ++j) dp[j] = max(dp[j], dp[j-1]);
for (int j = 2; j <= m; ++j) {
cur = max(cur, qs[m] + v[j-1].st - qs[j-1] + dp[j-1]);
}
ans += cur;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message
islands.cpp: In function 'void solve()':
islands.cpp:32:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | auto [prt, w] = nxt[cur];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
852 KB |
Output is correct |
2 |
Correct |
11 ms |
1912 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2508 KB |
Output is correct |
2 |
Correct |
21 ms |
3724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
7024 KB |
Output is correct |
2 |
Correct |
46 ms |
8948 KB |
Output is correct |
3 |
Correct |
55 ms |
12572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
11200 KB |
Output is correct |
2 |
Correct |
98 ms |
21872 KB |
Output is correct |
3 |
Correct |
119 ms |
21536 KB |
Output is correct |
4 |
Correct |
139 ms |
31628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
12264 KB |
Output is correct |
2 |
Correct |
244 ms |
36168 KB |
Output is correct |
3 |
Correct |
175 ms |
34084 KB |
Output is correct |
4 |
Correct |
212 ms |
51436 KB |
Output is correct |
5 |
Correct |
214 ms |
52016 KB |
Output is correct |
6 |
Correct |
336 ms |
43376 KB |
Output is correct |
7 |
Correct |
253 ms |
63908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
27712 KB |
Output is correct |
2 |
Correct |
217 ms |
27704 KB |
Output is correct |
3 |
Correct |
232 ms |
44508 KB |
Output is correct |
4 |
Correct |
234 ms |
13260 KB |
Output is correct |
5 |
Correct |
200 ms |
40740 KB |
Output is correct |
6 |
Correct |
231 ms |
45168 KB |
Output is correct |
7 |
Correct |
344 ms |
44528 KB |
Output is correct |