---
tags: IOI
---
# IOI2008 Day1-2 島 (Islands)
TOO TIGHT MEMORY LIMIT
## 問題
https://www.ioi-jp.org/ioi/2008/problems/Day1_jpn/islands_j.pdf
https://oj.uz/problem/view/IOI08_islands
$N$ 個の島と $N$ 個の橋があり、 $i$ 番目の橋は島 $i$ と島 $A_i$ を長さ $L_i$ で結んでいます。
あなたは任意の島からスタートして、以下のようにして島を回ります。
- まだ訪れていない島に橋を使って移動する
- まだ訪れていない連結成分に船を使って移動する
使う橋の長さの和の最大値を求めてください。
$N ≤ 10^6$
## 考察
それぞれの連結成分は頂点と辺の数が同じ **なもりグラフ** になっていて、それぞれのなもりグラフについて最長のパスの和を求めれば良い。
なもりグラフはサイクルとサイクルの頂点から生える木に分解できる。
サイクルを一部使うとすると、それぞれのサイクルから生えた木に対してサイクルからの最遠点を求めて、使うサイクルの始点をずらしながらスライド最大値をすれば良い。
サイクルを使わないとすると、それぞれのサイクルから生えた木に対して直径を求めれば良い。
## 実装
メモリ制限がとてもきついので、連結成分を新しいグラフに取り出して最長パスを求めると 150 MiB くらいでMLEになってしまう。
- 連結成分を新しいグラフに取り出さない
- 再帰を非再帰に変える
- 直径をもとめたり、最遠点を求めたりする部分の vector を連結成分ごとに作るのではなく、共有する + 配列にする
と通る。
https://oj.uz/submission/219669
$O(N)$
1696 ms / 2000 ms
99.3 MiB / 128 MiB
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const ll INF = 0x1fffffffffffffff;
#define overload3(a,b,c,d,...) d
#define all(a) begin(a), end(a)
#define rep(n) for(int i = 0; i < n; i++)
#define each(i,a) for(auto&& i : a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
struct SlidMax{
deque<pair<ll, int>> q;
int low = 0, high = 0;
ll pop(){
ll ans = q[0].first;
if(q.front().second == low++) q.pop_front();
return ans;
}
void push(ll x){
while(q.size() && q.back().first <= x) q.pop_back();
q.emplace_back(x, high++);
}
};
const int MAX = 1000000;
bool circle[MAX], used[MAX];
vector<pii> g[MAX];
ll d[MAX], cost1[MAX], cost2[MAX];
inline ll Diameter(int at){
queue<int> q;
cost1[at] = 0;
q.push(at);
ll max = 0;
int idx;
while(q.size()){
int at = q.front();
q.pop();
each(i, g[at]) if(cost1[i.first] == INF){
cost1[i.first] = cost1[at] + i.second;
if(chmax(max, cost1[i.first])) idx = i.first;
q.push(i.first);
}
}
cost2[idx] = 0;
q.push(idx);
while(q.size()){
int at = q.front();
q.pop();
each(i, g[at]) if(cost2[i.first] == INF){
cost2[i.first] = cost2[at] + i.second;
chmax(max, cost2[i.first]);
q.push(i.first);
}
}
return max;
}
int main(){
fill(cost1, cost1 + MAX, INF);
fill(cost2, cost2 + MAX, INF);
int n;
scanf("%d", &n);
rep(n){
int a, b;
scanf("%d%d", &a, &b);
a--;
g[i].emplace_back(a, b);
}
rep(n) g[g[i][0].first].emplace_back(i, g[i][0].second);
each(i, g) i.shrink_to_fit();
ll ans = 0;
rep(n) if(!used[i]){
int at = i;
vector<int> list = {i};
while(!used[at]){
used[at] = 1;
list.push_back(at);
at = g[at][0].first;
}
each(i, list) used[i] = 0;
int circle_size = 0;
while(!circle[at]){
circle[at] = 1;
circle_size++;
at = g[at][0].first;
}
used[at] = 1;
list = {at};
for(int i = 0; i < list.size(); i++){
const int at = list[i];
for(int j = 1; j < g[at].size(); j++){
const int to = g[at][j].first;
if(!used[to]){
used[to] = 1;
list.push_back(to);
}
}
}
while(list.size() > 1){
const int at = list.back();
if(!circle[at]) chmax(d[g[at][0].first], d[at] + g[at][0].second);
list.pop_back();
}
ll max = Diameter(at);
vector<ll> len(circle_size * 2 + 1);
rep(circle_size * 2){
const int to = g[at][0].first;
len[i + 1] = len[i] + g[at][0].second;
at = to;
}
SlidMax sw;
rep(circle_size - 1){
at = g[at][0].first;
sw.push(len[i + 1] + d[at]);
}
at = g[at][0].first;
rep(circle_size){
chmax(max, sw.pop() - len[i] + d[at]);
sw.push(len[circle_size + i] + d[at]);
at = g[at][0].first;
}
ans += max;
}
cout << ans << endl;
}
```