This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 rep1(n) for(int i = 0; i < n; i++)
#define rep2(i,n) for(int i = 0; i < n; i++)
#define rep3(i,a,b) for(int i = a; i < b; i++)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
#define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
#define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);
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++);
}
};
ll Diameter(const vector<vector<pii>>& g){
int n = g.size();
vector<ll> cost(n, INF);
vector<bool> used(n);
queue<int> q;
q.push(0);
cost[0] = 0;
while(q.size()){
int at = q.front();
q.pop();
if(used[at]) continue;
used[at] = 1;
each(i, g[at]) if(chmin(cost[i.first], cost[at] + i.second)) q.push(i.first);
}
int d = max_element(all(cost)) - cost.begin();
cost.assign(n, INF);
used.assign(n, 0);
q.push(d);
cost[d] = 0;
while(q.size()){
int at = q.front();
q.pop();
if(used[at]) continue;
used[at] = 1;
each(i, g[at]) if(chmin(cost[i.first], cost[at] + i.second)) q.push(i.first);
}
return *max_element(all(cost));
}
ll solve(const vector<vector<pii>>& g){
int n = g.size();
int at = 0;
vector<bool> circle(n);
rep(n) at = g[at][0].first;
rep(n){
at = g[at][0].first;
circle[at] = 1;
}
vector<ll> d(n);
auto dfs = [&](int from, int at, const auto& f) -> void {
rep(i, 1, g[at].size()){
int to = g[at][i].first;
if(to != from && !circle[to]){
f(at, to, f);
chmax(d[at], d[to] + g[at][i].second);
}
}
};
rep(n) if(circle[i]) dfs(-1, i, dfs);
ll ans = Diameter(g);
int circle_size = accumulate(all(circle), 0);
vector<ll> len(circle_size * 2 + 1);
rep(circle_size * 2){
int to = g[at][0].first;
len[i + 1] = len[i] + g[at][0].second;
at = to;
}
SlidMax mx;
rep(circle_size - 1){
at = g[at][0].first;
mx.push(len[i + 1] + d[at]);
}
at = g[at][0].first;
rep(circle_size){
chmax(ans, mx.pop() - len[i] + d[at]);
mx.push(len[circle_size + i] + d[at]);
at = g[at][0].first;
}
return ans;
}
int main(){
int n = 1000000;
vector<vector<pii>> G(n);
rep(n){
int a = (i + 1) % n, b = 100000000;
G[i].emplace_back(a, b);
}
rep(n) G[G[i][0].first].emplace_back(i, G[i][0].second);
vector<int> idx(n, -1);
ll ans = 0;
BEGIN_STACK_EXTEND(3 << 24);
rep(n) if(idx[i] == -1){
vector<int> vertex;
auto cc = [&](int at, const auto& f) -> void {
idx[at] = vertex.size();
vertex.push_back(at);
each(i, G[at]) if(idx[i.first] == -1) f(i.first, f);
};
cc(i, cc);
int n = vertex.size();
vector<vector<pii>> g(n);
rep(n){
auto& edge = G[vertex[i]][0];
g[i].emplace_back(idx[edge.first], edge.second);
}
rep(n){
auto& edge = G[vertex[i]][0];
g[idx[edge.first]].emplace_back(i, edge.second);
}
ans += solve(g);
}
END_STACK_EXTEND;
cout << ans << endl;
}
Compilation message (stderr)
islands.cpp: In instantiation of 'solve(const std::vector<std::vector<std::pair<int, int> > >&)::<lambda(int, int, const auto:1&)> [with auto:1 = solve(const std::vector<std::vector<std::pair<int, int> > >&)::<lambda(int, int, const auto:1&)>]':
islands.cpp:81:40: required from here
islands.cpp:10:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep3(i,a,b) for(int i = a; i < b; i++)
^
islands.cpp:6:32: note: in expansion of macro 'rep3'
#define overload3(a,b,c,d,...) d
^
islands.cpp:11:18: note: in expansion of macro 'overload3'
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
^~~~~~~~~
islands.cpp:73:9: note: in expansion of macro 'rep'
rep(i, 1, g[at].size()){
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |