#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
int N,M;
ll ans = 0;
int dsu[mxn],sz[mxn];
set<pii> in[mxn],out[mxn];
map<pii,int> toaru;
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void init(){
for(int i = 1;i<=N;i++){
dsu[i] = i;
sz[i] = 1;
}
ans = 0;
return;
}
void add_edge(int a,int b){
int ra = root(a),rb = root(b);
out[ra].insert({rb,a});
in[rb].insert({ra,a});
toaru[pii(ra,rb)]++;
ans += sz[rb];
return;
}
vector<pii> v;
void combine(){
auto [a,b] = v.back();
v.pop_back();
//cerr<<"COMBINE:"<<a<<','<<b<<endl;
int ra = root(a),rb = root(b);
if(ra == rb)return;
toaru.erase(pii(rb,ra));
if(in[ra].size()+out[ra].size()>in[rb].size()+out[rb].size())swap(ra,rb);
//cerr<<"ans:"<<ans<<endl;
ans = ans-1ll*sz[ra]*in[ra].size()-1ll*sz[rb]*in[rb].size();
//cerr<<"ans:"<<ans<<endl;
while(!in[ra].empty()){
auto [rt,now] = *in[ra].begin();
in[ra].erase(in[ra].begin());
//assert(rt != ra);
if(rt == rb){
continue;
}
out[rt].erase(pii(ra,now));
out[rt].insert({rb,now});
toaru[pii(rt,rb)]++;
toaru[pii(rt,ra)]--;
in[rb].insert({rt,now});
auto it = in[rt].lower_bound({rb,-1});
if(it != in[rt].end()&&it->fs == rb)v.emplace_back(rb,rt);
}
while(!out[ra].empty()){
auto [rt,now] = *out[ra].begin();
out[ra].erase(out[ra].begin());
//assert(rt != ra);
if(rt == rb){
continue;
}
auto src = in[rt].lower_bound({ra,-1})->sc;
in[rt].erase(pii(ra,now));
in[rt].insert(pii(rb,now));
toaru[pii(ra,rt)]--;
toaru[pii(rb,rt)]++;
out[rb].insert({rt,now});
auto it = out[rt].lower_bound({rb,-1});
if(it != out[rt].end()&&it->fs == rb)v.emplace_back(rb,rt);
}
out[rb].erase(out[rb].lower_bound({ra,-1}),out[rb].lower_bound({ra+1,-1}));
in[rb].erase(in[rb].lower_bound({ra,-1}),in[rb].lower_bound({ra+1,-1}));
//cerr<<a<<' '<<b<<":"<<ra<<' '<<rb<<endl;
//cerr<<"in[rb]:";for(auto &i:in[rb])cerr<<i.fs<<','<<i.sc<<' ';cerr<<endl;
//cerr<<"out[rb]:";for(auto &i:out[rb])cerr<<i.fs<<','<<i.sc<<' ';cerr<<endl;
ans += 1ll*(sz[ra]+sz[rb])*in[rb].size();
//cerr<<"ans:"<<ans<<endl;
ans += 2ll*sz[ra]*sz[rb];
dsu[ra] = rb;
sz[rb] += sz[ra];
return;
}
inline void calc(int a,int b){
int rb = root(b),ra = root(a);
if(in[rb].find(pii(ra,a)) != in[rb].end())return;
else if(ra == rb)return;
if(!toaru[pii(rb,ra)])add_edge(a,b);
else v.push_back({a,b});
while(!v.empty())combine();
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
init();
for(int i = 1;i<=M;i++){
int a,b;
cin>>a>>b;
calc(a,b);
cout<<ans<<'\n';
}
return 0;
}
Compilation message
joitter2.cpp: In function 'void combine()':
joitter2.cpp:42:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | auto [a,b] = v.back();
| ^
joitter2.cpp:53:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | auto [rt,now] = *in[ra].begin();
| ^
joitter2.cpp:68:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | auto [rt,now] = *out[ra].begin();
| ^
joitter2.cpp:74:8: warning: unused variable 'src' [-Wunused-variable]
74 | auto src = in[rt].lower_bound({ra,-1})->sc;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
3 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9856 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9816 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9816 KB |
Output is correct |
30 |
Correct |
2 ms |
9820 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
3 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9856 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9816 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9816 KB |
Output is correct |
30 |
Correct |
2 ms |
9820 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9820 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
66 ms |
15956 KB |
Output is correct |
36 |
Correct |
95 ms |
20172 KB |
Output is correct |
37 |
Correct |
100 ms |
20452 KB |
Output is correct |
38 |
Correct |
92 ms |
19792 KB |
Output is correct |
39 |
Correct |
4 ms |
9820 KB |
Output is correct |
40 |
Correct |
5 ms |
10076 KB |
Output is correct |
41 |
Correct |
6 ms |
10076 KB |
Output is correct |
42 |
Correct |
4 ms |
9820 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
6 ms |
10168 KB |
Output is correct |
45 |
Correct |
4 ms |
9820 KB |
Output is correct |
46 |
Correct |
5 ms |
9820 KB |
Output is correct |
47 |
Correct |
6 ms |
10076 KB |
Output is correct |
48 |
Correct |
8 ms |
10076 KB |
Output is correct |
49 |
Correct |
14 ms |
11284 KB |
Output is correct |
50 |
Correct |
103 ms |
20552 KB |
Output is correct |
51 |
Correct |
11 ms |
10628 KB |
Output is correct |
52 |
Correct |
75 ms |
17832 KB |
Output is correct |
53 |
Correct |
13 ms |
11348 KB |
Output is correct |
54 |
Correct |
82 ms |
19152 KB |
Output is correct |
55 |
Correct |
9 ms |
10844 KB |
Output is correct |
56 |
Correct |
8 ms |
10832 KB |
Output is correct |
57 |
Correct |
8 ms |
10844 KB |
Output is correct |
58 |
Correct |
9 ms |
10796 KB |
Output is correct |
59 |
Correct |
4 ms |
9820 KB |
Output is correct |
60 |
Correct |
53 ms |
14928 KB |
Output is correct |
61 |
Correct |
6 ms |
10332 KB |
Output is correct |
62 |
Correct |
92 ms |
19540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
9816 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9820 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
3 ms |
9820 KB |
Output is correct |
8 |
Correct |
3 ms |
9820 KB |
Output is correct |
9 |
Correct |
3 ms |
9820 KB |
Output is correct |
10 |
Correct |
3 ms |
9820 KB |
Output is correct |
11 |
Correct |
3 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
3 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9820 KB |
Output is correct |
15 |
Correct |
2 ms |
9856 KB |
Output is correct |
16 |
Correct |
2 ms |
9820 KB |
Output is correct |
17 |
Correct |
2 ms |
9820 KB |
Output is correct |
18 |
Correct |
3 ms |
9820 KB |
Output is correct |
19 |
Correct |
2 ms |
9816 KB |
Output is correct |
20 |
Correct |
2 ms |
9820 KB |
Output is correct |
21 |
Correct |
3 ms |
9820 KB |
Output is correct |
22 |
Correct |
2 ms |
9820 KB |
Output is correct |
23 |
Correct |
3 ms |
9820 KB |
Output is correct |
24 |
Correct |
2 ms |
9816 KB |
Output is correct |
25 |
Correct |
3 ms |
9820 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
2 ms |
9820 KB |
Output is correct |
29 |
Correct |
3 ms |
9816 KB |
Output is correct |
30 |
Correct |
2 ms |
9820 KB |
Output is correct |
31 |
Correct |
2 ms |
9820 KB |
Output is correct |
32 |
Correct |
2 ms |
9820 KB |
Output is correct |
33 |
Correct |
3 ms |
9820 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
66 ms |
15956 KB |
Output is correct |
36 |
Correct |
95 ms |
20172 KB |
Output is correct |
37 |
Correct |
100 ms |
20452 KB |
Output is correct |
38 |
Correct |
92 ms |
19792 KB |
Output is correct |
39 |
Correct |
4 ms |
9820 KB |
Output is correct |
40 |
Correct |
5 ms |
10076 KB |
Output is correct |
41 |
Correct |
6 ms |
10076 KB |
Output is correct |
42 |
Correct |
4 ms |
9820 KB |
Output is correct |
43 |
Correct |
5 ms |
10076 KB |
Output is correct |
44 |
Correct |
6 ms |
10168 KB |
Output is correct |
45 |
Correct |
4 ms |
9820 KB |
Output is correct |
46 |
Correct |
5 ms |
9820 KB |
Output is correct |
47 |
Correct |
6 ms |
10076 KB |
Output is correct |
48 |
Correct |
8 ms |
10076 KB |
Output is correct |
49 |
Correct |
14 ms |
11284 KB |
Output is correct |
50 |
Correct |
103 ms |
20552 KB |
Output is correct |
51 |
Correct |
11 ms |
10628 KB |
Output is correct |
52 |
Correct |
75 ms |
17832 KB |
Output is correct |
53 |
Correct |
13 ms |
11348 KB |
Output is correct |
54 |
Correct |
82 ms |
19152 KB |
Output is correct |
55 |
Correct |
9 ms |
10844 KB |
Output is correct |
56 |
Correct |
8 ms |
10832 KB |
Output is correct |
57 |
Correct |
8 ms |
10844 KB |
Output is correct |
58 |
Correct |
9 ms |
10796 KB |
Output is correct |
59 |
Correct |
4 ms |
9820 KB |
Output is correct |
60 |
Correct |
53 ms |
14928 KB |
Output is correct |
61 |
Correct |
6 ms |
10332 KB |
Output is correct |
62 |
Correct |
92 ms |
19540 KB |
Output is correct |
63 |
Correct |
789 ms |
82000 KB |
Output is correct |
64 |
Correct |
740 ms |
82260 KB |
Output is correct |
65 |
Correct |
762 ms |
82000 KB |
Output is correct |
66 |
Correct |
111 ms |
21072 KB |
Output is correct |
67 |
Correct |
313 ms |
28244 KB |
Output is correct |
68 |
Correct |
106 ms |
21240 KB |
Output is correct |
69 |
Correct |
331 ms |
27860 KB |
Output is correct |
70 |
Correct |
111 ms |
21456 KB |
Output is correct |
71 |
Correct |
101 ms |
21288 KB |
Output is correct |
72 |
Correct |
311 ms |
28716 KB |
Output is correct |
73 |
Correct |
342 ms |
28504 KB |
Output is correct |
74 |
Correct |
1232 ms |
60872 KB |
Output is correct |
75 |
Correct |
578 ms |
37312 KB |
Output is correct |
76 |
Correct |
858 ms |
49652 KB |
Output is correct |
77 |
Correct |
888 ms |
49952 KB |
Output is correct |
78 |
Correct |
228 ms |
31176 KB |
Output is correct |
79 |
Correct |
328 ms |
34896 KB |
Output is correct |
80 |
Correct |
216 ms |
31060 KB |
Output is correct |
81 |
Correct |
308 ms |
34384 KB |
Output is correct |
82 |
Correct |
948 ms |
59988 KB |
Output is correct |
83 |
Correct |
902 ms |
60200 KB |
Output is correct |
84 |
Correct |
835 ms |
59680 KB |
Output is correct |
85 |
Correct |
807 ms |
59552 KB |
Output is correct |
86 |
Correct |
151 ms |
20460 KB |
Output is correct |
87 |
Correct |
166 ms |
22612 KB |
Output is correct |
88 |
Correct |
315 ms |
28708 KB |
Output is correct |
89 |
Correct |
782 ms |
47752 KB |
Output is correct |