#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define M 200000
int ds[N], mm[N], nn[N];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j) {
mm[i]++;
return;
}
if (ds[i] > ds[j])
ds[i] = j, mm[j] += mm[i] + 1, nn[j] += nn[i];
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, mm[i] += mm[j] + 1, nn[i] += nn[j];
}
}
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
char visited[N]; int sz[N], qu[N], cnt;
void dfs(int p, int i) {
int o;
qu[cnt++] = i;
visited[i] = 1;
sz[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
sz[i] += sz[j];
}
}
}
int main() {
static int dd[N];
int n, m, h, i, j, pathcyc;
long long ans;
scanf("%d%d", &n, &m);
memset(ds, -1, n * sizeof *ds);
for (i = 0; i < n; i++) {
nn[i] = 1;
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
dd[i]++, dd[j]++;
join(i, j);
append(i, j), append(j, i);
}
pathcyc = 1;
for (i = 0; i < n; i++)
if (dd[i] > 2) {
pathcyc = 0;
break;
}
if (pathcyc) {
ans = 0;
for (i = 0; i < n; i++)
if (ds[i] < 0)
ans += nn[i] == mm[i] ? (long long) nn[i] * (nn[i] - 1) * (nn[i] - 2) : (long long) nn[i] * (nn[i] - 1) * (nn[i] - 2) / 3;
} else {
ans = 0;
for (i = 0; i < n; i++)
if (!visited[i]) {
dfs(-1, i);
ans += (long long) sz[i] * (sz[i] - 1) * (sz[i] - 2);
while (--cnt) {
j = qu[cnt];
ans -= (long long) sz[j] * (sz[j] - 1) + (long long) (sz[i] - sz[j]) * (sz[i] - sz[j] - 1);
}
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
count_triplets.c: In function 'append':
count_triplets.c:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
35 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
count_triplets.c: In function 'main':
count_triplets.c:63:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
63 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
count_triplets.c:70:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
46444 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
46444 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
7384 KB |
Output is correct |
2 |
Correct |
54 ms |
7404 KB |
Output is correct |
3 |
Correct |
77 ms |
7368 KB |
Output is correct |
4 |
Correct |
64 ms |
7404 KB |
Output is correct |
5 |
Correct |
68 ms |
7404 KB |
Output is correct |
6 |
Correct |
54 ms |
7404 KB |
Output is correct |
7 |
Correct |
72 ms |
7364 KB |
Output is correct |
8 |
Correct |
69 ms |
7404 KB |
Output is correct |
9 |
Correct |
60 ms |
7332 KB |
Output is correct |
10 |
Correct |
65 ms |
7404 KB |
Output is correct |
11 |
Correct |
53 ms |
7276 KB |
Output is correct |
12 |
Correct |
60 ms |
7164 KB |
Output is correct |
13 |
Correct |
45 ms |
7236 KB |
Output is correct |
14 |
Correct |
50 ms |
7148 KB |
Output is correct |
15 |
Correct |
35 ms |
6892 KB |
Output is correct |
16 |
Correct |
33 ms |
6892 KB |
Output is correct |
17 |
Correct |
9 ms |
6124 KB |
Output is correct |
18 |
Correct |
9 ms |
6124 KB |
Output is correct |
19 |
Correct |
9 ms |
5996 KB |
Output is correct |
20 |
Correct |
8 ms |
5996 KB |
Output is correct |
21 |
Correct |
7 ms |
5868 KB |
Output is correct |
22 |
Correct |
8 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
496 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
8556 KB |
Output is correct |
2 |
Correct |
80 ms |
8576 KB |
Output is correct |
3 |
Correct |
93 ms |
8744 KB |
Output is correct |
4 |
Correct |
78 ms |
8556 KB |
Output is correct |
5 |
Correct |
72 ms |
8520 KB |
Output is correct |
6 |
Correct |
63 ms |
7364 KB |
Output is correct |
7 |
Correct |
90 ms |
11116 KB |
Output is correct |
8 |
Correct |
71 ms |
10476 KB |
Output is correct |
9 |
Correct |
82 ms |
9708 KB |
Output is correct |
10 |
Correct |
95 ms |
8428 KB |
Output is correct |
11 |
Correct |
83 ms |
8556 KB |
Output is correct |
12 |
Correct |
78 ms |
8300 KB |
Output is correct |
13 |
Correct |
78 ms |
8316 KB |
Output is correct |
14 |
Correct |
58 ms |
7916 KB |
Output is correct |
15 |
Correct |
59 ms |
7660 KB |
Output is correct |
16 |
Correct |
40 ms |
7276 KB |
Output is correct |
17 |
Correct |
58 ms |
8384 KB |
Output is correct |
18 |
Correct |
77 ms |
8408 KB |
Output is correct |
19 |
Correct |
61 ms |
8528 KB |
Output is correct |
20 |
Correct |
66 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Runtime error |
36 ms |
33900 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
8644 KB |
Output is correct |
2 |
Correct |
87 ms |
8428 KB |
Output is correct |
3 |
Runtime error |
135 ms |
63312 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
46444 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
46444 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |