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 pil = pair<int, ll>;
using pll = pair<ll, ll>;
using pili = pair<pil, int>;
using pli = pair<ll, int>;
#define X first
#define Y second
const int N = 1000005;
const ll INF = ll(1e18);
vector<pili> e[N];
int n, v[N], o[N], c, p[N], q[N];
vector<int> w;
vector<ll> d;
ll a[2 * N], b[2 * N], r, u[N];
void f(int x, int y){
if(q[x]){
p[c] = -1;
for(int i = c - 1; p[i + 1] != x; i--){
w.push_back(p[i]); d.push_back(u[i]);
o[p[i]] = 1;
}
return;
}
if(v[x]) return;
v[x] = 1;
int r = 0;
for(pili i : e[x]){
if(i.Y == y) continue;
q[x] = 1; p[c] = x; u[c] = i.X.Y; c++;
f(i.X.X, i.Y);
q[x] = 0; c--;
}
}
ll g(int x, int p, ll &t){
pll r = {0, 0};
for(pili i : e[x]){
if(o[i.X.X] || i.X.X == p) continue;
ll z = g(i.X.X, x, t) + i.X.Y;
if(z >= r.X){ r.Y = r.X; r.X = z; }
else if(z > r.Y) r.Y = z;
}
t = max(t, r.X + r.Y);
return r.X;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
int x; ll y;
scanf("%d%lld", &x, &y);
e[i].push_back(pili(pil(x, y), i));
e[x].push_back(pili(pil(i, y), i));
}
for(int i = 1; i <= n; i++){
if(v[i]) continue;
w.clear(); d.clear();
f(i, 0);
int s = w.size();
ll t = 0;
for(int j = 0; j < s; j++){
a[j] = a[j + s] = g(w[j], 0, t);
b[j] = d[j] + (j ? b[j - 1] : 0);
}
for(int j = 0; j < s; j++) b[j + s] = b[j + s - 1] + d[j];
priority_queue<pli> lq, rq;
for(int j = 1; j < s; j++) rq.push({a[j] + b[j], j});
for(int j = 0, k = 0; j < s; j++){
while(k < j + s && b[k] - b[j] < b[j + s] - b[k]) k++;
while(!rq.empty() && rq.top().Y < k){
int x = rq.top().Y;
lq.push({a[x] - b[x], x});
rq.pop();
}
while(!lq.empty() && lq.top().Y <= j) lq.pop();
t = max(t, max((lq.empty() ? -INF : lq.top().X) + b[j + s],
(rq.empty() ? -INF : rq.top().X) - b[j]) + a[j]);
rq.push({a[j + s] + b[j + s], j + s});
}
r += t;
}
printf("%lld\n", r);
}
Compilation message (stderr)
islands.cpp: In function 'void f(int, int)':
islands.cpp:33:9: warning: unused variable 'r' [-Wunused-variable]
int r = 0;
^
islands.cpp: In function 'int main()':
islands.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
islands.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%lld", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |