#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], col[NN];
void link(int u, int v, int w) {
static int ii = 0;
int i = ++ii;
to[i] = v;
ww[i] = w;
lnk[i] = head[u];
head[u] = i;
}
int cycle_head, cycle_tail;
int cyc[NN], co, par[NN], incyc[NN];
long long tentacle[NN], aa[NN];
int dfs(int u, int p) {
par[u] = p;
col[u] = 1;
for (int j = head[u]; j; j = lnk[j]) {
int v = to[j];
if (v == p) continue;
if (col[v]) {
cycle_head = u;
cycle_tail = v;
co = 0;
cyc[co++] = v;
int cur = u;
for (; cur != v && cur != -1; cur = par[cur])
cyc[co++] = cur;
return 1;
}
if (dfs(v, u))
return 1;
}
return 0;
}
void dfs2(int u, int p) {
col[u] = 1;
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])
tentacle[u] = tentacle[v] + ww[j];
}
}
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 (col[ii]) continue;
dfs(ii, -1);
long long op0 = -1e18, op1 = -1e18, here = 0;
if (co == 2) {
for (int j = head[cycle_head]; j; j = lnk[j])
if (cycle_tail == to[j])
here = max(here, ww[j]);
answer += here;
continue;
}
for (int i = 0; i < co; ++i) incyc[cyc[i]] = 1;
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.c: In function 'main':
islands.c:62:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
islands.c:64:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf("%d%d", &j, &k), link(i, j, k), link(j, i, k);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1372 KB |
Output is correct |
2 |
Correct |
9 ms |
3420 KB |
Output is correct |
3 |
Incorrect |
4 ms |
1628 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
4952 KB |
Output is correct |
2 |
Correct |
20 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
13652 KB |
Output is correct |
2 |
Correct |
42 ms |
17516 KB |
Output is correct |
3 |
Correct |
49 ms |
26904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
24892 KB |
Output is correct |
2 |
Correct |
94 ms |
44368 KB |
Output is correct |
3 |
Correct |
121 ms |
48968 KB |
Output is correct |
4 |
Correct |
127 ms |
68432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
143 ms |
47960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
221 ms |
127792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |