#include<iostream>
#include<set>
#include<vector>
#define DIM 100005
using namespace std;
int n, q, i, j, x, y, r1, r2, ry;
long long sol;
int r[DIM], num[DIM], nv[DIM];
vector<int> v[DIM], vr[DIM];
set<int> s[DIM], sr[DIM];
int rad(int x){
while(r[x] > 0){
x = r[x];
}
return x;
}
void uneste(int r1, int r2){
int i, j, x, y, rx, ry;
if(q == 1){
int abc = 0;
}
if(r[r1] > 0 || r[r2] > 0){
return;
}
if(r[r1] > r[r2]){
swap(r1, r2);
}
sol -= num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
sol -= num[r2] * 1LL * (num[r2] - 1) + num[r2] * 1LL * nv[r2];
for(i = 0; i < vr[r2].size(); i++){
x = vr[r2][i];
vr[r1].push_back(x);
if(s[x].find(r1) != s[x].end() ){
nv[r1]--;
s[x].erase(r1);
}
for(j = 0; j < v[x].size(); j++){
y = v[x][j];
ry = rad(y);
if(ry == r1 || ry == r2){
s[y].erase(r2);
continue;
}
if(s[y].find(r1) == s[y].end() ){
s[y].insert(r1);
nv[r1]++;
}
s[y].erase(r2);
sr[ry].insert(r1);
sr[ry].erase(r2);
}
}
set<int> :: iterator it;
for(it = sr[r2].begin(); it != sr[r2].end(); it++){
sr[r1].insert(*it);
}
r[r1] += r[r2];
r[r2] = r1;
num[r1] += num[r2];
sr[r1].erase(r2);
sol += num[r1] * 1LL * (num[r1] - 1) + num[r1] * 1LL * nv[r1];
for(it = sr[r2].begin(); it != sr[r2].end(); it++){
rx = rad(r1);
ry = rad(*it);
if(sr[ry].find(rx) != sr[ry].end() && rx != ry){
uneste(rx, ry);
}
}
for(i = 0; i < vr[r2].size(); i++){
x = vr[r2][i];
for(j = 0; j < v[x].size(); j++){
y = v[x][j];
rx = rad(r1);
ry = rad(y);
if(sr[rx].find(ry) != sr[rx].end() && rx != ry){
uneste(rx, ry);
}
}
}
}
int main(){
cin>> n >> q;
for(i = 1; i <= n; i++){
r[i] = -1;
num[i] = 1;
vr[i].push_back(i);
}
for(; q; q--){
cin>> x >> y;
v[y].push_back(x);
r1 = rad(x);
r2 = rad(y);
if(r1 == r2){
cout<< sol <<"\n";
continue;
}
if(s[x].find(r2) == s[x].end() ){
sol += num[r2];
nv[r2]++;
s[x].insert(r2);
}
sr[r1].insert(r2);
if(sr[r2].find(r1) != sr[r2].end() ){
uneste(r1, r2);
}
cout<< sol <<"\n";
}
}
Compilation message
joitter2.cpp: In function 'void uneste(int, int)':
joitter2.cpp:20:13: warning: unused variable 'abc' [-Wunused-variable]
int abc = 0;
^~~
joitter2.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < vr[r2].size(); i++){
~~^~~~~~~~~~~~~~~
joitter2.cpp:37:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < v[x].size(); j++){
~~^~~~~~~~~~~~~
joitter2.cpp:69:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < vr[r2].size(); i++){
~~^~~~~~~~~~~~~~~
joitter2.cpp:71:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < v[x].size(); j++){
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
20 ms |
14464 KB |
Output is correct |
8 |
Correct |
19 ms |
14464 KB |
Output is correct |
9 |
Correct |
19 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
20 ms |
14464 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
18 ms |
14508 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
13 ms |
14464 KB |
Output is correct |
28 |
Correct |
17 ms |
14464 KB |
Output is correct |
29 |
Correct |
13 ms |
14464 KB |
Output is correct |
30 |
Correct |
16 ms |
14464 KB |
Output is correct |
31 |
Correct |
18 ms |
14464 KB |
Output is correct |
32 |
Correct |
16 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
20 ms |
14464 KB |
Output is correct |
8 |
Correct |
19 ms |
14464 KB |
Output is correct |
9 |
Correct |
19 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
20 ms |
14464 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
18 ms |
14508 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
13 ms |
14464 KB |
Output is correct |
28 |
Correct |
17 ms |
14464 KB |
Output is correct |
29 |
Correct |
13 ms |
14464 KB |
Output is correct |
30 |
Correct |
16 ms |
14464 KB |
Output is correct |
31 |
Correct |
18 ms |
14464 KB |
Output is correct |
32 |
Correct |
16 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14464 KB |
Output is correct |
34 |
Correct |
38 ms |
14592 KB |
Output is correct |
35 |
Correct |
789 ms |
22136 KB |
Output is correct |
36 |
Correct |
845 ms |
24568 KB |
Output is correct |
37 |
Correct |
840 ms |
24572 KB |
Output is correct |
38 |
Correct |
822 ms |
24184 KB |
Output is correct |
39 |
Correct |
25 ms |
14720 KB |
Output is correct |
40 |
Correct |
27 ms |
14840 KB |
Output is correct |
41 |
Correct |
27 ms |
14848 KB |
Output is correct |
42 |
Correct |
29 ms |
14840 KB |
Output is correct |
43 |
Correct |
27 ms |
14840 KB |
Output is correct |
44 |
Correct |
26 ms |
14720 KB |
Output is correct |
45 |
Correct |
25 ms |
14720 KB |
Output is correct |
46 |
Correct |
25 ms |
14720 KB |
Output is correct |
47 |
Correct |
28 ms |
14848 KB |
Output is correct |
48 |
Correct |
27 ms |
14848 KB |
Output is correct |
49 |
Correct |
49 ms |
15452 KB |
Output is correct |
50 |
Correct |
836 ms |
24312 KB |
Output is correct |
51 |
Correct |
43 ms |
14976 KB |
Output is correct |
52 |
Correct |
843 ms |
23416 KB |
Output is correct |
53 |
Correct |
46 ms |
15352 KB |
Output is correct |
54 |
Correct |
824 ms |
23904 KB |
Output is correct |
55 |
Correct |
27 ms |
14968 KB |
Output is correct |
56 |
Correct |
26 ms |
14976 KB |
Output is correct |
57 |
Correct |
27 ms |
15232 KB |
Output is correct |
58 |
Correct |
26 ms |
15232 KB |
Output is correct |
59 |
Correct |
28 ms |
14840 KB |
Output is correct |
60 |
Correct |
844 ms |
22264 KB |
Output is correct |
61 |
Correct |
29 ms |
14848 KB |
Output is correct |
62 |
Correct |
822 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
20 ms |
14464 KB |
Output is correct |
8 |
Correct |
19 ms |
14464 KB |
Output is correct |
9 |
Correct |
19 ms |
14464 KB |
Output is correct |
10 |
Correct |
13 ms |
14464 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
14 ms |
14592 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
13 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
13 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
13 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
20 ms |
14464 KB |
Output is correct |
22 |
Correct |
14 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
18 ms |
14508 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
13 ms |
14464 KB |
Output is correct |
28 |
Correct |
17 ms |
14464 KB |
Output is correct |
29 |
Correct |
13 ms |
14464 KB |
Output is correct |
30 |
Correct |
16 ms |
14464 KB |
Output is correct |
31 |
Correct |
18 ms |
14464 KB |
Output is correct |
32 |
Correct |
16 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14464 KB |
Output is correct |
34 |
Correct |
38 ms |
14592 KB |
Output is correct |
35 |
Correct |
789 ms |
22136 KB |
Output is correct |
36 |
Correct |
845 ms |
24568 KB |
Output is correct |
37 |
Correct |
840 ms |
24572 KB |
Output is correct |
38 |
Correct |
822 ms |
24184 KB |
Output is correct |
39 |
Correct |
25 ms |
14720 KB |
Output is correct |
40 |
Correct |
27 ms |
14840 KB |
Output is correct |
41 |
Correct |
27 ms |
14848 KB |
Output is correct |
42 |
Correct |
29 ms |
14840 KB |
Output is correct |
43 |
Correct |
27 ms |
14840 KB |
Output is correct |
44 |
Correct |
26 ms |
14720 KB |
Output is correct |
45 |
Correct |
25 ms |
14720 KB |
Output is correct |
46 |
Correct |
25 ms |
14720 KB |
Output is correct |
47 |
Correct |
28 ms |
14848 KB |
Output is correct |
48 |
Correct |
27 ms |
14848 KB |
Output is correct |
49 |
Correct |
49 ms |
15452 KB |
Output is correct |
50 |
Correct |
836 ms |
24312 KB |
Output is correct |
51 |
Correct |
43 ms |
14976 KB |
Output is correct |
52 |
Correct |
843 ms |
23416 KB |
Output is correct |
53 |
Correct |
46 ms |
15352 KB |
Output is correct |
54 |
Correct |
824 ms |
23904 KB |
Output is correct |
55 |
Correct |
27 ms |
14968 KB |
Output is correct |
56 |
Correct |
26 ms |
14976 KB |
Output is correct |
57 |
Correct |
27 ms |
15232 KB |
Output is correct |
58 |
Correct |
26 ms |
15232 KB |
Output is correct |
59 |
Correct |
28 ms |
14840 KB |
Output is correct |
60 |
Correct |
844 ms |
22264 KB |
Output is correct |
61 |
Correct |
29 ms |
14848 KB |
Output is correct |
62 |
Correct |
822 ms |
24056 KB |
Output is correct |
63 |
Correct |
1240 ms |
55904 KB |
Output is correct |
64 |
Correct |
1252 ms |
56084 KB |
Output is correct |
65 |
Correct |
1316 ms |
55776 KB |
Output is correct |
66 |
Correct |
753 ms |
31444 KB |
Output is correct |
67 |
Correct |
1158 ms |
37344 KB |
Output is correct |
68 |
Correct |
701 ms |
31844 KB |
Output is correct |
69 |
Correct |
879 ms |
32608 KB |
Output is correct |
70 |
Correct |
754 ms |
31472 KB |
Output is correct |
71 |
Correct |
746 ms |
31472 KB |
Output is correct |
72 |
Correct |
1043 ms |
35316 KB |
Output is correct |
73 |
Correct |
1032 ms |
35676 KB |
Output is correct |
74 |
Correct |
1760 ms |
51184 KB |
Output is correct |
75 |
Correct |
1340 ms |
37364 KB |
Output is correct |
76 |
Correct |
1574 ms |
44456 KB |
Output is correct |
77 |
Correct |
1507 ms |
44568 KB |
Output is correct |
78 |
Correct |
670 ms |
32756 KB |
Output is correct |
79 |
Correct |
1247 ms |
36368 KB |
Output is correct |
80 |
Correct |
560 ms |
32496 KB |
Output is correct |
81 |
Correct |
1068 ms |
36924 KB |
Output is correct |
82 |
Correct |
1046 ms |
45192 KB |
Output is correct |
83 |
Correct |
1057 ms |
45124 KB |
Output is correct |
84 |
Correct |
1020 ms |
52712 KB |
Output is correct |
85 |
Correct |
979 ms |
52728 KB |
Output is correct |
86 |
Correct |
973 ms |
38256 KB |
Output is correct |
87 |
Correct |
1286 ms |
40684 KB |
Output is correct |
88 |
Correct |
1032 ms |
35444 KB |
Output is correct |
89 |
Correct |
1456 ms |
43036 KB |
Output is correct |