This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
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;
}
int ds[N], sz[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)
return;
if (ds[i] > ds[j])
ds[i] = j, sz[j] += sz[i];
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i, sz[i] += sz[j];
}
}
char visited[N];
void dfs(int i) {
int o;
if (visited[i])
return;
visited[i] = 1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
join(i, j);
dfs(j);
}
}
int main() {
int n, m, i, j, j_, o;
long long ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
while (m--) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j);
}
memset(ds, -1, n * sizeof *ds);
for (i = 0; i < n; i++)
sz[i] = 1;
for (i = 0; i < n; i++)
if (eo[i] >= 2) {
j_ = ej[i][0];
for (o = eo[i]; o--; ) {
j = ej[i][o];
join(j, j_);
}
for (o = eo[i]; o--; ) {
j = ej[i][o];
dfs(j);
}
}
ans = 0;
for (i = 0; i < n; i++)
if (ds[i] < 0)
ans += sz[i] == 1 ? eo[i] : (long long) sz[i] * (sz[i] - 1);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
friends.c: In function 'append':
friends.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
friends.c: In function 'main':
friends.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
friends.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |