#include <bits/stdc++.h>
#define fi first
#define se second
const int N = 100100;
const int mod = 1e9 + 7;
using namespace std;
long long res;
int n;
int p[N];
int sz[N];
set<int> S[N];
set<int> A[N], B[N];
queue<pair<int, int>> q;
int get(int x) {
return p[x] == x ? x : p[x] = get(p[x]);
}
void upd(int X, int Y) {
A[X].insert(Y);
B[Y].insert(X);
if (A[Y].find(X) != A[Y].end()) {
q.push({X, Y});
}
}
void solve(int X, int Y)
{
X = get(X);
Y = get(Y);
if (X == Y) {
return;
}
if (S[X].size() < S[Y].size()) {
swap(X, Y);
}
res += 1ll * sz[X] * S[Y].size() + 1ll * sz[Y] * S[X].size();
p[Y] = X;
sz[X] += sz[Y];
for (int z: S[Y]) {
if (S[X].find(z) == S[X].end()) {
S[X].insert(z);
} else {
res -= sz[X];
}
}
A[X].erase(Y);
B[X].erase(Y);
A[Y].erase(X);
B[Y].erase(X);
for (int z: A[Y]) {
upd(X, z);
}
for (int z: B[Y]) {
upd(z, X);
}
}
int main() {
#ifdef zxc
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
int m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
S[i].insert(i);
}
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
int X = get(x);
int Y = get(y);
if (X != Y && S[Y].find(x) == S[Y].end()) {
res += sz[Y];
S[Y].insert(x);
upd(X, Y);
while (!q.empty()) {
solve(q.front().fi, q.front().se);
q.pop();
}
}
cout << res << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14352 KB |
Output is correct |
2 |
Correct |
7 ms |
14432 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14384 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14324 KB |
Output is correct |
17 |
Correct |
7 ms |
14432 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14356 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
7 ms |
14420 KB |
Output is correct |
30 |
Correct |
7 ms |
14420 KB |
Output is correct |
31 |
Correct |
11 ms |
14420 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14352 KB |
Output is correct |
2 |
Correct |
7 ms |
14432 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14384 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14324 KB |
Output is correct |
17 |
Correct |
7 ms |
14432 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14356 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
7 ms |
14420 KB |
Output is correct |
30 |
Correct |
7 ms |
14420 KB |
Output is correct |
31 |
Correct |
11 ms |
14420 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
34 |
Correct |
19 ms |
14572 KB |
Output is correct |
35 |
Correct |
382 ms |
20408 KB |
Output is correct |
36 |
Correct |
390 ms |
23652 KB |
Output is correct |
37 |
Correct |
405 ms |
23640 KB |
Output is correct |
38 |
Correct |
382 ms |
23244 KB |
Output is correct |
39 |
Correct |
14 ms |
14700 KB |
Output is correct |
40 |
Correct |
16 ms |
15700 KB |
Output is correct |
41 |
Correct |
20 ms |
15668 KB |
Output is correct |
42 |
Correct |
17 ms |
14804 KB |
Output is correct |
43 |
Correct |
14 ms |
14804 KB |
Output is correct |
44 |
Correct |
14 ms |
14776 KB |
Output is correct |
45 |
Correct |
14 ms |
14700 KB |
Output is correct |
46 |
Correct |
13 ms |
14676 KB |
Output is correct |
47 |
Correct |
15 ms |
15188 KB |
Output is correct |
48 |
Correct |
15 ms |
15220 KB |
Output is correct |
49 |
Correct |
27 ms |
16244 KB |
Output is correct |
50 |
Correct |
388 ms |
23812 KB |
Output is correct |
51 |
Correct |
30 ms |
15316 KB |
Output is correct |
52 |
Correct |
375 ms |
21988 KB |
Output is correct |
53 |
Correct |
27 ms |
15956 KB |
Output is correct |
54 |
Correct |
389 ms |
22852 KB |
Output is correct |
55 |
Correct |
16 ms |
15444 KB |
Output is correct |
56 |
Correct |
16 ms |
15340 KB |
Output is correct |
57 |
Correct |
16 ms |
15392 KB |
Output is correct |
58 |
Correct |
16 ms |
15472 KB |
Output is correct |
59 |
Correct |
14 ms |
15112 KB |
Output is correct |
60 |
Correct |
359 ms |
20132 KB |
Output is correct |
61 |
Correct |
16 ms |
15212 KB |
Output is correct |
62 |
Correct |
381 ms |
23032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14352 KB |
Output is correct |
2 |
Correct |
7 ms |
14432 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
10 ms |
14420 KB |
Output is correct |
9 |
Correct |
10 ms |
14464 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14384 KB |
Output is correct |
12 |
Correct |
7 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14324 KB |
Output is correct |
17 |
Correct |
7 ms |
14432 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14432 KB |
Output is correct |
21 |
Correct |
9 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
8 ms |
14420 KB |
Output is correct |
24 |
Correct |
8 ms |
14356 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
7 ms |
14420 KB |
Output is correct |
27 |
Correct |
7 ms |
14428 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
7 ms |
14420 KB |
Output is correct |
30 |
Correct |
7 ms |
14420 KB |
Output is correct |
31 |
Correct |
11 ms |
14420 KB |
Output is correct |
32 |
Correct |
7 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14420 KB |
Output is correct |
34 |
Correct |
19 ms |
14572 KB |
Output is correct |
35 |
Correct |
382 ms |
20408 KB |
Output is correct |
36 |
Correct |
390 ms |
23652 KB |
Output is correct |
37 |
Correct |
405 ms |
23640 KB |
Output is correct |
38 |
Correct |
382 ms |
23244 KB |
Output is correct |
39 |
Correct |
14 ms |
14700 KB |
Output is correct |
40 |
Correct |
16 ms |
15700 KB |
Output is correct |
41 |
Correct |
20 ms |
15668 KB |
Output is correct |
42 |
Correct |
17 ms |
14804 KB |
Output is correct |
43 |
Correct |
14 ms |
14804 KB |
Output is correct |
44 |
Correct |
14 ms |
14776 KB |
Output is correct |
45 |
Correct |
14 ms |
14700 KB |
Output is correct |
46 |
Correct |
13 ms |
14676 KB |
Output is correct |
47 |
Correct |
15 ms |
15188 KB |
Output is correct |
48 |
Correct |
15 ms |
15220 KB |
Output is correct |
49 |
Correct |
27 ms |
16244 KB |
Output is correct |
50 |
Correct |
388 ms |
23812 KB |
Output is correct |
51 |
Correct |
30 ms |
15316 KB |
Output is correct |
52 |
Correct |
375 ms |
21988 KB |
Output is correct |
53 |
Correct |
27 ms |
15956 KB |
Output is correct |
54 |
Correct |
389 ms |
22852 KB |
Output is correct |
55 |
Correct |
16 ms |
15444 KB |
Output is correct |
56 |
Correct |
16 ms |
15340 KB |
Output is correct |
57 |
Correct |
16 ms |
15392 KB |
Output is correct |
58 |
Correct |
16 ms |
15472 KB |
Output is correct |
59 |
Correct |
14 ms |
15112 KB |
Output is correct |
60 |
Correct |
359 ms |
20132 KB |
Output is correct |
61 |
Correct |
16 ms |
15212 KB |
Output is correct |
62 |
Correct |
381 ms |
23032 KB |
Output is correct |
63 |
Correct |
817 ms |
67640 KB |
Output is correct |
64 |
Correct |
815 ms |
67512 KB |
Output is correct |
65 |
Correct |
818 ms |
67496 KB |
Output is correct |
66 |
Correct |
401 ms |
33692 KB |
Output is correct |
67 |
Correct |
922 ms |
100656 KB |
Output is correct |
68 |
Correct |
385 ms |
33612 KB |
Output is correct |
69 |
Correct |
553 ms |
34780 KB |
Output is correct |
70 |
Correct |
409 ms |
33580 KB |
Output is correct |
71 |
Correct |
412 ms |
33604 KB |
Output is correct |
72 |
Correct |
740 ms |
56256 KB |
Output is correct |
73 |
Correct |
754 ms |
59304 KB |
Output is correct |
74 |
Correct |
1863 ms |
110160 KB |
Output is correct |
75 |
Correct |
874 ms |
45996 KB |
Output is correct |
76 |
Correct |
1201 ms |
71792 KB |
Output is correct |
77 |
Correct |
1225 ms |
72144 KB |
Output is correct |
78 |
Correct |
426 ms |
45528 KB |
Output is correct |
79 |
Correct |
754 ms |
51272 KB |
Output is correct |
80 |
Correct |
401 ms |
39608 KB |
Output is correct |
81 |
Correct |
665 ms |
43792 KB |
Output is correct |
82 |
Correct |
1052 ms |
65824 KB |
Output is correct |
83 |
Correct |
1055 ms |
65832 KB |
Output is correct |
84 |
Correct |
947 ms |
68792 KB |
Output is correct |
85 |
Correct |
910 ms |
68824 KB |
Output is correct |
86 |
Correct |
488 ms |
66372 KB |
Output is correct |
87 |
Correct |
630 ms |
68660 KB |
Output is correct |
88 |
Correct |
748 ms |
57780 KB |
Output is correct |
89 |
Correct |
1176 ms |
69244 KB |
Output is correct |