#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 100005
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007LL
int p[N], sz[N];
int find(int v){
return v == p[v] ? v : p[v] = find(p[v]);
}
ll res = 0;
multiset<pair<int, int> > on, in[N], con;
int cnt[N];
void insert(int v, int u){
int vv = find(v);
int uu = find(u);
if(vv == uu) return;
if(on.count({v, uu})) return;
if(!con.count({uu, vv})){
res += sz[uu];
con.insert({vv, uu});
on.insert({v, uu});
++cnt[uu];
in[vv].insert({v, uu});
in[uu].insert({v, uu});
return;
}
if(in[vv].size() < in[uu].size())
swap(vv, uu);
res -= 1LL * sz[vv] * (sz[vv] - 1);
res -= 1LL * sz[uu] * (sz[uu] - 1);
for(auto x : in[uu]){
res -= sz[x.ss];
con.erase(con.find({find(x.ff), x.ss}));
on.erase(x);
--cnt[x.ss];
if(x.ss == uu)
in[find(x.ff)].erase(x);
else
in[x.ss].erase(x);
}
p[uu] = vv;
res += cnt[vv] * sz[uu];
sz[vv] += sz[uu];
res += 1LL * sz[vv] * (sz[vv] - 1);
for(auto x : in[uu])
insert(x.ff, x.ss);
in[uu].clear();
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
for(int i = 1; i <= n; ++i){
p[i] = i;
sz[i] = 1;
}
while(m--){
int v, u;
cin>>v>>u;
insert(v, u);
cout<<res<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
8 ms |
4992 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
4992 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
8 ms |
5120 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
4992 KB |
Output is correct |
28 |
Correct |
7 ms |
5120 KB |
Output is correct |
29 |
Correct |
7 ms |
4992 KB |
Output is correct |
30 |
Correct |
7 ms |
4992 KB |
Output is correct |
31 |
Correct |
8 ms |
5120 KB |
Output is correct |
32 |
Correct |
7 ms |
4992 KB |
Output is correct |
33 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
8 ms |
4992 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
4992 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
8 ms |
5120 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
4992 KB |
Output is correct |
28 |
Correct |
7 ms |
5120 KB |
Output is correct |
29 |
Correct |
7 ms |
4992 KB |
Output is correct |
30 |
Correct |
7 ms |
4992 KB |
Output is correct |
31 |
Correct |
8 ms |
5120 KB |
Output is correct |
32 |
Correct |
7 ms |
4992 KB |
Output is correct |
33 |
Correct |
8 ms |
5120 KB |
Output is correct |
34 |
Correct |
12 ms |
5248 KB |
Output is correct |
35 |
Correct |
104 ms |
9044 KB |
Output is correct |
36 |
Correct |
154 ms |
12012 KB |
Output is correct |
37 |
Correct |
145 ms |
12152 KB |
Output is correct |
38 |
Correct |
150 ms |
11644 KB |
Output is correct |
39 |
Correct |
9 ms |
5120 KB |
Output is correct |
40 |
Correct |
12 ms |
5376 KB |
Output is correct |
41 |
Correct |
12 ms |
5248 KB |
Output is correct |
42 |
Correct |
9 ms |
5120 KB |
Output is correct |
43 |
Correct |
11 ms |
5248 KB |
Output is correct |
44 |
Correct |
11 ms |
5376 KB |
Output is correct |
45 |
Correct |
9 ms |
5120 KB |
Output is correct |
46 |
Correct |
11 ms |
5120 KB |
Output is correct |
47 |
Correct |
12 ms |
5248 KB |
Output is correct |
48 |
Correct |
14 ms |
5248 KB |
Output is correct |
49 |
Correct |
26 ms |
6144 KB |
Output is correct |
50 |
Correct |
143 ms |
12152 KB |
Output is correct |
51 |
Correct |
18 ms |
5632 KB |
Output is correct |
52 |
Correct |
115 ms |
10104 KB |
Output is correct |
53 |
Correct |
24 ms |
6016 KB |
Output is correct |
54 |
Correct |
128 ms |
11128 KB |
Output is correct |
55 |
Correct |
20 ms |
5888 KB |
Output is correct |
56 |
Correct |
17 ms |
5888 KB |
Output is correct |
57 |
Correct |
17 ms |
5888 KB |
Output is correct |
58 |
Correct |
17 ms |
5888 KB |
Output is correct |
59 |
Correct |
9 ms |
5120 KB |
Output is correct |
60 |
Correct |
96 ms |
7416 KB |
Output is correct |
61 |
Correct |
14 ms |
5376 KB |
Output is correct |
62 |
Correct |
138 ms |
11640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
4992 KB |
Output is correct |
6 |
Correct |
8 ms |
4992 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
7 ms |
4992 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
4992 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
4992 KB |
Output is correct |
23 |
Correct |
9 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
8 ms |
5120 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
4992 KB |
Output is correct |
28 |
Correct |
7 ms |
5120 KB |
Output is correct |
29 |
Correct |
7 ms |
4992 KB |
Output is correct |
30 |
Correct |
7 ms |
4992 KB |
Output is correct |
31 |
Correct |
8 ms |
5120 KB |
Output is correct |
32 |
Correct |
7 ms |
4992 KB |
Output is correct |
33 |
Correct |
8 ms |
5120 KB |
Output is correct |
34 |
Correct |
12 ms |
5248 KB |
Output is correct |
35 |
Correct |
104 ms |
9044 KB |
Output is correct |
36 |
Correct |
154 ms |
12012 KB |
Output is correct |
37 |
Correct |
145 ms |
12152 KB |
Output is correct |
38 |
Correct |
150 ms |
11644 KB |
Output is correct |
39 |
Correct |
9 ms |
5120 KB |
Output is correct |
40 |
Correct |
12 ms |
5376 KB |
Output is correct |
41 |
Correct |
12 ms |
5248 KB |
Output is correct |
42 |
Correct |
9 ms |
5120 KB |
Output is correct |
43 |
Correct |
11 ms |
5248 KB |
Output is correct |
44 |
Correct |
11 ms |
5376 KB |
Output is correct |
45 |
Correct |
9 ms |
5120 KB |
Output is correct |
46 |
Correct |
11 ms |
5120 KB |
Output is correct |
47 |
Correct |
12 ms |
5248 KB |
Output is correct |
48 |
Correct |
14 ms |
5248 KB |
Output is correct |
49 |
Correct |
26 ms |
6144 KB |
Output is correct |
50 |
Correct |
143 ms |
12152 KB |
Output is correct |
51 |
Correct |
18 ms |
5632 KB |
Output is correct |
52 |
Correct |
115 ms |
10104 KB |
Output is correct |
53 |
Correct |
24 ms |
6016 KB |
Output is correct |
54 |
Correct |
128 ms |
11128 KB |
Output is correct |
55 |
Correct |
20 ms |
5888 KB |
Output is correct |
56 |
Correct |
17 ms |
5888 KB |
Output is correct |
57 |
Correct |
17 ms |
5888 KB |
Output is correct |
58 |
Correct |
17 ms |
5888 KB |
Output is correct |
59 |
Correct |
9 ms |
5120 KB |
Output is correct |
60 |
Correct |
96 ms |
7416 KB |
Output is correct |
61 |
Correct |
14 ms |
5376 KB |
Output is correct |
62 |
Correct |
138 ms |
11640 KB |
Output is correct |
63 |
Correct |
1088 ms |
64580 KB |
Output is correct |
64 |
Correct |
1107 ms |
64888 KB |
Output is correct |
65 |
Correct |
1084 ms |
64636 KB |
Output is correct |
66 |
Correct |
108 ms |
8312 KB |
Output is correct |
67 |
Correct |
585 ms |
17016 KB |
Output is correct |
68 |
Correct |
110 ms |
8312 KB |
Output is correct |
69 |
Correct |
417 ms |
17656 KB |
Output is correct |
70 |
Correct |
121 ms |
8312 KB |
Output is correct |
71 |
Correct |
122 ms |
8312 KB |
Output is correct |
72 |
Correct |
660 ms |
17144 KB |
Output is correct |
73 |
Correct |
633 ms |
17144 KB |
Output is correct |
74 |
Correct |
2086 ms |
30284 KB |
Output is correct |
75 |
Correct |
856 ms |
23928 KB |
Output is correct |
76 |
Correct |
1531 ms |
29952 KB |
Output is correct |
77 |
Correct |
1520 ms |
30092 KB |
Output is correct |
78 |
Correct |
277 ms |
17144 KB |
Output is correct |
79 |
Correct |
548 ms |
19576 KB |
Output is correct |
80 |
Correct |
263 ms |
17072 KB |
Output is correct |
81 |
Correct |
420 ms |
18680 KB |
Output is correct |
82 |
Correct |
1330 ms |
45304 KB |
Output is correct |
83 |
Correct |
1355 ms |
45048 KB |
Output is correct |
84 |
Correct |
1324 ms |
45332 KB |
Output is correct |
85 |
Correct |
1275 ms |
45304 KB |
Output is correct |
86 |
Correct |
112 ms |
7544 KB |
Output is correct |
87 |
Correct |
163 ms |
8696 KB |
Output is correct |
88 |
Correct |
619 ms |
17144 KB |
Output is correct |
89 |
Correct |
1421 ms |
29304 KB |
Output is correct |