/* There's someone in my head but it's not me */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e6 + 1;
int n, rt[N], P[N], C[N], out[N], w[N], R; ll A[N], ret[N], H[N], S[N];
vector<int> adj[N], vec; vector<vector<int>> comps;
int Find(int v) {
return !rt[v] ? v : rt[v] = Find(rt[v]);
}
int Union(int v, int u) {
u = Find(u), v = Find(v);
if (u == v) return 1;
rt[u] = v;
return 0;
}
void preDFS(int v, int tr, int p = -1) {
vec.push_back(v);
for (int &id : adj[v]) {
int u = out[id] ^ id ^ v;
if ((!C[u] || u == tr) && u != p)
H[u] = H[v] + w[id], preDFS(u, tr, v);
}
}
void cycDFS(int v, int tr, int p = -1) {
P[v] = p; vec.push_back(v);
for (int &id : adj[v]) {
if (id != tr) {
int u = out[id] ^ id ^ v;
if (u != p) cycDFS(u, tr, v);
}
}
}
ll CYC(int v, int p = -1) {
ll ans = -1e18;
for (int &id : adj[v]) {
int u = out[id] ^ id ^ v;
if (C[u] && u != p && u != R) {
H[u] = H[v] + w[id], ans = CYC(u, v);
break;
}
}
ret[Find(v)] = max(ret[Find(v)], ans + A[v] - H[v]);
return max(ans, A[v] + H[v]);
}
ll CYC2(int v, int p = -1) {
ll ans = -1e18;
for (int &id : adj[v]) {
int u = out[id] ^ id ^ v;
if (C[u] && u != p && u != R) {
ans = CYC2(u, v);
break;
}
}
ret[Find(v)] = max(ret[Find(v)], ans + A[v] + H[v]);
return max(ans, S[Find(v)] - H[v] + A[v]);
}
int main() {
scanf("%d", &n);
vector<int> bads;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &out[i], &w[i]);
adj[i].push_back(i);
adj[out[i]].push_back(i);
if (Union(i, out[i]))
bads.push_back(i);
}
for (int &id : bads) {
vec = {};
cycDFS(out[id], id);
comps.push_back(vec);
int v = id;
while (~v)
C[v] = 1, v = P[v];
}
bads = {}, bads.shrink_to_fit();
for (int i = 1; i <= n; i++) {
if (!C[i]) continue;
vec = {}, preDFS(i, i); vec.shrink_to_fit();
for (int &id : adj[i]) {
int u = out[id] ^ id ^ i;
if (C[u]) S[Find(i)] += w[id];
}
int b = -1;
for (int &v : vec)
if (!~b || H[b] < H[v]) b = v;
A[i] = H[b]; vec = {};
H[b] = 0, preDFS(b, i);
b = -1;
for (int v : vec)
if (!~b || H[b] < H[v]) b = v;
ret[Find(i)] = max(ret[Find(i)], H[b]);
}
ll res = 0;
for (; SZ(comps); comps.pop_back()) {
vector<int> &c = comps.back();
S[Find(c.back())] >>= 1;
for (int &v : c)
if (C[v]) R = v;
H[R] = 0;
CYC(R), CYC2(R);
res += ret[Find(c.back())];
}
printf("%lld\n", res);
return 0;
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
75 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
78 | scanf("%d%d", &out[i], &w[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23796 KB |
Output is correct |
3 |
Correct |
15 ms |
23884 KB |
Output is correct |
4 |
Correct |
15 ms |
23844 KB |
Output is correct |
5 |
Correct |
15 ms |
23836 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
16 ms |
23756 KB |
Output is correct |
9 |
Correct |
16 ms |
23756 KB |
Output is correct |
10 |
Correct |
17 ms |
23760 KB |
Output is correct |
11 |
Correct |
17 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23916 KB |
Output is correct |
2 |
Correct |
16 ms |
23992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24012 KB |
Output is correct |
2 |
Correct |
18 ms |
24224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
24952 KB |
Output is correct |
2 |
Correct |
41 ms |
27280 KB |
Output is correct |
3 |
Correct |
31 ms |
25472 KB |
Output is correct |
4 |
Correct |
22 ms |
24660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
28708 KB |
Output is correct |
2 |
Correct |
64 ms |
31992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
39364 KB |
Output is correct |
2 |
Correct |
136 ms |
44044 KB |
Output is correct |
3 |
Correct |
166 ms |
50964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
53444 KB |
Output is correct |
2 |
Correct |
288 ms |
71920 KB |
Output is correct |
3 |
Correct |
324 ms |
81712 KB |
Output is correct |
4 |
Correct |
373 ms |
93200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
412 ms |
104812 KB |
Output is correct |
2 |
Correct |
1288 ms |
120544 KB |
Output is correct |
3 |
Correct |
405 ms |
82028 KB |
Output is correct |
4 |
Correct |
549 ms |
113436 KB |
Output is correct |
5 |
Correct |
546 ms |
113232 KB |
Output is correct |
6 |
Correct |
1932 ms |
89276 KB |
Output is correct |
7 |
Runtime error |
561 ms |
131076 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
425 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |