Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4381 [IOI2008] Island

P4381 [IOI2008] Island

当作仙人掌做。考虑两种边,桥和环。桥直接转移,环考虑更新当前最大值,以及环上的贡献,枚举环上两点是 $O(n ^ 2)$ ,可以单调队列优化。注意重边的处理。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstdio>
#include <algorithm>
#include <vector>
#define v first
#define w second
#define pb push_back
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
typedef pair <int, int> PII;
const int N = 1e6 + 10;
int hd, tl, q[N];
int n, r[N], p[N];
LL ans, f[N];
vector <PII> g[N];
int stmp, dfn[N], low[N];
void solve (int t, vector <LL> &h, vector <LL> &d)
{
auto add = [&](int i)
{
while (hd <= tl && h[q[tl]] + d[q[tl]] <= h[i] + d[i]) --tl;
q[++tl] = i;
};
hd = 1, tl = 0;
for (int i = 0; i < t; ++i) add(i);
for (int i = 0; i < t * 2; ++i)
{
if (hd <= tl && q[hd] <= i) ++hd;
if (i - 1 < t) add(i + t - 1);
if (hd <= tl) ans = max(ans, h[i] + h[q[hd]] + d[q[hd]] - d[i]);
}
}
void tarjan (int u)
{
dfn[u] = low[u] = ++stmp;
for (PII i : g[u]) if (i.v ^ p[u])
if (!dfn[i.v])
{
p[i.v] = u;
r[i.v] = i.w;
tarjan(i.v);
low[u] = min(low[u], low[i.v]);
if (low[i.v] > dfn[u])
{
ans = max(ans, f[u] + f[i.v] + i.w);
f[u] = max(f[u], f[i.v] + i.w);
}
}
else low[u] = min(low[u], dfn[i.v]);
for (PII i : g[u]) if (dfn[i.v] > dfn[u] && p[i.v] ^ u)
{
vector <LL> h, d; h.pb(0), d.pb(0), d.pb(i.w);
int sz = 1;
for (int t = i.v; t ^ u; t = p[t])
h.pb(f[t]), d.pb(r[t]), ++sz;
h.resize(sz * 2), d.resize(sz * 2);
for (int i = 0; i < sz; ++i) h[i + sz] = h[i];
for (int i = 1; i < sz; ++i) d[i + sz] = d[i];
for (int i = 1; i < sz * 2; ++i) d[i] += d[i - 1];
for (int i = 1; i < sz; ++i)
ans = max(ans, f[u] + h[i] + max(d[i], d[sz] - d[i]));
for (int i = 1; i < sz; ++i)
f[u] = max(f[u], h[i] + max(d[i], d[sz] - d[i]));
solve(sz, h, d);
}
}
int main ()
{
read(n);
for (int i = 1, a, b; i <= n; ++i)
{
read(a, b);
g[a].pb({i, b}), g[i].pb({a, b});
}
for (int i = 1; i <= n; ++i)
{
vector <PII> t;
sort(g[i].begin(), g[i].end());
for (int j = 0; j < g[i].size(); ++j)
if (j == g[i].size() - 1 || g[i][j].v ^ g[i][j + 1].v)
t.pb(g[i][j]);
g[i] = t;
}
LL res = 0;
for (int i = 1; i <= n; ++i) if (!dfn[i])
ans = 0, tarjan(i), res += ans;
write(res);
return 0;
}