#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=2e18;
const int inf=1e9;
int n, m;
LL ans;
int par[100010], sz[100010];
set<pii> od[100010];
set<int> id[100010];
void init(){for(int i=1; i<=100000; i++){par[i]=i, sz[i]=1;}}
int findpar(int num){return num==par[num]?num:par[num]=findpar(par[num]);}
void mergepar(int a, int b){
int pa=findpar(a), pb=findpar(b);
if(pa==pb)return;
bool flg=false;
auto lb=od[pb].lower_bound(mp(pa, -inf));
if(lb!=od[pb].end()&&lb->F==pa)flg=true;
if(!flg){
ans-=(LL)sz[pb]*id[pb].size();
id[pb].insert(a);
od[pa].insert(mp(pb, a));
ans+=(LL)sz[pb]*id[pb].size();
}
else{
if(id[pa].size()+od[pa].size()>id[pb].size()+od[pb].size())swap(pa, pb);
ans-=(LL)sz[pa]*id[pa].size();
vector<pii> del;
for(auto i:od[pa]){
ans-=(LL)sz[i.F];
id[i.F].erase(i.S);
if(i.F!=pb)del.eb(i);
}
od[pa].clear();
ans+=(LL)sz[pa]*id[pb].size();
vector<int> del2;
for(auto i:id[pa]){
od[findpar(i)].erase(mp(pa, i));
if(findpar(i)!=pb)del2.eb(i);
}
id[pa].clear();
par[pa]=pb;
ans+=2ll*sz[pa]*sz[pb];
sz[pb]+=sz[pa];
for(auto i:del2)mergepar(i, pb);
for(auto i:del)mergepar(i.S, i.F);
}
}
int main(){
scanf("%d %d", &n, &m);
init();
for(int i=1; i<=m; i++){
int a, b;
scanf("%d %d", &a, &b);
mergepar(a, b);
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
10496 KB |
Output is correct |
2 |
Correct |
7 ms |
10496 KB |
Output is correct |
3 |
Correct |
9 ms |
10496 KB |
Output is correct |
4 |
Correct |
8 ms |
10496 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
8 ms |
10496 KB |
Output is correct |
7 |
Correct |
9 ms |
10496 KB |
Output is correct |
8 |
Correct |
9 ms |
10496 KB |
Output is correct |
9 |
Correct |
8 ms |
10624 KB |
Output is correct |
10 |
Correct |
9 ms |
10496 KB |
Output is correct |
11 |
Correct |
7 ms |
10496 KB |
Output is correct |
12 |
Correct |
7 ms |
10496 KB |
Output is correct |
13 |
Correct |
7 ms |
10496 KB |
Output is correct |
14 |
Correct |
8 ms |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10496 KB |
Output is correct |
16 |
Correct |
8 ms |
10496 KB |
Output is correct |
17 |
Correct |
7 ms |
10496 KB |
Output is correct |
18 |
Correct |
7 ms |
10496 KB |
Output is correct |
19 |
Correct |
7 ms |
10496 KB |
Output is correct |
20 |
Correct |
7 ms |
10496 KB |
Output is correct |
21 |
Correct |
8 ms |
10496 KB |
Output is correct |
22 |
Correct |
7 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10496 KB |
Output is correct |
24 |
Correct |
8 ms |
10496 KB |
Output is correct |
25 |
Correct |
8 ms |
10496 KB |
Output is correct |
26 |
Correct |
7 ms |
10496 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
7 ms |
10496 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10496 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
9 ms |
10496 KB |
Output is correct |
33 |
Correct |
8 ms |
10496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
10496 KB |
Output is correct |
2 |
Correct |
7 ms |
10496 KB |
Output is correct |
3 |
Correct |
9 ms |
10496 KB |
Output is correct |
4 |
Correct |
8 ms |
10496 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
8 ms |
10496 KB |
Output is correct |
7 |
Correct |
9 ms |
10496 KB |
Output is correct |
8 |
Correct |
9 ms |
10496 KB |
Output is correct |
9 |
Correct |
8 ms |
10624 KB |
Output is correct |
10 |
Correct |
9 ms |
10496 KB |
Output is correct |
11 |
Correct |
7 ms |
10496 KB |
Output is correct |
12 |
Correct |
7 ms |
10496 KB |
Output is correct |
13 |
Correct |
7 ms |
10496 KB |
Output is correct |
14 |
Correct |
8 ms |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10496 KB |
Output is correct |
16 |
Correct |
8 ms |
10496 KB |
Output is correct |
17 |
Correct |
7 ms |
10496 KB |
Output is correct |
18 |
Correct |
7 ms |
10496 KB |
Output is correct |
19 |
Correct |
7 ms |
10496 KB |
Output is correct |
20 |
Correct |
7 ms |
10496 KB |
Output is correct |
21 |
Correct |
8 ms |
10496 KB |
Output is correct |
22 |
Correct |
7 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10496 KB |
Output is correct |
24 |
Correct |
8 ms |
10496 KB |
Output is correct |
25 |
Correct |
8 ms |
10496 KB |
Output is correct |
26 |
Correct |
7 ms |
10496 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
7 ms |
10496 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10496 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
9 ms |
10496 KB |
Output is correct |
33 |
Correct |
8 ms |
10496 KB |
Output is correct |
34 |
Correct |
11 ms |
10600 KB |
Output is correct |
35 |
Correct |
145 ms |
15960 KB |
Output is correct |
36 |
Correct |
152 ms |
18040 KB |
Output is correct |
37 |
Correct |
179 ms |
17972 KB |
Output is correct |
38 |
Correct |
186 ms |
17912 KB |
Output is correct |
39 |
Correct |
11 ms |
10496 KB |
Output is correct |
40 |
Correct |
10 ms |
10624 KB |
Output is correct |
41 |
Correct |
10 ms |
10624 KB |
Output is correct |
42 |
Correct |
9 ms |
10496 KB |
Output is correct |
43 |
Correct |
10 ms |
10624 KB |
Output is correct |
44 |
Correct |
11 ms |
10624 KB |
Output is correct |
45 |
Correct |
9 ms |
10496 KB |
Output is correct |
46 |
Correct |
9 ms |
10496 KB |
Output is correct |
47 |
Correct |
10 ms |
10624 KB |
Output is correct |
48 |
Correct |
11 ms |
10624 KB |
Output is correct |
49 |
Correct |
17 ms |
11136 KB |
Output is correct |
50 |
Correct |
151 ms |
18168 KB |
Output is correct |
51 |
Correct |
14 ms |
10880 KB |
Output is correct |
52 |
Correct |
150 ms |
17016 KB |
Output is correct |
53 |
Correct |
16 ms |
11136 KB |
Output is correct |
54 |
Correct |
151 ms |
17544 KB |
Output is correct |
55 |
Correct |
12 ms |
10880 KB |
Output is correct |
56 |
Correct |
12 ms |
10880 KB |
Output is correct |
57 |
Correct |
12 ms |
10880 KB |
Output is correct |
58 |
Correct |
12 ms |
11008 KB |
Output is correct |
59 |
Correct |
11 ms |
10496 KB |
Output is correct |
60 |
Correct |
133 ms |
15480 KB |
Output is correct |
61 |
Correct |
12 ms |
10624 KB |
Output is correct |
62 |
Correct |
175 ms |
17784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
10496 KB |
Output is correct |
2 |
Correct |
7 ms |
10496 KB |
Output is correct |
3 |
Correct |
9 ms |
10496 KB |
Output is correct |
4 |
Correct |
8 ms |
10496 KB |
Output is correct |
5 |
Correct |
7 ms |
10496 KB |
Output is correct |
6 |
Correct |
8 ms |
10496 KB |
Output is correct |
7 |
Correct |
9 ms |
10496 KB |
Output is correct |
8 |
Correct |
9 ms |
10496 KB |
Output is correct |
9 |
Correct |
8 ms |
10624 KB |
Output is correct |
10 |
Correct |
9 ms |
10496 KB |
Output is correct |
11 |
Correct |
7 ms |
10496 KB |
Output is correct |
12 |
Correct |
7 ms |
10496 KB |
Output is correct |
13 |
Correct |
7 ms |
10496 KB |
Output is correct |
14 |
Correct |
8 ms |
10496 KB |
Output is correct |
15 |
Correct |
7 ms |
10496 KB |
Output is correct |
16 |
Correct |
8 ms |
10496 KB |
Output is correct |
17 |
Correct |
7 ms |
10496 KB |
Output is correct |
18 |
Correct |
7 ms |
10496 KB |
Output is correct |
19 |
Correct |
7 ms |
10496 KB |
Output is correct |
20 |
Correct |
7 ms |
10496 KB |
Output is correct |
21 |
Correct |
8 ms |
10496 KB |
Output is correct |
22 |
Correct |
7 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10496 KB |
Output is correct |
24 |
Correct |
8 ms |
10496 KB |
Output is correct |
25 |
Correct |
8 ms |
10496 KB |
Output is correct |
26 |
Correct |
7 ms |
10496 KB |
Output is correct |
27 |
Correct |
8 ms |
10496 KB |
Output is correct |
28 |
Correct |
7 ms |
10496 KB |
Output is correct |
29 |
Correct |
7 ms |
10496 KB |
Output is correct |
30 |
Correct |
7 ms |
10496 KB |
Output is correct |
31 |
Correct |
8 ms |
10496 KB |
Output is correct |
32 |
Correct |
9 ms |
10496 KB |
Output is correct |
33 |
Correct |
8 ms |
10496 KB |
Output is correct |
34 |
Correct |
11 ms |
10600 KB |
Output is correct |
35 |
Correct |
145 ms |
15960 KB |
Output is correct |
36 |
Correct |
152 ms |
18040 KB |
Output is correct |
37 |
Correct |
179 ms |
17972 KB |
Output is correct |
38 |
Correct |
186 ms |
17912 KB |
Output is correct |
39 |
Correct |
11 ms |
10496 KB |
Output is correct |
40 |
Correct |
10 ms |
10624 KB |
Output is correct |
41 |
Correct |
10 ms |
10624 KB |
Output is correct |
42 |
Correct |
9 ms |
10496 KB |
Output is correct |
43 |
Correct |
10 ms |
10624 KB |
Output is correct |
44 |
Correct |
11 ms |
10624 KB |
Output is correct |
45 |
Correct |
9 ms |
10496 KB |
Output is correct |
46 |
Correct |
9 ms |
10496 KB |
Output is correct |
47 |
Correct |
10 ms |
10624 KB |
Output is correct |
48 |
Correct |
11 ms |
10624 KB |
Output is correct |
49 |
Correct |
17 ms |
11136 KB |
Output is correct |
50 |
Correct |
151 ms |
18168 KB |
Output is correct |
51 |
Correct |
14 ms |
10880 KB |
Output is correct |
52 |
Correct |
150 ms |
17016 KB |
Output is correct |
53 |
Correct |
16 ms |
11136 KB |
Output is correct |
54 |
Correct |
151 ms |
17544 KB |
Output is correct |
55 |
Correct |
12 ms |
10880 KB |
Output is correct |
56 |
Correct |
12 ms |
10880 KB |
Output is correct |
57 |
Correct |
12 ms |
10880 KB |
Output is correct |
58 |
Correct |
12 ms |
11008 KB |
Output is correct |
59 |
Correct |
11 ms |
10496 KB |
Output is correct |
60 |
Correct |
133 ms |
15480 KB |
Output is correct |
61 |
Correct |
12 ms |
10624 KB |
Output is correct |
62 |
Correct |
175 ms |
17784 KB |
Output is correct |
63 |
Correct |
485 ms |
43656 KB |
Output is correct |
64 |
Correct |
480 ms |
43768 KB |
Output is correct |
65 |
Correct |
460 ms |
43640 KB |
Output is correct |
66 |
Correct |
132 ms |
14968 KB |
Output is correct |
67 |
Correct |
236 ms |
18808 KB |
Output is correct |
68 |
Correct |
124 ms |
14968 KB |
Output is correct |
69 |
Correct |
281 ms |
19596 KB |
Output is correct |
70 |
Correct |
137 ms |
14968 KB |
Output is correct |
71 |
Correct |
143 ms |
15008 KB |
Output is correct |
72 |
Correct |
285 ms |
18988 KB |
Output is correct |
73 |
Correct |
267 ms |
19136 KB |
Output is correct |
74 |
Correct |
793 ms |
27444 KB |
Output is correct |
75 |
Correct |
422 ms |
23800 KB |
Output is correct |
76 |
Correct |
587 ms |
26684 KB |
Output is correct |
77 |
Correct |
568 ms |
26764 KB |
Output is correct |
78 |
Correct |
169 ms |
18476 KB |
Output is correct |
79 |
Correct |
335 ms |
21692 KB |
Output is correct |
80 |
Correct |
166 ms |
18424 KB |
Output is correct |
81 |
Correct |
304 ms |
21272 KB |
Output is correct |
82 |
Correct |
479 ms |
32888 KB |
Output is correct |
83 |
Correct |
506 ms |
33040 KB |
Output is correct |
84 |
Correct |
496 ms |
33272 KB |
Output is correct |
85 |
Correct |
485 ms |
33144 KB |
Output is correct |
86 |
Correct |
145 ms |
14200 KB |
Output is correct |
87 |
Correct |
190 ms |
15860 KB |
Output is correct |
88 |
Correct |
295 ms |
18936 KB |
Output is correct |
89 |
Correct |
624 ms |
26232 KB |
Output is correct |