#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+10,mod = 998244353,inf = 1e9+10,sq = 700;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int par[N],sz[N];
ll ans;
set<pll> in[N],out[N];
int getpar(int v){
if (v == par[v]) return v;
return par[v] = getpar(par[v]);
}
inline void mg(int u,int v){
if (getpar(u) == getpar(v)) return;
ans += 2ll*sz[u]*sz[v];
while (!out[v].empty()){
auto it = out[v].lower_bound({u,0});
if (it == out[v].end() || it->X != u) break;
ans -= sz[u];
out[v].erase(it);
}
while (!out[u].empty()){
auto it = out[u].lower_bound({v,0});
if (it == out[u].end() || it->X != v) break;
ans -= sz[v];
out[u].erase(it);
}
while (!in[v].empty()){
auto it = in[v].lower_bound({u,0});
if (it == in[v].end() || it->X != u) break;
in[v].erase(it);
}
while (!in[u].empty()){
auto it = in[u].lower_bound({v,0});
if (it == in[u].end() || it->X != v) break;
in[u].erase(it);
}
if (out[u].size()+in[u].size() > out[v].size()+in[v].size()) swap(u,v);
par[u] = v;
vector<int> nxt;
int beg = in[v].size(),mosh = 0;
for (pll p : out[u]){
auto it = in[v].lower_bound({p.X,0});
if (it != in[v].end() && it->X == p.X) nxt.pb(p.X);
out[v].insert(p);
in[p.X].erase({u,p.Y});
in[p.X].insert({v,p.Y});
}
for (pll p : in[u]){
out[p.X].erase({u,p.Y});
if (in[v].find(p) != in[v].end()){
mosh++;
continue;
}
auto it = out[v].lower_bound({p.X,0});
if (it != out[v].end() && it->X == p.X) nxt.pb(p.X);
out[p.X].insert({v,p.Y});
in[v].insert(p);
ans += sz[v];
}
sz[v] += sz[u];
out[u].clear();
in[u].clear();
ans += 1ll*sz[u]*(beg-mosh);
for (int w : nxt) mg(w,v);
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n,q;
cin >> n >> q;
rep(i,1,n+1){
par[i] = i;
sz[i] = 1;
}
while (q--){
int u,v;
cin >> u >> v;
getpar(u);
getpar(v);
if (par[u] == par[v] || out[par[u]].find({par[v],u}) != out[par[u]].end()){
cout << ans << endl;
continue;
}
out[par[u]].insert({par[v],u});
in[par[v]].insert({par[u],u});
u = getpar(u);
v = getpar(v);
ans += sz[v];
auto it = out[v].lower_bound({u,0});
if (it != out[v].end() && it->X == u){
mg(u,v);
cout << ans << endl;
}
else{
cout << ans << endl;
continue;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9656 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9672 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
6 ms |
9668 KB |
Output is correct |
14 |
Correct |
6 ms |
9660 KB |
Output is correct |
15 |
Correct |
5 ms |
9672 KB |
Output is correct |
16 |
Correct |
6 ms |
9600 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9684 KB |
Output is correct |
23 |
Correct |
8 ms |
9712 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9668 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
6 ms |
9684 KB |
Output is correct |
29 |
Correct |
7 ms |
9684 KB |
Output is correct |
30 |
Correct |
7 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9656 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9672 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
6 ms |
9668 KB |
Output is correct |
14 |
Correct |
6 ms |
9660 KB |
Output is correct |
15 |
Correct |
5 ms |
9672 KB |
Output is correct |
16 |
Correct |
6 ms |
9600 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9684 KB |
Output is correct |
23 |
Correct |
8 ms |
9712 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9668 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
6 ms |
9684 KB |
Output is correct |
29 |
Correct |
7 ms |
9684 KB |
Output is correct |
30 |
Correct |
7 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
7 ms |
9828 KB |
Output is correct |
35 |
Correct |
69 ms |
15052 KB |
Output is correct |
36 |
Correct |
99 ms |
17184 KB |
Output is correct |
37 |
Correct |
94 ms |
17356 KB |
Output is correct |
38 |
Correct |
89 ms |
17132 KB |
Output is correct |
39 |
Correct |
7 ms |
9812 KB |
Output is correct |
40 |
Correct |
8 ms |
9812 KB |
Output is correct |
41 |
Correct |
8 ms |
9844 KB |
Output is correct |
42 |
Correct |
7 ms |
9684 KB |
Output is correct |
43 |
Correct |
8 ms |
9824 KB |
Output is correct |
44 |
Correct |
8 ms |
9824 KB |
Output is correct |
45 |
Correct |
6 ms |
9812 KB |
Output is correct |
46 |
Correct |
7 ms |
9684 KB |
Output is correct |
47 |
Correct |
10 ms |
9860 KB |
Output is correct |
48 |
Correct |
7 ms |
9824 KB |
Output is correct |
49 |
Correct |
13 ms |
10392 KB |
Output is correct |
50 |
Correct |
101 ms |
17416 KB |
Output is correct |
51 |
Correct |
11 ms |
10068 KB |
Output is correct |
52 |
Correct |
88 ms |
16320 KB |
Output is correct |
53 |
Correct |
13 ms |
10324 KB |
Output is correct |
54 |
Correct |
93 ms |
16788 KB |
Output is correct |
55 |
Correct |
10 ms |
10068 KB |
Output is correct |
56 |
Correct |
10 ms |
10120 KB |
Output is correct |
57 |
Correct |
10 ms |
10608 KB |
Output is correct |
58 |
Correct |
9 ms |
10580 KB |
Output is correct |
59 |
Correct |
8 ms |
9812 KB |
Output is correct |
60 |
Correct |
79 ms |
14668 KB |
Output is correct |
61 |
Correct |
8 ms |
9868 KB |
Output is correct |
62 |
Correct |
89 ms |
16948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9656 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9672 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9612 KB |
Output is correct |
12 |
Correct |
6 ms |
9716 KB |
Output is correct |
13 |
Correct |
6 ms |
9668 KB |
Output is correct |
14 |
Correct |
6 ms |
9660 KB |
Output is correct |
15 |
Correct |
5 ms |
9672 KB |
Output is correct |
16 |
Correct |
6 ms |
9600 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
9684 KB |
Output is correct |
23 |
Correct |
8 ms |
9712 KB |
Output is correct |
24 |
Correct |
5 ms |
9684 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9668 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
6 ms |
9684 KB |
Output is correct |
29 |
Correct |
7 ms |
9684 KB |
Output is correct |
30 |
Correct |
7 ms |
9684 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
5 ms |
9684 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
7 ms |
9828 KB |
Output is correct |
35 |
Correct |
69 ms |
15052 KB |
Output is correct |
36 |
Correct |
99 ms |
17184 KB |
Output is correct |
37 |
Correct |
94 ms |
17356 KB |
Output is correct |
38 |
Correct |
89 ms |
17132 KB |
Output is correct |
39 |
Correct |
7 ms |
9812 KB |
Output is correct |
40 |
Correct |
8 ms |
9812 KB |
Output is correct |
41 |
Correct |
8 ms |
9844 KB |
Output is correct |
42 |
Correct |
7 ms |
9684 KB |
Output is correct |
43 |
Correct |
8 ms |
9824 KB |
Output is correct |
44 |
Correct |
8 ms |
9824 KB |
Output is correct |
45 |
Correct |
6 ms |
9812 KB |
Output is correct |
46 |
Correct |
7 ms |
9684 KB |
Output is correct |
47 |
Correct |
10 ms |
9860 KB |
Output is correct |
48 |
Correct |
7 ms |
9824 KB |
Output is correct |
49 |
Correct |
13 ms |
10392 KB |
Output is correct |
50 |
Correct |
101 ms |
17416 KB |
Output is correct |
51 |
Correct |
11 ms |
10068 KB |
Output is correct |
52 |
Correct |
88 ms |
16320 KB |
Output is correct |
53 |
Correct |
13 ms |
10324 KB |
Output is correct |
54 |
Correct |
93 ms |
16788 KB |
Output is correct |
55 |
Correct |
10 ms |
10068 KB |
Output is correct |
56 |
Correct |
10 ms |
10120 KB |
Output is correct |
57 |
Correct |
10 ms |
10608 KB |
Output is correct |
58 |
Correct |
9 ms |
10580 KB |
Output is correct |
59 |
Correct |
8 ms |
9812 KB |
Output is correct |
60 |
Correct |
79 ms |
14668 KB |
Output is correct |
61 |
Correct |
8 ms |
9868 KB |
Output is correct |
62 |
Correct |
89 ms |
16948 KB |
Output is correct |
63 |
Correct |
421 ms |
44136 KB |
Output is correct |
64 |
Correct |
364 ms |
44072 KB |
Output is correct |
65 |
Correct |
414 ms |
44072 KB |
Output is correct |
66 |
Correct |
114 ms |
14852 KB |
Output is correct |
67 |
Correct |
200 ms |
18800 KB |
Output is correct |
68 |
Correct |
96 ms |
14896 KB |
Output is correct |
69 |
Correct |
277 ms |
19544 KB |
Output is correct |
70 |
Correct |
93 ms |
14800 KB |
Output is correct |
71 |
Correct |
89 ms |
14796 KB |
Output is correct |
72 |
Correct |
212 ms |
18908 KB |
Output is correct |
73 |
Correct |
224 ms |
18920 KB |
Output is correct |
74 |
Correct |
626 ms |
28572 KB |
Output is correct |
75 |
Correct |
407 ms |
24232 KB |
Output is correct |
76 |
Correct |
485 ms |
27556 KB |
Output is correct |
77 |
Correct |
502 ms |
27544 KB |
Output is correct |
78 |
Correct |
146 ms |
18368 KB |
Output is correct |
79 |
Correct |
275 ms |
21976 KB |
Output is correct |
80 |
Correct |
137 ms |
18304 KB |
Output is correct |
81 |
Correct |
207 ms |
21588 KB |
Output is correct |
82 |
Correct |
482 ms |
32872 KB |
Output is correct |
83 |
Correct |
477 ms |
32844 KB |
Output is correct |
84 |
Correct |
444 ms |
56340 KB |
Output is correct |
85 |
Correct |
485 ms |
56428 KB |
Output is correct |
86 |
Correct |
109 ms |
14072 KB |
Output is correct |
87 |
Correct |
122 ms |
16324 KB |
Output is correct |
88 |
Correct |
250 ms |
18928 KB |
Output is correct |
89 |
Correct |
449 ms |
26672 KB |
Output is correct |