#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
const ll INF = 1e9;
const ll MAXN = 1e5+100;
set <ll> s[MAXN];
set <pll> rev[MAXN];
set <ll> edge[MAXN];
ll dsu[MAXN];
ll ans;
vector <pll> all;
void join(ll x,ll y){
x = dsu[x],y = dsu[y];
if (x==y)return;
if (sz(s[x]) < sz(s[y]))swap(x,y);
for (auto it = rev[x].lower_bound(MP(y,0));it != rev[x].end() && (*it).fi == y;it=rev[x].erase(it)){
ans -= sz(s[x]);
}
for (auto it = rev[y].lower_bound(MP(x,0));it != rev[y].end() && (*it).fi == x;it=rev[y].erase(it)){
ans -= sz(s[y]);
}
ans += sz(s[x]) * sz(s[y]) * 2;
ans += sz(rev[x]) * sz(s[y]) + sz(rev[y]) * sz(s[x]);
for (auto z:rev[y]){
if (rev[x].insert(z).se == 0){
ans -= sz(s[x]) + sz(s[y]);
}
edge[z.fi].erase(y);
edge[z.fi].insert(x);
if (edge[x].find(z.fi) != edge[x].end()){
all.push_back(MP(x,z.fi));
}
}
edge[y].erase(x);
for (auto z:edge[y]){
edge[x].insert(z);
if (z!=x){
vector <pll> tmp;
for (auto it = rev[z].lower_bound(MP(y,0));it != rev[z].end() && (*it).fi == y;it=rev[z].erase(it)){
tmp.push_back(MP(x,(*it).se));
}
for (auto z1:tmp)rev[z].insert(z1);
}
if (edge[z].find(x) != edge[z].end()){
all.push_back(MP(x,z));
}
}
for (auto z:s[y]){
dsu[z] = x;
s[x].insert(z);
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);
ll n,m;
cin>>n>>m;
for (ll i = 1;i <= n;i ++){
s[i].insert(i);
dsu[i] = i;
}
for (ll j = 1;j <= m;j ++){
ll x,y;
cin>>x>>y;
if (dsu[x] != dsu[y]){
edge[dsu[x]].insert(dsu[y]);
if (rev[dsu[y]].insert(MP(dsu[x],x)).se)ans+=sz(s[dsu[y]]);
if (edge[dsu[y]].find(dsu[x]) != edge[dsu[y]].end()){
all.push_back(MP(dsu[x],dsu[y]));
}
while (!all.empty()){
pll u = all.back();all.pop_back();
join(u.fi,u.se);
}
}
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
15192 KB |
Output is correct |
8 |
Correct |
2 ms |
15196 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
15168 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14936 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
2 ms |
14940 KB |
Output is correct |
21 |
Correct |
2 ms |
15196 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
15204 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
2 ms |
15196 KB |
Output is correct |
26 |
Correct |
2 ms |
15136 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
2 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
15192 KB |
Output is correct |
30 |
Correct |
2 ms |
15192 KB |
Output is correct |
31 |
Correct |
2 ms |
15192 KB |
Output is correct |
32 |
Correct |
2 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
15192 KB |
Output is correct |
8 |
Correct |
2 ms |
15196 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
15168 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14936 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
2 ms |
14940 KB |
Output is correct |
21 |
Correct |
2 ms |
15196 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
15204 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
2 ms |
15196 KB |
Output is correct |
26 |
Correct |
2 ms |
15136 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
2 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
15192 KB |
Output is correct |
30 |
Correct |
2 ms |
15192 KB |
Output is correct |
31 |
Correct |
2 ms |
15192 KB |
Output is correct |
32 |
Correct |
2 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
15196 KB |
Output is correct |
34 |
Correct |
3 ms |
15196 KB |
Output is correct |
35 |
Correct |
39 ms |
18544 KB |
Output is correct |
36 |
Correct |
56 ms |
20680 KB |
Output is correct |
37 |
Correct |
55 ms |
20720 KB |
Output is correct |
38 |
Correct |
50 ms |
20432 KB |
Output is correct |
39 |
Correct |
4 ms |
15396 KB |
Output is correct |
40 |
Correct |
5 ms |
15964 KB |
Output is correct |
41 |
Correct |
5 ms |
15964 KB |
Output is correct |
42 |
Correct |
3 ms |
15448 KB |
Output is correct |
43 |
Correct |
5 ms |
15448 KB |
Output is correct |
44 |
Correct |
4 ms |
15452 KB |
Output is correct |
45 |
Correct |
3 ms |
15452 KB |
Output is correct |
46 |
Correct |
3 ms |
15452 KB |
Output is correct |
47 |
Correct |
4 ms |
15768 KB |
Output is correct |
48 |
Correct |
4 ms |
15800 KB |
Output is correct |
49 |
Correct |
7 ms |
16052 KB |
Output is correct |
50 |
Correct |
53 ms |
20816 KB |
Output is correct |
51 |
Correct |
5 ms |
15708 KB |
Output is correct |
52 |
Correct |
43 ms |
19324 KB |
Output is correct |
53 |
Correct |
7 ms |
15960 KB |
Output is correct |
54 |
Correct |
49 ms |
19964 KB |
Output is correct |
55 |
Correct |
5 ms |
15708 KB |
Output is correct |
56 |
Correct |
4 ms |
15708 KB |
Output is correct |
57 |
Correct |
4 ms |
15708 KB |
Output is correct |
58 |
Correct |
4 ms |
15708 KB |
Output is correct |
59 |
Correct |
6 ms |
16220 KB |
Output is correct |
60 |
Correct |
39 ms |
18536 KB |
Output is correct |
61 |
Correct |
6 ms |
15704 KB |
Output is correct |
62 |
Correct |
52 ms |
20172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14936 KB |
Output is correct |
6 |
Correct |
2 ms |
14936 KB |
Output is correct |
7 |
Correct |
3 ms |
15192 KB |
Output is correct |
8 |
Correct |
2 ms |
15196 KB |
Output is correct |
9 |
Correct |
2 ms |
15196 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
15168 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
2 ms |
14936 KB |
Output is correct |
19 |
Correct |
2 ms |
14940 KB |
Output is correct |
20 |
Correct |
2 ms |
14940 KB |
Output is correct |
21 |
Correct |
2 ms |
15196 KB |
Output is correct |
22 |
Correct |
2 ms |
14940 KB |
Output is correct |
23 |
Correct |
2 ms |
15204 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
2 ms |
15196 KB |
Output is correct |
26 |
Correct |
2 ms |
15136 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
2 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
15192 KB |
Output is correct |
30 |
Correct |
2 ms |
15192 KB |
Output is correct |
31 |
Correct |
2 ms |
15192 KB |
Output is correct |
32 |
Correct |
2 ms |
14940 KB |
Output is correct |
33 |
Correct |
2 ms |
15196 KB |
Output is correct |
34 |
Correct |
3 ms |
15196 KB |
Output is correct |
35 |
Correct |
39 ms |
18544 KB |
Output is correct |
36 |
Correct |
56 ms |
20680 KB |
Output is correct |
37 |
Correct |
55 ms |
20720 KB |
Output is correct |
38 |
Correct |
50 ms |
20432 KB |
Output is correct |
39 |
Correct |
4 ms |
15396 KB |
Output is correct |
40 |
Correct |
5 ms |
15964 KB |
Output is correct |
41 |
Correct |
5 ms |
15964 KB |
Output is correct |
42 |
Correct |
3 ms |
15448 KB |
Output is correct |
43 |
Correct |
5 ms |
15448 KB |
Output is correct |
44 |
Correct |
4 ms |
15452 KB |
Output is correct |
45 |
Correct |
3 ms |
15452 KB |
Output is correct |
46 |
Correct |
3 ms |
15452 KB |
Output is correct |
47 |
Correct |
4 ms |
15768 KB |
Output is correct |
48 |
Correct |
4 ms |
15800 KB |
Output is correct |
49 |
Correct |
7 ms |
16052 KB |
Output is correct |
50 |
Correct |
53 ms |
20816 KB |
Output is correct |
51 |
Correct |
5 ms |
15708 KB |
Output is correct |
52 |
Correct |
43 ms |
19324 KB |
Output is correct |
53 |
Correct |
7 ms |
15960 KB |
Output is correct |
54 |
Correct |
49 ms |
19964 KB |
Output is correct |
55 |
Correct |
5 ms |
15708 KB |
Output is correct |
56 |
Correct |
4 ms |
15708 KB |
Output is correct |
57 |
Correct |
4 ms |
15708 KB |
Output is correct |
58 |
Correct |
4 ms |
15708 KB |
Output is correct |
59 |
Correct |
6 ms |
16220 KB |
Output is correct |
60 |
Correct |
39 ms |
18536 KB |
Output is correct |
61 |
Correct |
6 ms |
15704 KB |
Output is correct |
62 |
Correct |
52 ms |
20172 KB |
Output is correct |
63 |
Correct |
203 ms |
54812 KB |
Output is correct |
64 |
Correct |
203 ms |
54864 KB |
Output is correct |
65 |
Correct |
189 ms |
54864 KB |
Output is correct |
66 |
Correct |
97 ms |
31316 KB |
Output is correct |
67 |
Correct |
332 ms |
83536 KB |
Output is correct |
68 |
Correct |
106 ms |
33620 KB |
Output is correct |
69 |
Correct |
178 ms |
34552 KB |
Output is correct |
70 |
Correct |
105 ms |
33876 KB |
Output is correct |
71 |
Correct |
99 ms |
33620 KB |
Output is correct |
72 |
Correct |
251 ms |
52540 KB |
Output is correct |
73 |
Correct |
261 ms |
55920 KB |
Output is correct |
74 |
Correct |
532 ms |
70220 KB |
Output is correct |
75 |
Correct |
276 ms |
39748 KB |
Output is correct |
76 |
Correct |
350 ms |
53208 KB |
Output is correct |
77 |
Correct |
353 ms |
53328 KB |
Output is correct |
78 |
Correct |
95 ms |
34900 KB |
Output is correct |
79 |
Correct |
170 ms |
39896 KB |
Output is correct |
80 |
Correct |
104 ms |
34644 KB |
Output is correct |
81 |
Correct |
187 ms |
38676 KB |
Output is correct |
82 |
Correct |
283 ms |
47812 KB |
Output is correct |
83 |
Correct |
284 ms |
47700 KB |
Output is correct |
84 |
Correct |
258 ms |
47188 KB |
Output is correct |
85 |
Correct |
270 ms |
47140 KB |
Output is correct |
86 |
Correct |
319 ms |
100156 KB |
Output is correct |
87 |
Correct |
312 ms |
102228 KB |
Output is correct |
88 |
Correct |
273 ms |
54168 KB |
Output is correct |
89 |
Correct |
308 ms |
51720 KB |
Output is correct |