#include <stdio.h>
#define NN 1000001
#define MM 2000002
long long max(long long i, long long j) {return i > j ? i : j;}
int n, ww[MM], to[MM], lnk[MM], head[NN];
void link(int u, int v, int w) {
static int ii = 1;
int i = ++ii;
to[i] = v;
ww[i] = w;
lnk[i] = head[u];
head[u] = i;
}
int cycle_head, cycle_tail, cyc[NN], co, par[NN];
char incyc[NN];
long long tentacle[NN], tentacle2[NN], aa[NN], here;
int dfs(int u, int pe) {
for (int j = head[u]; j; j = lnk[j]) if (j / 2 - pe / 2) {
int v = to[j];
if (par[v]) {
cycle_head = u;
cycle_tail = v;
co = 0;
int cur = u;
for (; cur != v && cur != -1; cur = par[cur])
incyc[cyc[co++] = cur] = 1;
incyc[cyc[co++] = v] = 1;
return 1;
}
par[v] = u;
if (dfs(v, j))
return 1;
}
return 0;
}
void dfs2(int u, int p) {
par[u] = p;
for (int j = head[u]; j; j = lnk[j]) {
int v = to[j];
if (v == p || incyc[v]) continue;
dfs2(v, u);
if (tentacle[v] + ww[j] >= tentacle[u])
tentacle2[u] = tentacle[u], tentacle[u] = tentacle[v] + ww[j];
else if (tentacle[v] + ww[j] >= tentacle2[u])
tentacle2[u] = tentacle[v] + ww[j];
}
here = max(here, tentacle[u]+ tentacle2[u]);
}
long long answer = 0;
int main() {
scanf("%d", &n);
for (int i = 1, j, k; i <= n; ++i)
scanf("%d%d", &j, &k), link(i, j, k), link(j, i, k);
for (int ii = 1; ii <= n; ++ii) {
if (par[ii]) continue;
par[ii] = -1;
dfs(ii, -1);
long long op0 = -1e18, op1 = -1e18;
here = 0;
long long cycle_w = 0;
for (int i = 0; i < co; ++i) {
for (int j = head[cyc[i]]; j; j = lnk[j])
if (incyc[to[j]]) cycle_w += ww[j];
dfs2(cyc[i], -1);
}
cycle_w /= 2;
for (int i = 0; i < co; ++i) {
here = max(here, tentacle[cyc[i]] + max(aa[i] + op0, -aa[i] + op1));
op0 = max(tentacle[cyc[i]] - aa[i], op0);
op1 = max(tentacle[cyc[i]] + cycle_w + aa[i], op1);
if (i + 1 < co)
for (int j = head[cyc[i]]; j; j = lnk[j])
if (to[j] == cyc[i + 1]) {
aa[i + 1] = aa[i] + ww[j];
break;
}
}
answer += here;
}
printf("%lld\n", answer);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
islands.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d", &j, &k), link(i, j, k), link(j, i, k);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6576 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7260 KB |
Output is correct |
2 |
Correct |
8 ms |
9052 KB |
Output is correct |
3 |
Correct |
5 ms |
7232 KB |
Output is correct |
4 |
Correct |
3 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10076 KB |
Output is correct |
2 |
Correct |
16 ms |
11984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
19540 KB |
Output is correct |
2 |
Correct |
66 ms |
20816 KB |
Output is correct |
3 |
Correct |
78 ms |
31008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
28244 KB |
Output is correct |
2 |
Correct |
85 ms |
48208 KB |
Output is correct |
3 |
Correct |
93 ms |
49328 KB |
Output is correct |
4 |
Correct |
122 ms |
72536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
46164 KB |
Output is correct |
2 |
Correct |
305 ms |
90960 KB |
Output is correct |
3 |
Correct |
161 ms |
51724 KB |
Output is correct |
4 |
Correct |
169 ms |
81932 KB |
Output is correct |
5 |
Correct |
198 ms |
81580 KB |
Output is correct |
6 |
Correct |
294 ms |
60624 KB |
Output is correct |
7 |
Correct |
186 ms |
105556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
131072 KB |
Output is correct |
2 |
Correct |
220 ms |
100792 KB |
Output is correct |
3 |
Correct |
209 ms |
120916 KB |
Output is correct |
4 |
Correct |
146 ms |
46084 KB |
Output is correct |
5 |
Correct |
217 ms |
82008 KB |
Output is correct |
6 |
Correct |
178 ms |
66900 KB |
Output is correct |
7 |
Correct |
376 ms |
61524 KB |
Output is correct |