// In the name of God
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define _sz(e) (int)e.size()
#define _all(e) e.begin() , e.end()
#define _lcm(a , b) (1ll * a * b) / __gcd(a , b)
#define ll long long
#define int long long
#define ld long double
#define pii pair <int , int>
#define pcc pair <char , char>
#define pll pair <long long , long long>
#define piii pair <int , pair <int , int>>
#define FAST ios::sync_with_stdio(0);cin.tie(0)
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avmini,avmini2")
const int maxn = 2e5 + 10 , N = 2e6 + 10 , SQ = 320 , base = 800287 , mod = 1e9 + 7 , INF = 1e18 + 10 , lg = 22;
const ld eps = 1e-4;
int n , m , ans , sz[maxn] , par[maxn];
set <int> edges[maxn] , revedges[maxn] , inn[maxn];
queue <pii> q;
int root(int v) {
return (par[v] == v ? v : par[v] = root(par[v]));
}
inline void add_edge(int v , int u) {
edges[v].insert(u) , revedges[u].insert(v);
if(edges[u].find(v) != edges[u].end()) {
q.push({v , u});
}
}
void merge(int v , int u) {
if(v == u) {return ;}
int sz1 = _sz(edges[v]) + _sz(revedges[v]) + _sz(inn[v]);
int sz2 = _sz(edges[u]) + _sz(revedges[u]) + _sz(inn[u]);
if(sz1 < sz2) {swap(v , u);}
ans += (sz[v] * 1ll * _sz(inn[u])) + (sz[u] * 1ll * _sz(inn[v]));
sz[v] += sz[u] , par[u] = v;
for (auto x : inn[u]) {
if(inn[v].find(x) != inn[v].end()) {
ans -= sz[v];
}
else {
inn[v].insert(x);
}
}
edges[v].erase(u) , revedges[v].erase(u);
edges[u].erase(v) , revedges[u].erase(v);
for (auto x : edges[u]) {
revedges[x].erase(u);
add_edge(v , x);
}
for (auto x : revedges[u]) {
edges[x].erase(u);
add_edge(x , v);
}
edges[u].clear() , revedges[u].clear() , inn[u].clear();
}
int32_t main() {
FAST;
cin >> n >> m;
iota(par , par + maxn , 0);
fill(sz , sz + maxn , 1);
for (int i = 0; i < maxn; ++i) {
inn[i].insert(i);
}
for (int i = 0; i < m; ++i) {
int x , y; cin >> x >> y;
x-- , y = root(y - 1);
if(root(x) == y || inn[y].find(x) != inn[y].end()) {
cout << ans << '\n';
continue;
}
inn[y].insert(x);
ans += sz[y];
add_edge(root(x) , y);
while(_sz(q)) {
pii p = q.front();
merge(root(p.F) , root(p.S));
q.pop();
}
cout << ans << '\n';
}
}
// Mistakes:
// * Read the problem carefully.
// * Check maxn.
// * Overflows.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
40908 KB |
Output is correct |
2 |
Correct |
42 ms |
40924 KB |
Output is correct |
3 |
Correct |
31 ms |
40952 KB |
Output is correct |
4 |
Correct |
36 ms |
40920 KB |
Output is correct |
5 |
Correct |
31 ms |
40908 KB |
Output is correct |
6 |
Correct |
30 ms |
40940 KB |
Output is correct |
7 |
Correct |
37 ms |
40944 KB |
Output is correct |
8 |
Correct |
33 ms |
41064 KB |
Output is correct |
9 |
Correct |
32 ms |
41040 KB |
Output is correct |
10 |
Correct |
49 ms |
40896 KB |
Output is correct |
11 |
Correct |
31 ms |
40952 KB |
Output is correct |
12 |
Correct |
34 ms |
40908 KB |
Output is correct |
13 |
Correct |
31 ms |
40912 KB |
Output is correct |
14 |
Correct |
31 ms |
40916 KB |
Output is correct |
15 |
Correct |
30 ms |
40964 KB |
Output is correct |
16 |
Correct |
86 ms |
40960 KB |
Output is correct |
17 |
Correct |
70 ms |
40896 KB |
Output is correct |
18 |
Correct |
32 ms |
41008 KB |
Output is correct |
19 |
Correct |
31 ms |
41000 KB |
Output is correct |
20 |
Correct |
51 ms |
41016 KB |
Output is correct |
21 |
Correct |
32 ms |
41012 KB |
Output is correct |
22 |
Correct |
32 ms |
40900 KB |
Output is correct |
23 |
Correct |
33 ms |
41032 KB |
Output is correct |
24 |
Correct |
31 ms |
40988 KB |
Output is correct |
25 |
Correct |
33 ms |
40972 KB |
Output is correct |
26 |
Correct |
33 ms |
41020 KB |
Output is correct |
27 |
Correct |
53 ms |
40900 KB |
Output is correct |
28 |
Correct |
36 ms |
40904 KB |
Output is correct |
29 |
Correct |
31 ms |
41016 KB |
Output is correct |
30 |
Correct |
31 ms |
40908 KB |
Output is correct |
31 |
Correct |
35 ms |
41036 KB |
Output is correct |
32 |
Correct |
36 ms |
40908 KB |
Output is correct |
33 |
Correct |
39 ms |
41048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
40908 KB |
Output is correct |
2 |
Correct |
42 ms |
40924 KB |
Output is correct |
3 |
Correct |
31 ms |
40952 KB |
Output is correct |
4 |
Correct |
36 ms |
40920 KB |
Output is correct |
5 |
Correct |
31 ms |
40908 KB |
Output is correct |
6 |
Correct |
30 ms |
40940 KB |
Output is correct |
7 |
Correct |
37 ms |
40944 KB |
Output is correct |
8 |
Correct |
33 ms |
41064 KB |
Output is correct |
9 |
Correct |
32 ms |
41040 KB |
Output is correct |
10 |
Correct |
49 ms |
40896 KB |
Output is correct |
11 |
Correct |
31 ms |
40952 KB |
Output is correct |
12 |
Correct |
34 ms |
40908 KB |
Output is correct |
13 |
Correct |
31 ms |
40912 KB |
Output is correct |
14 |
Correct |
31 ms |
40916 KB |
Output is correct |
15 |
Correct |
30 ms |
40964 KB |
Output is correct |
16 |
Correct |
86 ms |
40960 KB |
Output is correct |
17 |
Correct |
70 ms |
40896 KB |
Output is correct |
18 |
Correct |
32 ms |
41008 KB |
Output is correct |
19 |
Correct |
31 ms |
41000 KB |
Output is correct |
20 |
Correct |
51 ms |
41016 KB |
Output is correct |
21 |
Correct |
32 ms |
41012 KB |
Output is correct |
22 |
Correct |
32 ms |
40900 KB |
Output is correct |
23 |
Correct |
33 ms |
41032 KB |
Output is correct |
24 |
Correct |
31 ms |
40988 KB |
Output is correct |
25 |
Correct |
33 ms |
40972 KB |
Output is correct |
26 |
Correct |
33 ms |
41020 KB |
Output is correct |
27 |
Correct |
53 ms |
40900 KB |
Output is correct |
28 |
Correct |
36 ms |
40904 KB |
Output is correct |
29 |
Correct |
31 ms |
41016 KB |
Output is correct |
30 |
Correct |
31 ms |
40908 KB |
Output is correct |
31 |
Correct |
35 ms |
41036 KB |
Output is correct |
32 |
Correct |
36 ms |
40908 KB |
Output is correct |
33 |
Correct |
39 ms |
41048 KB |
Output is correct |
34 |
Correct |
49 ms |
41076 KB |
Output is correct |
35 |
Correct |
137 ms |
46784 KB |
Output is correct |
36 |
Correct |
164 ms |
49400 KB |
Output is correct |
37 |
Correct |
201 ms |
49460 KB |
Output is correct |
38 |
Correct |
183 ms |
49120 KB |
Output is correct |
39 |
Correct |
33 ms |
41060 KB |
Output is correct |
40 |
Correct |
35 ms |
41172 KB |
Output is correct |
41 |
Correct |
43 ms |
41196 KB |
Output is correct |
42 |
Correct |
39 ms |
41036 KB |
Output is correct |
43 |
Correct |
39 ms |
41148 KB |
Output is correct |
44 |
Correct |
43 ms |
41196 KB |
Output is correct |
45 |
Correct |
47 ms |
41044 KB |
Output is correct |
46 |
Correct |
39 ms |
41012 KB |
Output is correct |
47 |
Correct |
37 ms |
41092 KB |
Output is correct |
48 |
Correct |
64 ms |
41176 KB |
Output is correct |
49 |
Correct |
47 ms |
41804 KB |
Output is correct |
50 |
Correct |
179 ms |
49604 KB |
Output is correct |
51 |
Correct |
45 ms |
41368 KB |
Output is correct |
52 |
Correct |
152 ms |
47880 KB |
Output is correct |
53 |
Correct |
56 ms |
41796 KB |
Output is correct |
54 |
Correct |
166 ms |
48724 KB |
Output is correct |
55 |
Correct |
49 ms |
41628 KB |
Output is correct |
56 |
Correct |
39 ms |
41620 KB |
Output is correct |
57 |
Correct |
42 ms |
41524 KB |
Output is correct |
58 |
Correct |
42 ms |
41628 KB |
Output is correct |
59 |
Correct |
39 ms |
41044 KB |
Output is correct |
60 |
Correct |
134 ms |
45952 KB |
Output is correct |
61 |
Correct |
48 ms |
41204 KB |
Output is correct |
62 |
Correct |
186 ms |
48908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
40908 KB |
Output is correct |
2 |
Correct |
42 ms |
40924 KB |
Output is correct |
3 |
Correct |
31 ms |
40952 KB |
Output is correct |
4 |
Correct |
36 ms |
40920 KB |
Output is correct |
5 |
Correct |
31 ms |
40908 KB |
Output is correct |
6 |
Correct |
30 ms |
40940 KB |
Output is correct |
7 |
Correct |
37 ms |
40944 KB |
Output is correct |
8 |
Correct |
33 ms |
41064 KB |
Output is correct |
9 |
Correct |
32 ms |
41040 KB |
Output is correct |
10 |
Correct |
49 ms |
40896 KB |
Output is correct |
11 |
Correct |
31 ms |
40952 KB |
Output is correct |
12 |
Correct |
34 ms |
40908 KB |
Output is correct |
13 |
Correct |
31 ms |
40912 KB |
Output is correct |
14 |
Correct |
31 ms |
40916 KB |
Output is correct |
15 |
Correct |
30 ms |
40964 KB |
Output is correct |
16 |
Correct |
86 ms |
40960 KB |
Output is correct |
17 |
Correct |
70 ms |
40896 KB |
Output is correct |
18 |
Correct |
32 ms |
41008 KB |
Output is correct |
19 |
Correct |
31 ms |
41000 KB |
Output is correct |
20 |
Correct |
51 ms |
41016 KB |
Output is correct |
21 |
Correct |
32 ms |
41012 KB |
Output is correct |
22 |
Correct |
32 ms |
40900 KB |
Output is correct |
23 |
Correct |
33 ms |
41032 KB |
Output is correct |
24 |
Correct |
31 ms |
40988 KB |
Output is correct |
25 |
Correct |
33 ms |
40972 KB |
Output is correct |
26 |
Correct |
33 ms |
41020 KB |
Output is correct |
27 |
Correct |
53 ms |
40900 KB |
Output is correct |
28 |
Correct |
36 ms |
40904 KB |
Output is correct |
29 |
Correct |
31 ms |
41016 KB |
Output is correct |
30 |
Correct |
31 ms |
40908 KB |
Output is correct |
31 |
Correct |
35 ms |
41036 KB |
Output is correct |
32 |
Correct |
36 ms |
40908 KB |
Output is correct |
33 |
Correct |
39 ms |
41048 KB |
Output is correct |
34 |
Correct |
49 ms |
41076 KB |
Output is correct |
35 |
Correct |
137 ms |
46784 KB |
Output is correct |
36 |
Correct |
164 ms |
49400 KB |
Output is correct |
37 |
Correct |
201 ms |
49460 KB |
Output is correct |
38 |
Correct |
183 ms |
49120 KB |
Output is correct |
39 |
Correct |
33 ms |
41060 KB |
Output is correct |
40 |
Correct |
35 ms |
41172 KB |
Output is correct |
41 |
Correct |
43 ms |
41196 KB |
Output is correct |
42 |
Correct |
39 ms |
41036 KB |
Output is correct |
43 |
Correct |
39 ms |
41148 KB |
Output is correct |
44 |
Correct |
43 ms |
41196 KB |
Output is correct |
45 |
Correct |
47 ms |
41044 KB |
Output is correct |
46 |
Correct |
39 ms |
41012 KB |
Output is correct |
47 |
Correct |
37 ms |
41092 KB |
Output is correct |
48 |
Correct |
64 ms |
41176 KB |
Output is correct |
49 |
Correct |
47 ms |
41804 KB |
Output is correct |
50 |
Correct |
179 ms |
49604 KB |
Output is correct |
51 |
Correct |
45 ms |
41368 KB |
Output is correct |
52 |
Correct |
152 ms |
47880 KB |
Output is correct |
53 |
Correct |
56 ms |
41796 KB |
Output is correct |
54 |
Correct |
166 ms |
48724 KB |
Output is correct |
55 |
Correct |
49 ms |
41628 KB |
Output is correct |
56 |
Correct |
39 ms |
41620 KB |
Output is correct |
57 |
Correct |
42 ms |
41524 KB |
Output is correct |
58 |
Correct |
42 ms |
41628 KB |
Output is correct |
59 |
Correct |
39 ms |
41044 KB |
Output is correct |
60 |
Correct |
134 ms |
45952 KB |
Output is correct |
61 |
Correct |
48 ms |
41204 KB |
Output is correct |
62 |
Correct |
186 ms |
48908 KB |
Output is correct |
63 |
Correct |
748 ms |
88652 KB |
Output is correct |
64 |
Correct |
760 ms |
88748 KB |
Output is correct |
65 |
Correct |
841 ms |
88728 KB |
Output is correct |
66 |
Correct |
317 ms |
45376 KB |
Output is correct |
67 |
Correct |
716 ms |
51720 KB |
Output is correct |
68 |
Correct |
351 ms |
45416 KB |
Output is correct |
69 |
Correct |
678 ms |
52408 KB |
Output is correct |
70 |
Correct |
390 ms |
45368 KB |
Output is correct |
71 |
Correct |
412 ms |
45404 KB |
Output is correct |
72 |
Correct |
806 ms |
51836 KB |
Output is correct |
73 |
Correct |
803 ms |
51792 KB |
Output is correct |
74 |
Correct |
1565 ms |
63280 KB |
Output is correct |
75 |
Correct |
1033 ms |
58168 KB |
Output is correct |
76 |
Correct |
1378 ms |
62540 KB |
Output is correct |
77 |
Correct |
1377 ms |
62680 KB |
Output is correct |
78 |
Correct |
422 ms |
51140 KB |
Output is correct |
79 |
Correct |
758 ms |
54632 KB |
Output is correct |
80 |
Correct |
350 ms |
51272 KB |
Output is correct |
81 |
Correct |
604 ms |
54620 KB |
Output is correct |
82 |
Correct |
1611 ms |
72932 KB |
Output is correct |
83 |
Correct |
1679 ms |
72936 KB |
Output is correct |
84 |
Correct |
1146 ms |
72772 KB |
Output is correct |
85 |
Correct |
1201 ms |
72712 KB |
Output is correct |
86 |
Correct |
599 ms |
46168 KB |
Output is correct |
87 |
Correct |
714 ms |
48464 KB |
Output is correct |
88 |
Correct |
879 ms |
51844 KB |
Output is correct |
89 |
Correct |
1254 ms |
62388 KB |
Output is correct |