#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];
void dfs(int p, int i) {
int o;
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);
}
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 {
for (i = 0; i < n; i++)
if (!visited[i]) {
dfs(-1, i);
visited[i] = 2;
}
ans = (long long) n * (n - 1) * (n - 2);
for (i = 1; i < n; i++)
if (visited[i] == 1)
ans -= (long long) sz[i] * (sz[i] - 1) + (long long) (n - sz[i]) * (n - sz[i] - 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:62:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
62 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
count_triplets.c:69:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
7408 KB |
Output is correct |
2 |
Correct |
67 ms |
7276 KB |
Output is correct |
3 |
Correct |
55 ms |
7276 KB |
Output is correct |
4 |
Correct |
54 ms |
7240 KB |
Output is correct |
5 |
Correct |
59 ms |
7276 KB |
Output is correct |
6 |
Correct |
61 ms |
7276 KB |
Output is correct |
7 |
Correct |
55 ms |
7404 KB |
Output is correct |
8 |
Correct |
52 ms |
7368 KB |
Output is correct |
9 |
Correct |
58 ms |
7404 KB |
Output is correct |
10 |
Correct |
54 ms |
7344 KB |
Output is correct |
11 |
Correct |
59 ms |
7148 KB |
Output is correct |
12 |
Correct |
48 ms |
7276 KB |
Output is correct |
13 |
Correct |
53 ms |
7112 KB |
Output is correct |
14 |
Correct |
44 ms |
7148 KB |
Output is correct |
15 |
Correct |
34 ms |
6892 KB |
Output is correct |
16 |
Correct |
33 ms |
7020 KB |
Output is correct |
17 |
Correct |
8 ms |
5996 KB |
Output is correct |
18 |
Correct |
7 ms |
6124 KB |
Output is correct |
19 |
Correct |
8 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 |
7 ms |
5772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
96 ms |
7660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
7532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |