#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
int N,M;
ll rod[maxn],vel[maxn];
ll res;
set<int> cukomp[maxn],kompuc[maxn],compout[maxn],compin[maxn];
vector<int> koji[maxn];
queue<pair<int,int>> Q;
int getpar(int x){
if(rod[x]==x)
return x;
rod[x]=getpar(rod[x]);
return rod[x];
}
void pretvori(int a,int b){
for(auto it=kompuc[a].begin();it!=kompuc[a].end();it++){
cukomp[*it].erase(a);
if(getpar(*it)!=b)
cukomp[*it].insert(b);
}
// cout<<"A"<<endl;
for(auto it=compout[a].begin();it!=compout[a].end();it++){
compin[*it].erase(a);
compin[*it].insert(b);
}
//cout<<"B"<<endl;
for(auto it=compin[a].begin();it!=compin[a].end();it++){
compout[*it].erase(a);
compout[*it].insert(b);
}
// cout<<"C"<<endl;
}
void spoj(int a,int b){ /// a i b su indeksi komponenti
if(getpar(a)!=a or getpar(b)!=b)
return;
if(vel[b]>vel[a])
swap(a,b);
//cout<<"SPAJAM "<<a<<" "<<b<<endl;
res-=vel[a]*kompuc[a].size();
res-=vel[a]*(vel[a]-1);
res-=vel[b]*kompuc[b].size();
res-=vel[b]*(vel[b]-1);
//cout<<res<<endl;
compin[a].erase(b);
compout[a].erase(b);
compin[b].erase(a);
compout[b].erase(a);
//cout<<"OVDE"<<endl;
vel[a]+=vel[b];
rod[b]=a;
pretvori(b,a);
// cout<<"OKK"<<endl;
for(int i=0;i<koji[b].size();i++){
kompuc[a].erase(koji[b][i]);
koji[a].push_back(koji[b][i]);
}
for(auto it=compin[b].begin();it!=compin[b].end();it++){
if(compout[a].find(*it)!=compout[a].end())
Q.push({getpar(*it),getpar(a)});
compin[a].insert(*it);
}
for(auto it=compout[b].begin();it!=compout[b].end();it++){
if(compin[a].find(*it)!=compin[a].end())
Q.push({getpar(a),getpar(*it)});
compout[a].insert(*it);
}
for(auto it=kompuc[b].begin();it!=kompuc[b].end();it++)
if(getpar(*it)!=a)
kompuc[a].insert(*it);
res+=vel[a]*(vel[a]-1);
res+=vel[a]*kompuc[a].size();
// cout<<vel[a]<<" "<<kompuc[a].size()<<endl;
return;
}
void odradi(int a,int b){
//cout<<a<<" "<<b<<endl;
int ca=getpar(a);
int cb=getpar(b);
if(ca==cb)
return;
if(cukomp[a].find(cb)!=cukomp[a].end())
return;
if(compout[cb].find(ca)!=compout[cb].end()){
// cout<<"SPAJAJ"<<endl;
spoj(ca,cb);
return;
}
res+=vel[cb];
cukomp[a].insert(cb);
kompuc[cb].insert(a);
compin[cb].insert(ca);
compout[ca].insert(cb);
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N>>M;
for(int i=1;i<=N;i++){
vel[i]=1;
rod[i]=i;
koji[i].push_back(i);
}
res=0;
int a,b;
while(M--){
cin>>a>>b;
odradi(a,b);
while(Q.size()){
spoj(Q.front().first,Q.front().second);
Q.pop();
}
cout<<res<<"\n";
}
return 0;
}
Compilation message
joitter2.cpp: In function 'void spoj(int, int)':
joitter2.cpp:55:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<koji[b].size();i++){
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21396 KB |
Output is correct |
12 |
Correct |
13 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21372 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21460 KB |
Output is correct |
19 |
Correct |
13 ms |
21436 KB |
Output is correct |
20 |
Correct |
10 ms |
21460 KB |
Output is correct |
21 |
Correct |
13 ms |
21416 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
12 ms |
21460 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
9 ms |
21460 KB |
Output is correct |
28 |
Correct |
9 ms |
21420 KB |
Output is correct |
29 |
Correct |
13 ms |
21632 KB |
Output is correct |
30 |
Correct |
10 ms |
21460 KB |
Output is correct |
31 |
Correct |
10 ms |
21412 KB |
Output is correct |
32 |
Correct |
9 ms |
21460 KB |
Output is correct |
33 |
Correct |
9 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21396 KB |
Output is correct |
12 |
Correct |
13 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21372 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21460 KB |
Output is correct |
19 |
Correct |
13 ms |
21436 KB |
Output is correct |
20 |
Correct |
10 ms |
21460 KB |
Output is correct |
21 |
Correct |
13 ms |
21416 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
12 ms |
21460 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
9 ms |
21460 KB |
Output is correct |
28 |
Correct |
9 ms |
21420 KB |
Output is correct |
29 |
Correct |
13 ms |
21632 KB |
Output is correct |
30 |
Correct |
10 ms |
21460 KB |
Output is correct |
31 |
Correct |
10 ms |
21412 KB |
Output is correct |
32 |
Correct |
9 ms |
21460 KB |
Output is correct |
33 |
Correct |
9 ms |
21460 KB |
Output is correct |
34 |
Correct |
14 ms |
21588 KB |
Output is correct |
35 |
Correct |
69 ms |
27724 KB |
Output is correct |
36 |
Correct |
101 ms |
31112 KB |
Output is correct |
37 |
Correct |
100 ms |
31292 KB |
Output is correct |
38 |
Correct |
90 ms |
30776 KB |
Output is correct |
39 |
Correct |
11 ms |
21716 KB |
Output is correct |
40 |
Correct |
11 ms |
21844 KB |
Output is correct |
41 |
Correct |
13 ms |
21844 KB |
Output is correct |
42 |
Correct |
11 ms |
21648 KB |
Output is correct |
43 |
Correct |
16 ms |
21844 KB |
Output is correct |
44 |
Correct |
13 ms |
21844 KB |
Output is correct |
45 |
Correct |
14 ms |
21716 KB |
Output is correct |
46 |
Correct |
11 ms |
21716 KB |
Output is correct |
47 |
Correct |
15 ms |
21880 KB |
Output is correct |
48 |
Correct |
13 ms |
21844 KB |
Output is correct |
49 |
Correct |
20 ms |
22868 KB |
Output is correct |
50 |
Correct |
103 ms |
31440 KB |
Output is correct |
51 |
Correct |
15 ms |
22228 KB |
Output is correct |
52 |
Correct |
88 ms |
29132 KB |
Output is correct |
53 |
Correct |
19 ms |
22764 KB |
Output is correct |
54 |
Correct |
86 ms |
30124 KB |
Output is correct |
55 |
Correct |
19 ms |
22356 KB |
Output is correct |
56 |
Correct |
15 ms |
22356 KB |
Output is correct |
57 |
Correct |
15 ms |
22376 KB |
Output is correct |
58 |
Correct |
14 ms |
22356 KB |
Output is correct |
59 |
Correct |
10 ms |
21716 KB |
Output is correct |
60 |
Correct |
63 ms |
26696 KB |
Output is correct |
61 |
Correct |
13 ms |
21972 KB |
Output is correct |
62 |
Correct |
92 ms |
30532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
13 ms |
21460 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21396 KB |
Output is correct |
12 |
Correct |
13 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21372 KB |
Output is correct |
15 |
Correct |
10 ms |
21460 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21460 KB |
Output is correct |
19 |
Correct |
13 ms |
21436 KB |
Output is correct |
20 |
Correct |
10 ms |
21460 KB |
Output is correct |
21 |
Correct |
13 ms |
21416 KB |
Output is correct |
22 |
Correct |
10 ms |
21460 KB |
Output is correct |
23 |
Correct |
11 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21460 KB |
Output is correct |
25 |
Correct |
12 ms |
21460 KB |
Output is correct |
26 |
Correct |
10 ms |
21460 KB |
Output is correct |
27 |
Correct |
9 ms |
21460 KB |
Output is correct |
28 |
Correct |
9 ms |
21420 KB |
Output is correct |
29 |
Correct |
13 ms |
21632 KB |
Output is correct |
30 |
Correct |
10 ms |
21460 KB |
Output is correct |
31 |
Correct |
10 ms |
21412 KB |
Output is correct |
32 |
Correct |
9 ms |
21460 KB |
Output is correct |
33 |
Correct |
9 ms |
21460 KB |
Output is correct |
34 |
Correct |
14 ms |
21588 KB |
Output is correct |
35 |
Correct |
69 ms |
27724 KB |
Output is correct |
36 |
Correct |
101 ms |
31112 KB |
Output is correct |
37 |
Correct |
100 ms |
31292 KB |
Output is correct |
38 |
Correct |
90 ms |
30776 KB |
Output is correct |
39 |
Correct |
11 ms |
21716 KB |
Output is correct |
40 |
Correct |
11 ms |
21844 KB |
Output is correct |
41 |
Correct |
13 ms |
21844 KB |
Output is correct |
42 |
Correct |
11 ms |
21648 KB |
Output is correct |
43 |
Correct |
16 ms |
21844 KB |
Output is correct |
44 |
Correct |
13 ms |
21844 KB |
Output is correct |
45 |
Correct |
14 ms |
21716 KB |
Output is correct |
46 |
Correct |
11 ms |
21716 KB |
Output is correct |
47 |
Correct |
15 ms |
21880 KB |
Output is correct |
48 |
Correct |
13 ms |
21844 KB |
Output is correct |
49 |
Correct |
20 ms |
22868 KB |
Output is correct |
50 |
Correct |
103 ms |
31440 KB |
Output is correct |
51 |
Correct |
15 ms |
22228 KB |
Output is correct |
52 |
Correct |
88 ms |
29132 KB |
Output is correct |
53 |
Correct |
19 ms |
22764 KB |
Output is correct |
54 |
Correct |
86 ms |
30124 KB |
Output is correct |
55 |
Correct |
19 ms |
22356 KB |
Output is correct |
56 |
Correct |
15 ms |
22356 KB |
Output is correct |
57 |
Correct |
15 ms |
22376 KB |
Output is correct |
58 |
Correct |
14 ms |
22356 KB |
Output is correct |
59 |
Correct |
10 ms |
21716 KB |
Output is correct |
60 |
Correct |
63 ms |
26696 KB |
Output is correct |
61 |
Correct |
13 ms |
21972 KB |
Output is correct |
62 |
Correct |
92 ms |
30532 KB |
Output is correct |
63 |
Correct |
545 ms |
87892 KB |
Output is correct |
64 |
Correct |
516 ms |
87944 KB |
Output is correct |
65 |
Correct |
549 ms |
87948 KB |
Output is correct |
66 |
Correct |
155 ms |
35604 KB |
Output is correct |
67 |
Correct |
286 ms |
45544 KB |
Output is correct |
68 |
Correct |
141 ms |
35652 KB |
Output is correct |
69 |
Correct |
409 ms |
41536 KB |
Output is correct |
70 |
Correct |
150 ms |
35656 KB |
Output is correct |
71 |
Correct |
143 ms |
35592 KB |
Output is correct |
72 |
Correct |
340 ms |
44336 KB |
Output is correct |
73 |
Correct |
368 ms |
44520 KB |
Output is correct |
74 |
Correct |
1061 ms |
75688 KB |
Output is correct |
75 |
Correct |
605 ms |
49948 KB |
Output is correct |
76 |
Correct |
751 ms |
63176 KB |
Output is correct |
77 |
Correct |
716 ms |
63408 KB |
Output is correct |
78 |
Correct |
235 ms |
44840 KB |
Output is correct |
79 |
Correct |
350 ms |
48968 KB |
Output is correct |
80 |
Correct |
225 ms |
43668 KB |
Output is correct |
81 |
Correct |
309 ms |
47044 KB |
Output is correct |
82 |
Correct |
941 ms |
69096 KB |
Output is correct |
83 |
Correct |
823 ms |
69100 KB |
Output is correct |
84 |
Correct |
639 ms |
68332 KB |
Output is correct |
85 |
Correct |
675 ms |
68308 KB |
Output is correct |
86 |
Correct |
176 ms |
39728 KB |
Output is correct |
87 |
Correct |
210 ms |
41740 KB |
Output is correct |
88 |
Correct |
396 ms |
44712 KB |
Output is correct |
89 |
Correct |
698 ms |
60744 KB |
Output is correct |