# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
650690 |
2022-10-14T17:16:07 Z |
Spade1 |
Islands (IOI08_islands) |
C++14 |
|
494 ms |
131072 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;
vector<pair<int, pii>> adj[N];
bool vis[N];
int cycle;
vector<pll> v;
ll dp[N], qs[N], ans, cur;
pll dfs2(int i, int prt1, int prt2) {
ll mx_w = 0, mx = i;
for (auto [j, w] : adj[i]) {
if (j == prt1 || j == prt2) continue;
pll ret = dfs2(j, i, 0);
if (ret.nd + w.nd > mx_w) {
mx_w = ret.nd + w.nd;
mx = ret.st;
}
}
return {mx, mx_w};
}
ll dfs3(int i, int prt1, int prt2, int prt3) {
ll mx = 0;
for (auto [j, w] : adj[i]) {
if (j == prt1 || j == prt2 || j == prt3) continue;
mx = max(mx, dfs3(j, i, prt2, prt3) + w.nd);
}
return mx;
}
int dfs(int i, int ed, int prt) {
if (vis[i]) {
cycle = prt;
return i;
}
vis[i] = 1;
int ret = 0, K, W;
for (auto [j, w] : adj[i]) {
if (j == prt && ed == w.st) continue;
if (ret != 0 && vis[j]) continue;
int k = dfs(j, w.st, i);
if (k) {
ret = k;
K = j;
W = w.nd;
}
}
if (ret == i) {
auto [val, w] = dfs2(i, K, cycle);
cur = max(cur, dfs3(val, 0, K, cycle));
v.pb({w, W});
return 0;
}
else if (ret) {
auto [val, w] = dfs2(i, prt, K);
ll k = dfs3(val, 0, prt, K);
cur = max(cur, k);
v.pb({w, W});
}
return ret;
}
void solve() {
int n; cin >> n;
for (int i = 1; i <= n; ++i) {
int b, w; cin >> b >> w;
adj[i].pb({b ,{i, w}});
adj[b].pb({i, {i, w}});
}
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
dfs(i, 0, 0);
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] + 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] + 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] + 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] + dp[j-1]);
}
ans += cur;
v.clear();
cur = 0, cycle = 0;
}
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 'std::pair<long long int, long long int> dfs2(int, int, int)':
islands.cpp:23:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for (auto [j, w] : adj[i]) {
| ^
islands.cpp: In function 'long long int dfs3(int, int, int, int)':
islands.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
36 | for (auto [j, w] : adj[i]) {
| ^
islands.cpp: In function 'int dfs(int, int, int)':
islands.cpp:50:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for (auto [j, w] : adj[i]) {
| ^
islands.cpp:61:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
61 | auto [val, w] = dfs2(i, K, cycle);
| ^
islands.cpp:67:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | auto [val, w] = dfs2(i, prt, K);
| ^
islands.cpp:62:28: warning: 'K' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | cur = max(cur, dfs3(val, 0, K, cycle));
| ~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
13 ms |
23812 KB |
Output is correct |
6 |
Correct |
17 ms |
23728 KB |
Output is correct |
7 |
Correct |
16 ms |
23724 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23752 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23940 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23860 KB |
Output is correct |
2 |
Correct |
14 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
24828 KB |
Output is correct |
2 |
Correct |
29 ms |
27860 KB |
Output is correct |
3 |
Correct |
21 ms |
24660 KB |
Output is correct |
4 |
Correct |
18 ms |
24300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
29252 KB |
Output is correct |
2 |
Correct |
47 ms |
30944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
35528 KB |
Output is correct |
2 |
Correct |
92 ms |
43352 KB |
Output is correct |
3 |
Correct |
115 ms |
58560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
42548 KB |
Output is correct |
2 |
Correct |
192 ms |
77820 KB |
Output is correct |
3 |
Correct |
217 ms |
84288 KB |
Output is correct |
4 |
Correct |
263 ms |
117000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
346 ms |
67828 KB |
Output is correct |
2 |
Runtime error |
494 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
345 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |