# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1018646 |
2024-07-10T07:47:57 Z |
Muaath_5 |
Islands (IOI08_islands) |
C++14 |
|
427 ms |
131072 KB |
#pragma GCC optimize("Og")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 5e5+5;
// input
int n;
vector<pair<int, int>> adj[N]; // 8M
map<pii, int> edge; // count duplicate // 12M
map<pii, int> we; // weight of edge // 12M
// dfs
vector<bool> vis(N); // 1M or less
vector<int> stck; // 1M
vector<int> cyc; // 1M
// diameter dfs
pii remedge;
int blk[2];
int mx[N]; // 1M
ll a[N]; // 8M
//ll pref[N]; // 8M
//ll mxdep[N]; // 8M
void dfs(int node, int par=0) {
vis[node] = 1;
stck.push_back(node);
for (auto &[ch, cost] : adj[node]) {
if (!vis[ch])
dfs(ch, node);
else if (!cyc.size() && edge[{node, ch}] == 2) {
cyc.push_back(node);
cyc.push_back(ch);
}
else if (!cyc.size() && vis[ch] && ch != par) {
bool st = 0;
for (int cur : stck) {
if (cur == ch) st = 1;
if (st) cyc.push_back(cur);
}
}
}
stck.pop_back();
}
ll farthest_dp(int node, int par=0) {
mx[node] = node;
ll mxdep = 0;
for (auto [ch, cost] : adj[node]) {
if (blk[0] == ch || blk[1] == ch || ch == par) continue;
if (make_pair(node, ch) != remedge && make_pair(ch, node) != remedge) {
ll mxdepch = farthest_dp(ch, node);
if (mxdepch + cost > mxdep) {
mxdep = mxdepch + cost;
mx[node] = mx[ch];
}
}
}
return mxdep;
}
ll solve_for(int node)
{
cyc.clear();
dfs(node);
const ll L = cyc.size();
//assert(L > 1);
// cerr << "#" << node << ": ";
// cerr << L << ": ";
// for(int i:cyc)cerr<<i<<' ';
// cerr<<"| ";
// remove an edge from the cycle and find diameter
if (L > 2)
remedge = {cyc[0], cyc[1]};
else
remedge = {-1, -1};
farthest_dp(node);
int fr = mx[node];
ll diam = farthest_dp(fr);
// a[i] = max depth of tree going from i not in cycle direction
// prefix sums
for (int i = 0; i < L; i++) {
blk[0] = cyc[(i+1)%L];
blk[1] = cyc[(i-1+L)%L];
a[i] = farthest_dp(cyc[i]);
}
// for (int i = 0; i < L; i++)
// cerr << a[i] << ' ';
// cerr << "| ";
// cost of path(i,j) = a[i] + a[j] + (p[i] - p[j]);
// maintain maximum a[j] - p[j], and the answer for suffix i:
//
ll prvmx = 0, prvmx2 = 0;;
ll sol = 0;
ll pref = 0;
for (int i = 1; i < L; i++)
pref += we[{cyc[i], cyc[i-1]}];
const ll LL = pref + we[{cyc[L-1], cyc[0]}];
if (L == 2) {
sol = *max_element(a, a+L) + we[{cyc[0], cyc[1]}];
goto RET;
}
pref = 0;
prvmx = prvmx2 = a[0];
for (int i = 1; i < L; i++) {
pref += we[{cyc[i], cyc[i-1]}];
sol = max(sol, a[i] + pref + prvmx);
sol = max(sol, LL + a[i] - pref + prvmx2);
prvmx2 = max(prvmx2, a[i] + pref);
prvmx = max(prvmx, a[i] - pref);
}
RET:
return max(diam, sol);
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
// stck.reserve(N/2);
// cyc.reserve(N/2);
cin >> n;
for (int i = 1; i <= n; i++) {
int u, w;
cin >> u >> w;
adj[i].push_back({u, w});
adj[u].push_back({i, w});
edge[{i,u}]++, edge[{u,i}]++;
we[{i, u}] = we[{u, i}] = max(we[{i, u}], w);
}
ll sum = 0;
for (int i = 1; i <= n; i++)
if (!vis[i])
sum += solve_for(i);
cout << sum;
}
Compilation message
islands.cpp: In function 'void dfs(int, int)':
islands.cpp:33:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for (auto &[ch, cost] : adj[node]) {
| ^
islands.cpp: In function 'long long int farthest_dp(int, int)':
islands.cpp:56:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
56 | for (auto [ch, cost] : adj[node]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15720 KB |
Output is correct |
2 |
Correct |
5 ms |
15964 KB |
Output is correct |
3 |
Correct |
4 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15908 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
4 ms |
15708 KB |
Output is correct |
9 |
Correct |
5 ms |
15708 KB |
Output is correct |
10 |
Correct |
5 ms |
15708 KB |
Output is correct |
11 |
Correct |
4 ms |
15708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16064 KB |
Output is correct |
2 |
Correct |
6 ms |
15960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
16256 KB |
Output is correct |
2 |
Correct |
9 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
20316 KB |
Output is correct |
2 |
Correct |
65 ms |
28012 KB |
Output is correct |
3 |
Correct |
33 ms |
21848 KB |
Output is correct |
4 |
Correct |
16 ms |
18888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
33108 KB |
Output is correct |
2 |
Correct |
123 ms |
47104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
76288 KB |
Output is correct |
2 |
Correct |
335 ms |
93000 KB |
Output is correct |
3 |
Correct |
410 ms |
110140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
427 ms |
131072 KB |
Output is correct |
2 |
Runtime error |
284 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
27740 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
38788 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |