#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 300000;
int v[1 + nmax][2];
ll total = 0;
void _add(set<int> &myset1, set<int> &myset2){
if(myset1.size() < myset2.size())
swap(myset1, myset2);
for(set<int>::iterator it = myset2.begin(); it != myset2.end(); it++)
myset1.insert(*it);
}
namespace Dsu{
vector<int> mult;
vector<int> sz;
set<int> followers[1 + nmax];
void initialize(int n){
mult.resize(1 + n);
sz.resize(1 + n);
for(int i = 1;i <= n; i++) {
mult[i] = i;
sz[i] = 1;
followers[i].insert(i);
}
}
int jump(int gala){
if(mult[gala] != gala)
mult[gala] = jump(mult[gala]);
return mult[gala];
}
ll extract(int gala){
return 1LL * sz[gala] * followers[gala].size();
}
void unite(int gala, int galb){
gala = jump(gala);
galb = jump(galb);
if(gala == galb)
return ;
if(sz[galb] < sz[gala])
swap(gala, galb);
total += - extract(gala) - extract(galb);
sz[galb] += sz[gala];
mult[gala] = galb;
_add(followers[galb], followers[gala]);
total += extract(galb);
}
void addfollow(int x, int y){
y = jump(y);
total -= extract(y);
followers[y].insert(x);
total += extract(y);
}
}
namespace Dsu2{
vector<int> mult;
set<int> adj[1 + nmax];
set<pair<int,int>> freq;
void initialize(int n){
mult.resize(1 + n);
for(int i = 1; i <= n; i++)
mult[i] = i;
}
int jump(int gala) {
if(mult[gala] != gala)
mult[gala] = jump(mult[gala]);
return mult[gala];
}
vector<int> st;
void unite(int gala, int galb){
gala = jump(gala);
galb = jump(galb);
if(gala == galb)
return ;
Dsu::unite(gala, galb);
if(adj[galb].size() < adj[gala].size())
swap(gala, galb);
for(set<int>::iterator it = adj[gala].begin(); it != adj[gala].end(); it++)
st.push_back(*it);
_add(adj[galb], adj[gala]);
mult[gala] = galb;
}
void refresh(){
while(0 < st.size()){
int id = st.back();
int x = jump(v[id][0]);
int y = jump(v[id][1]);
adj[x].insert(id);
adj[y].insert(id);
freq.insert({x, y});
st.pop_back();
if(freq.find({y, x}) != freq.end())
unite(x, y);
}
}
}
void activate(int id){
int x = v[id][0];
int y = v[id][1];
Dsu::addfollow(x, y);
Dsu2::st.push_back(id);
Dsu2::refresh();
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
total = n;
for(int i = 1;i <= q; i++)
cin >> v[i][0] >> v[i][1];
Dsu::initialize(n);
Dsu2::initialize(n);
for(int i = 1;i <= q; i++){
activate(i);
cout << total - n<< '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28544 KB |
Output is correct |
2 |
Correct |
20 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
20 ms |
28544 KB |
Output is correct |
5 |
Correct |
20 ms |
28672 KB |
Output is correct |
6 |
Correct |
20 ms |
28544 KB |
Output is correct |
7 |
Correct |
22 ms |
28672 KB |
Output is correct |
8 |
Correct |
22 ms |
28672 KB |
Output is correct |
9 |
Correct |
22 ms |
28672 KB |
Output is correct |
10 |
Correct |
21 ms |
28544 KB |
Output is correct |
11 |
Correct |
20 ms |
28544 KB |
Output is correct |
12 |
Correct |
20 ms |
28544 KB |
Output is correct |
13 |
Correct |
21 ms |
28544 KB |
Output is correct |
14 |
Correct |
23 ms |
28544 KB |
Output is correct |
15 |
Correct |
20 ms |
28544 KB |
Output is correct |
16 |
Correct |
20 ms |
28544 KB |
Output is correct |
17 |
Correct |
20 ms |
28544 KB |
Output is correct |
18 |
Correct |
20 ms |
28544 KB |
Output is correct |
19 |
Correct |
20 ms |
28544 KB |
Output is correct |
20 |
Correct |
21 ms |
28544 KB |
Output is correct |
21 |
Correct |
22 ms |
28672 KB |
Output is correct |
22 |
Correct |
21 ms |
28544 KB |
Output is correct |
23 |
Correct |
22 ms |
28672 KB |
Output is correct |
24 |
Correct |
20 ms |
28544 KB |
Output is correct |
25 |
Correct |
16 ms |
28672 KB |
Output is correct |
26 |
Correct |
20 ms |
28544 KB |
Output is correct |
27 |
Correct |
20 ms |
28544 KB |
Output is correct |
28 |
Correct |
20 ms |
28544 KB |
Output is correct |
29 |
Correct |
20 ms |
28544 KB |
Output is correct |
30 |
Correct |
21 ms |
28544 KB |
Output is correct |
31 |
Correct |
21 ms |
28672 KB |
Output is correct |
32 |
Correct |
20 ms |
28544 KB |
Output is correct |
33 |
Correct |
23 ms |
28672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28544 KB |
Output is correct |
2 |
Correct |
20 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
20 ms |
28544 KB |
Output is correct |
5 |
Correct |
20 ms |
28672 KB |
Output is correct |
6 |
Correct |
20 ms |
28544 KB |
Output is correct |
7 |
Correct |
22 ms |
28672 KB |
Output is correct |
8 |
Correct |
22 ms |
28672 KB |
Output is correct |
9 |
Correct |
22 ms |
28672 KB |
Output is correct |
10 |
Correct |
21 ms |
28544 KB |
Output is correct |
11 |
Correct |
20 ms |
28544 KB |
Output is correct |
12 |
Correct |
20 ms |
28544 KB |
Output is correct |
13 |
Correct |
21 ms |
28544 KB |
Output is correct |
14 |
Correct |
23 ms |
28544 KB |
Output is correct |
15 |
Correct |
20 ms |
28544 KB |
Output is correct |
16 |
Correct |
20 ms |
28544 KB |
Output is correct |
17 |
Correct |
20 ms |
28544 KB |
Output is correct |
18 |
Correct |
20 ms |
28544 KB |
Output is correct |
19 |
Correct |
20 ms |
28544 KB |
Output is correct |
20 |
Correct |
21 ms |
28544 KB |
Output is correct |
21 |
Correct |
22 ms |
28672 KB |
Output is correct |
22 |
Correct |
21 ms |
28544 KB |
Output is correct |
23 |
Correct |
22 ms |
28672 KB |
Output is correct |
24 |
Correct |
20 ms |
28544 KB |
Output is correct |
25 |
Correct |
16 ms |
28672 KB |
Output is correct |
26 |
Correct |
20 ms |
28544 KB |
Output is correct |
27 |
Correct |
20 ms |
28544 KB |
Output is correct |
28 |
Correct |
20 ms |
28544 KB |
Output is correct |
29 |
Correct |
20 ms |
28544 KB |
Output is correct |
30 |
Correct |
21 ms |
28544 KB |
Output is correct |
31 |
Correct |
21 ms |
28672 KB |
Output is correct |
32 |
Correct |
20 ms |
28544 KB |
Output is correct |
33 |
Correct |
23 ms |
28672 KB |
Output is correct |
34 |
Correct |
26 ms |
29184 KB |
Output is correct |
35 |
Correct |
289 ms |
51064 KB |
Output is correct |
36 |
Correct |
351 ms |
55288 KB |
Output is correct |
37 |
Correct |
356 ms |
55160 KB |
Output is correct |
38 |
Correct |
353 ms |
54648 KB |
Output is correct |
39 |
Correct |
26 ms |
29440 KB |
Output is correct |
40 |
Correct |
34 ms |
30584 KB |
Output is correct |
41 |
Correct |
35 ms |
30584 KB |
Output is correct |
42 |
Correct |
27 ms |
29560 KB |
Output is correct |
43 |
Correct |
28 ms |
29568 KB |
Output is correct |
44 |
Correct |
28 ms |
29568 KB |
Output is correct |
45 |
Correct |
28 ms |
29440 KB |
Output is correct |
46 |
Correct |
27 ms |
29440 KB |
Output is correct |
47 |
Correct |
33 ms |
30072 KB |
Output is correct |
48 |
Correct |
33 ms |
30200 KB |
Output is correct |
49 |
Correct |
47 ms |
31224 KB |
Output is correct |
50 |
Correct |
349 ms |
55284 KB |
Output is correct |
51 |
Correct |
37 ms |
30336 KB |
Output is correct |
52 |
Correct |
337 ms |
52960 KB |
Output is correct |
53 |
Correct |
45 ms |
30976 KB |
Output is correct |
54 |
Correct |
339 ms |
54136 KB |
Output is correct |
55 |
Correct |
33 ms |
29952 KB |
Output is correct |
56 |
Correct |
31 ms |
29952 KB |
Output is correct |
57 |
Correct |
31 ms |
29952 KB |
Output is correct |
58 |
Correct |
31 ms |
29952 KB |
Output is correct |
59 |
Correct |
34 ms |
30848 KB |
Output is correct |
60 |
Correct |
270 ms |
51960 KB |
Output is correct |
61 |
Correct |
32 ms |
30080 KB |
Output is correct |
62 |
Correct |
342 ms |
54520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28544 KB |
Output is correct |
2 |
Correct |
20 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
20 ms |
28544 KB |
Output is correct |
5 |
Correct |
20 ms |
28672 KB |
Output is correct |
6 |
Correct |
20 ms |
28544 KB |
Output is correct |
7 |
Correct |
22 ms |
28672 KB |
Output is correct |
8 |
Correct |
22 ms |
28672 KB |
Output is correct |
9 |
Correct |
22 ms |
28672 KB |
Output is correct |
10 |
Correct |
21 ms |
28544 KB |
Output is correct |
11 |
Correct |
20 ms |
28544 KB |
Output is correct |
12 |
Correct |
20 ms |
28544 KB |
Output is correct |
13 |
Correct |
21 ms |
28544 KB |
Output is correct |
14 |
Correct |
23 ms |
28544 KB |
Output is correct |
15 |
Correct |
20 ms |
28544 KB |
Output is correct |
16 |
Correct |
20 ms |
28544 KB |
Output is correct |
17 |
Correct |
20 ms |
28544 KB |
Output is correct |
18 |
Correct |
20 ms |
28544 KB |
Output is correct |
19 |
Correct |
20 ms |
28544 KB |
Output is correct |
20 |
Correct |
21 ms |
28544 KB |
Output is correct |
21 |
Correct |
22 ms |
28672 KB |
Output is correct |
22 |
Correct |
21 ms |
28544 KB |
Output is correct |
23 |
Correct |
22 ms |
28672 KB |
Output is correct |
24 |
Correct |
20 ms |
28544 KB |
Output is correct |
25 |
Correct |
16 ms |
28672 KB |
Output is correct |
26 |
Correct |
20 ms |
28544 KB |
Output is correct |
27 |
Correct |
20 ms |
28544 KB |
Output is correct |
28 |
Correct |
20 ms |
28544 KB |
Output is correct |
29 |
Correct |
20 ms |
28544 KB |
Output is correct |
30 |
Correct |
21 ms |
28544 KB |
Output is correct |
31 |
Correct |
21 ms |
28672 KB |
Output is correct |
32 |
Correct |
20 ms |
28544 KB |
Output is correct |
33 |
Correct |
23 ms |
28672 KB |
Output is correct |
34 |
Correct |
26 ms |
29184 KB |
Output is correct |
35 |
Correct |
289 ms |
51064 KB |
Output is correct |
36 |
Correct |
351 ms |
55288 KB |
Output is correct |
37 |
Correct |
356 ms |
55160 KB |
Output is correct |
38 |
Correct |
353 ms |
54648 KB |
Output is correct |
39 |
Correct |
26 ms |
29440 KB |
Output is correct |
40 |
Correct |
34 ms |
30584 KB |
Output is correct |
41 |
Correct |
35 ms |
30584 KB |
Output is correct |
42 |
Correct |
27 ms |
29560 KB |
Output is correct |
43 |
Correct |
28 ms |
29568 KB |
Output is correct |
44 |
Correct |
28 ms |
29568 KB |
Output is correct |
45 |
Correct |
28 ms |
29440 KB |
Output is correct |
46 |
Correct |
27 ms |
29440 KB |
Output is correct |
47 |
Correct |
33 ms |
30072 KB |
Output is correct |
48 |
Correct |
33 ms |
30200 KB |
Output is correct |
49 |
Correct |
47 ms |
31224 KB |
Output is correct |
50 |
Correct |
349 ms |
55284 KB |
Output is correct |
51 |
Correct |
37 ms |
30336 KB |
Output is correct |
52 |
Correct |
337 ms |
52960 KB |
Output is correct |
53 |
Correct |
45 ms |
30976 KB |
Output is correct |
54 |
Correct |
339 ms |
54136 KB |
Output is correct |
55 |
Correct |
33 ms |
29952 KB |
Output is correct |
56 |
Correct |
31 ms |
29952 KB |
Output is correct |
57 |
Correct |
31 ms |
29952 KB |
Output is correct |
58 |
Correct |
31 ms |
29952 KB |
Output is correct |
59 |
Correct |
34 ms |
30848 KB |
Output is correct |
60 |
Correct |
270 ms |
51960 KB |
Output is correct |
61 |
Correct |
32 ms |
30080 KB |
Output is correct |
62 |
Correct |
342 ms |
54520 KB |
Output is correct |
63 |
Correct |
1094 ms |
98876 KB |
Output is correct |
64 |
Correct |
1099 ms |
98552 KB |
Output is correct |
65 |
Correct |
1086 ms |
98808 KB |
Output is correct |
66 |
Correct |
640 ms |
78104 KB |
Output is correct |
67 |
Correct |
1661 ms |
156392 KB |
Output is correct |
68 |
Correct |
608 ms |
78200 KB |
Output is correct |
69 |
Correct |
986 ms |
78200 KB |
Output is correct |
70 |
Correct |
684 ms |
78232 KB |
Output is correct |
71 |
Correct |
716 ms |
78164 KB |
Output is correct |
72 |
Correct |
1626 ms |
108116 KB |
Output is correct |
73 |
Correct |
1594 ms |
113184 KB |
Output is correct |
74 |
Correct |
2866 ms |
151260 KB |
Output is correct |
75 |
Correct |
1510 ms |
93920 KB |
Output is correct |
76 |
Correct |
2297 ms |
119172 KB |
Output is correct |
77 |
Correct |
2250 ms |
119416 KB |
Output is correct |
78 |
Correct |
511 ms |
71292 KB |
Output is correct |
79 |
Correct |
1149 ms |
92640 KB |
Output is correct |
80 |
Correct |
524 ms |
70308 KB |
Output is correct |
81 |
Correct |
1126 ms |
89716 KB |
Output is correct |
82 |
Correct |
1631 ms |
100856 KB |
Output is correct |
83 |
Correct |
1694 ms |
100856 KB |
Output is correct |
84 |
Correct |
1416 ms |
101492 KB |
Output is correct |
85 |
Correct |
1416 ms |
101484 KB |
Output is correct |
86 |
Correct |
1324 ms |
180592 KB |
Output is correct |
87 |
Correct |
1482 ms |
188528 KB |
Output is correct |
88 |
Correct |
1683 ms |
110868 KB |
Output is correct |
89 |
Correct |
2218 ms |
116188 KB |
Output is correct |