#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M 300000
#define X 0x3f3f3f3f
#define MD 0x7fffffff
int hash(int i, int j) {
return ((long long) i * X + j) % MD % M;
}
int *eu[M], *ev[M], *ew[M], eo_[M];
void add(int u, int v, int w) {
int h = hash(u, v), o;
for (o = eo_[h]; o--; )
if (eu[h][o] == u && ev[h][o] == v) {
ew[h][o] += w;
return;
}
o = eo_[h]++;
if (o >= 2 && (o & o - 1) == 0) {
eu[h] = (int *) realloc(eu[h], o * 2 * sizeof *eu[h]);
ev[h] = (int *) realloc(ev[h], o * 2 * sizeof *ev[h]);
ew[h] = (int *) realloc(ew[h], o * 2 * sizeof *ew[h]);
}
eu[h][o] = u, ev[h][o] = v, ew[h][o] = w;
}
void remove_(int u, int v) {
int h = hash(u, v), o;
for (o = eo_[h]; o--; )
if (eu[h][o] == u && ev[h][o] == v) {
eo_[h]--;
while (o < eo_[h])
eu[h][o] = eu[h][o + 1], ev[h][o] = ev[h][o + 1], ew[h][o] = ew[h][o + 1], o++;
return;
}
}
int get(int u, int v) {
int h = hash(u, v), o;
for (o = eo_[h]; o--; )
if (eu[h][o] == u && ev[h][o] == v)
return ew[h][o];
return 0;
}
int ds[N], sz[N], dd[N], *ej[N], eo[N], n;
int uu[M], vv[M], cnt;
long long ans;
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 find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
int o, tmp, d, w;
i = find(i);
j = find(j);
if (i == j)
return;
if (sz[i] > sz[j])
tmp = i, i = j, j = tmp;
if ((w = get(i, j)))
remove_(i, j), dd[j] -= w, ans -= (long long) w * sz[j];
if ((w = get(j, i)))
remove_(j, i), dd[i] -= w, ans -= (long long) w * sz[i];
d = dd[j] + dd[i];
for (o = eo[i]; o--; ) {
int k = ej[i][o];
if (k < n) {
int wik = get(i, k), wki = get(k, i), wjk = get(j, k), wkj = get(k, j);
remove_(i, k), remove_(k, i);
if (wki && wkj)
if (wik && wkj || wki && wjk)
uu[cnt] = j, vv[cnt] = k, cnt++;
add(j, k, wik), add(k, j, wki);
append(j, k);
} else {
if (find(k - n) == j || find(k - n) == i)
continue;
if (get(k, i) && get(k, j))
d--;
}
}
free(ej[i]);
ans += (long long) sz[i] * sz[j] * 2;
ans -= (long long) dd[i] * sz[i] + (long long) dd[j] * sz[j];
ds[i] = j, sz[j] += sz[i], dd[j] = d;
ans += (long long) dd[j] * sz[j];
}
int main() {
int m, h, i, j, u, v;
scanf("%d%d", &n, &m);
for (h = 0; h < M; h++) {
eu[h] = (int *) malloc(2 * sizeof *eu[h]);
ev[h] = (int *) malloc(2 * sizeof *ev[h]);
ew[h] = (int *) malloc(2 * sizeof *ew[h]);
}
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ds[i] = -1, sz[i] = 1;
}
while (m--) {
scanf("%d%d", &i, &j), i--, j--;
u = find(i), v = find(j);
if (u != v) {
if (!get(n + i, v))
dd[v]++, ans += sz[v], add(n + i, v, 1), add(u, v, 1), append(v, n + i);
append(u, v), append(v, u);
if (get(v, u)) {
cnt = 0;
uu[cnt] = i, vv[cnt] = j, cnt++;
while (cnt)
cnt--, join(uu[cnt], vv[cnt]);
}
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
joitter2.c: In function 'add':
joitter2.c:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
24 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
joitter2.c: In function 'append':
joitter2.c:60:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
60 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
joitter2.c: In function 'join':
joitter2.c:91:13: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
91 | if (wik && wkj || wki && wjk)
| ~~~~^~~~~~
joitter2.c: In function 'main':
joitter2.c:112:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
joitter2.c:123:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
35580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
35580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
35580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |