#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 3e5 + 5, N = 1e6;
const ll oo = 1e18 + 5, base = 311;
const int lg = 62, tx = 26;
const ll mod = 1e9 + 7;
#define pii pair<ll, ll>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n, m;
int sz[nmax];
set<int> in[nmax], out[nmax], trh[nmax];
set<pii> one[nmax], two[nmax];
int r[nmax];
int get(int u){
return r[u] ? r[u] = get(r[u]) : u;
}
ll ans = 0;
void Union(int u, int v){
int x = get(u);
int y = get(v);
if(x != y){
bool ok = 0;
if(in[x].size() + out[x].size() + one[x].size() + two[x].size() > in[y].size() + out[y].size() + one[y].size() + two[y].size()) swap(x, y);
ans -= sz[x] * (sz[x] - 1);
ans -= sz[y] * (sz[y] - 1);
int it = trh[x].size();
ans -= it * sz[x];
it = trh[y].size();
ans -= it * sz[y];
in[x].erase(y);
out[x].erase(y);
in[y].erase(x);
out[y].erase(x);
vector<int> lis;
for(auto p : in[x]){
if(out[y].count(p)) lis.push_back(p);
out[p].erase(x);
out[p].insert(y);
in[y].insert(p);
}
for(auto p : out[x]){
if(in[y].count(p)) lis.push_back(p);
in[p].erase(x);
out[y].insert(p);
in[p].insert(y);
}
for(auto p : one[x]){
if(get(p.fi) == y){
two[y].erase(p);
}
else{
one[y].insert(p);
trh[y].insert(p.fi);
}
}
for(auto p : two[x]){
if(get(p.se) == y){
one[y].erase(p);
pii it = *one[v].lower_bound({p.fi, -oo});
if(it.fi != p.fi) trh[y].erase(p.fi);
}
else{
two[y].insert(p);
}
}
sz[y] += sz[x];
r[x] = y;
it = trh[y].size();
ans += it * sz[y];
ans += sz[y] * (sz[y] - 1);
for(auto p : lis) Union(x, p);
}
}
bool check(int u, int v){
int x = get(u);
int y = get(v);
return out[x].count(y) > 0;
}
pii a[nmax];
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; ++i){
int u, v;
cin >> u >> v;
a[i] = {u, v};
}
for(int i = 1; i <= n; ++i) sz[i] = 1;
for(int i = 1; i <= m; ++i){
int u = a[i].fi, v = a[i].se;
if(get(u) == get(v)){
cout <<ans << endl;
continue;
}
u = get(u); v = get(v);
if(!check(v, u)){
pii it = *one[v].lower_bound({a[i].fi, -oo});
if(it.fi != a[i].fi) ans += sz[v];
in[v].insert(u);
out[u].insert(v);
one[v].insert(a[i]);
two[u].insert(a[i]);
trh[v].insert(a[i].fi);
}
else{
// cout << i << ' ';
Union(u, v);
}
cout <<ans << endl;
}
}
/*
3 4
3 1
1 2
2 3
1 3
*/
Compilation message
joitter2.cpp: In function 'void Union(long long int, long long int)':
joitter2.cpp:27:14: warning: unused variable 'ok' [-Wunused-variable]
27 | bool ok = 0;
| ^~
joitter2.cpp: At global scope:
joitter2.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
88 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
70744 KB |
Output is correct |
2 |
Correct |
32 ms |
70744 KB |
Output is correct |
3 |
Correct |
34 ms |
70844 KB |
Output is correct |
4 |
Correct |
38 ms |
70740 KB |
Output is correct |
5 |
Correct |
37 ms |
70848 KB |
Output is correct |
6 |
Correct |
35 ms |
70892 KB |
Output is correct |
7 |
Correct |
36 ms |
70992 KB |
Output is correct |
8 |
Correct |
36 ms |
71004 KB |
Output is correct |
9 |
Correct |
36 ms |
71004 KB |
Output is correct |
10 |
Correct |
32 ms |
70748 KB |
Output is correct |
11 |
Correct |
30 ms |
70800 KB |
Output is correct |
12 |
Correct |
33 ms |
70748 KB |
Output is correct |
13 |
Correct |
35 ms |
70676 KB |
Output is correct |
14 |
Correct |
32 ms |
70752 KB |
Output is correct |
15 |
Correct |
38 ms |
70748 KB |
Output is correct |
16 |
Correct |
32 ms |
70900 KB |
Output is correct |
17 |
Correct |
31 ms |
70740 KB |
Output is correct |
18 |
Correct |
32 ms |
70720 KB |
Output is correct |
19 |
Correct |
33 ms |
70744 KB |
Output is correct |
20 |
Correct |
30 ms |
70748 KB |
Output is correct |
21 |
Correct |
32 ms |
71000 KB |
Output is correct |
22 |
Correct |
39 ms |
70736 KB |
Output is correct |
23 |
Correct |
34 ms |
71004 KB |
Output is correct |
24 |
Correct |
32 ms |
70744 KB |
Output is correct |
25 |
Correct |
33 ms |
70996 KB |
Output is correct |
26 |
Correct |
32 ms |
70748 KB |
Output is correct |
27 |
Correct |
32 ms |
70744 KB |
Output is correct |
28 |
Correct |
33 ms |
70748 KB |
Output is correct |
29 |
Correct |
32 ms |
70740 KB |
Output is correct |
30 |
Correct |
37 ms |
70748 KB |
Output is correct |
31 |
Correct |
34 ms |
70748 KB |
Output is correct |
32 |
Correct |
30 ms |
70748 KB |
Output is correct |
33 |
Correct |
34 ms |
70972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
70744 KB |
Output is correct |
2 |
Correct |
32 ms |
70744 KB |
Output is correct |
3 |
Correct |
34 ms |
70844 KB |
Output is correct |
4 |
Correct |
38 ms |
70740 KB |
Output is correct |
5 |
Correct |
37 ms |
70848 KB |
Output is correct |
6 |
Correct |
35 ms |
70892 KB |
Output is correct |
7 |
Correct |
36 ms |
70992 KB |
Output is correct |
8 |
Correct |
36 ms |
71004 KB |
Output is correct |
9 |
Correct |
36 ms |
71004 KB |
Output is correct |
10 |
Correct |
32 ms |
70748 KB |
Output is correct |
11 |
Correct |
30 ms |
70800 KB |
Output is correct |
12 |
Correct |
33 ms |
70748 KB |
Output is correct |
13 |
Correct |
35 ms |
70676 KB |
Output is correct |
14 |
Correct |
32 ms |
70752 KB |
Output is correct |
15 |
Correct |
38 ms |
70748 KB |
Output is correct |
16 |
Correct |
32 ms |
70900 KB |
Output is correct |
17 |
Correct |
31 ms |
70740 KB |
Output is correct |
18 |
Correct |
32 ms |
70720 KB |
Output is correct |
19 |
Correct |
33 ms |
70744 KB |
Output is correct |
20 |
Correct |
30 ms |
70748 KB |
Output is correct |
21 |
Correct |
32 ms |
71000 KB |
Output is correct |
22 |
Correct |
39 ms |
70736 KB |
Output is correct |
23 |
Correct |
34 ms |
71004 KB |
Output is correct |
24 |
Correct |
32 ms |
70744 KB |
Output is correct |
25 |
Correct |
33 ms |
70996 KB |
Output is correct |
26 |
Correct |
32 ms |
70748 KB |
Output is correct |
27 |
Correct |
32 ms |
70744 KB |
Output is correct |
28 |
Correct |
33 ms |
70748 KB |
Output is correct |
29 |
Correct |
32 ms |
70740 KB |
Output is correct |
30 |
Correct |
37 ms |
70748 KB |
Output is correct |
31 |
Correct |
34 ms |
70748 KB |
Output is correct |
32 |
Correct |
30 ms |
70748 KB |
Output is correct |
33 |
Correct |
34 ms |
70972 KB |
Output is correct |
34 |
Correct |
42 ms |
71252 KB |
Output is correct |
35 |
Correct |
385 ms |
83028 KB |
Output is correct |
36 |
Correct |
409 ms |
88192 KB |
Output is correct |
37 |
Correct |
426 ms |
88400 KB |
Output is correct |
38 |
Correct |
427 ms |
87632 KB |
Output is correct |
39 |
Correct |
45 ms |
71260 KB |
Output is correct |
40 |
Correct |
35 ms |
71216 KB |
Output is correct |
41 |
Correct |
38 ms |
71256 KB |
Output is correct |
42 |
Correct |
37 ms |
71248 KB |
Output is correct |
43 |
Correct |
38 ms |
71252 KB |
Output is correct |
44 |
Correct |
39 ms |
71252 KB |
Output is correct |
45 |
Correct |
38 ms |
71248 KB |
Output is correct |
46 |
Correct |
39 ms |
71252 KB |
Output is correct |
47 |
Correct |
39 ms |
71252 KB |
Output is correct |
48 |
Correct |
39 ms |
71260 KB |
Output is correct |
49 |
Correct |
57 ms |
73296 KB |
Output is correct |
50 |
Correct |
435 ms |
88660 KB |
Output is correct |
51 |
Correct |
49 ms |
72020 KB |
Output is correct |
52 |
Correct |
412 ms |
84940 KB |
Output is correct |
53 |
Correct |
52 ms |
72916 KB |
Output is correct |
54 |
Correct |
400 ms |
86660 KB |
Output is correct |
55 |
Correct |
41 ms |
72020 KB |
Output is correct |
56 |
Correct |
41 ms |
72020 KB |
Output is correct |
57 |
Correct |
41 ms |
72532 KB |
Output is correct |
58 |
Correct |
40 ms |
72416 KB |
Output is correct |
59 |
Correct |
40 ms |
71248 KB |
Output is correct |
60 |
Correct |
387 ms |
80796 KB |
Output is correct |
61 |
Correct |
40 ms |
71504 KB |
Output is correct |
62 |
Correct |
398 ms |
87244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
70744 KB |
Output is correct |
2 |
Correct |
32 ms |
70744 KB |
Output is correct |
3 |
Correct |
34 ms |
70844 KB |
Output is correct |
4 |
Correct |
38 ms |
70740 KB |
Output is correct |
5 |
Correct |
37 ms |
70848 KB |
Output is correct |
6 |
Correct |
35 ms |
70892 KB |
Output is correct |
7 |
Correct |
36 ms |
70992 KB |
Output is correct |
8 |
Correct |
36 ms |
71004 KB |
Output is correct |
9 |
Correct |
36 ms |
71004 KB |
Output is correct |
10 |
Correct |
32 ms |
70748 KB |
Output is correct |
11 |
Correct |
30 ms |
70800 KB |
Output is correct |
12 |
Correct |
33 ms |
70748 KB |
Output is correct |
13 |
Correct |
35 ms |
70676 KB |
Output is correct |
14 |
Correct |
32 ms |
70752 KB |
Output is correct |
15 |
Correct |
38 ms |
70748 KB |
Output is correct |
16 |
Correct |
32 ms |
70900 KB |
Output is correct |
17 |
Correct |
31 ms |
70740 KB |
Output is correct |
18 |
Correct |
32 ms |
70720 KB |
Output is correct |
19 |
Correct |
33 ms |
70744 KB |
Output is correct |
20 |
Correct |
30 ms |
70748 KB |
Output is correct |
21 |
Correct |
32 ms |
71000 KB |
Output is correct |
22 |
Correct |
39 ms |
70736 KB |
Output is correct |
23 |
Correct |
34 ms |
71004 KB |
Output is correct |
24 |
Correct |
32 ms |
70744 KB |
Output is correct |
25 |
Correct |
33 ms |
70996 KB |
Output is correct |
26 |
Correct |
32 ms |
70748 KB |
Output is correct |
27 |
Correct |
32 ms |
70744 KB |
Output is correct |
28 |
Correct |
33 ms |
70748 KB |
Output is correct |
29 |
Correct |
32 ms |
70740 KB |
Output is correct |
30 |
Correct |
37 ms |
70748 KB |
Output is correct |
31 |
Correct |
34 ms |
70748 KB |
Output is correct |
32 |
Correct |
30 ms |
70748 KB |
Output is correct |
33 |
Correct |
34 ms |
70972 KB |
Output is correct |
34 |
Correct |
42 ms |
71252 KB |
Output is correct |
35 |
Correct |
385 ms |
83028 KB |
Output is correct |
36 |
Correct |
409 ms |
88192 KB |
Output is correct |
37 |
Correct |
426 ms |
88400 KB |
Output is correct |
38 |
Correct |
427 ms |
87632 KB |
Output is correct |
39 |
Correct |
45 ms |
71260 KB |
Output is correct |
40 |
Correct |
35 ms |
71216 KB |
Output is correct |
41 |
Correct |
38 ms |
71256 KB |
Output is correct |
42 |
Correct |
37 ms |
71248 KB |
Output is correct |
43 |
Correct |
38 ms |
71252 KB |
Output is correct |
44 |
Correct |
39 ms |
71252 KB |
Output is correct |
45 |
Correct |
38 ms |
71248 KB |
Output is correct |
46 |
Correct |
39 ms |
71252 KB |
Output is correct |
47 |
Correct |
39 ms |
71252 KB |
Output is correct |
48 |
Correct |
39 ms |
71260 KB |
Output is correct |
49 |
Correct |
57 ms |
73296 KB |
Output is correct |
50 |
Correct |
435 ms |
88660 KB |
Output is correct |
51 |
Correct |
49 ms |
72020 KB |
Output is correct |
52 |
Correct |
412 ms |
84940 KB |
Output is correct |
53 |
Correct |
52 ms |
72916 KB |
Output is correct |
54 |
Correct |
400 ms |
86660 KB |
Output is correct |
55 |
Correct |
41 ms |
72020 KB |
Output is correct |
56 |
Correct |
41 ms |
72020 KB |
Output is correct |
57 |
Correct |
41 ms |
72532 KB |
Output is correct |
58 |
Correct |
40 ms |
72416 KB |
Output is correct |
59 |
Correct |
40 ms |
71248 KB |
Output is correct |
60 |
Correct |
387 ms |
80796 KB |
Output is correct |
61 |
Correct |
40 ms |
71504 KB |
Output is correct |
62 |
Correct |
398 ms |
87244 KB |
Output is correct |
63 |
Correct |
821 ms |
161604 KB |
Output is correct |
64 |
Correct |
822 ms |
161476 KB |
Output is correct |
65 |
Correct |
832 ms |
161668 KB |
Output is correct |
66 |
Correct |
335 ms |
90960 KB |
Output is correct |
67 |
Correct |
431 ms |
98640 KB |
Output is correct |
68 |
Correct |
363 ms |
90976 KB |
Output is correct |
69 |
Correct |
521 ms |
95828 KB |
Output is correct |
70 |
Correct |
336 ms |
90928 KB |
Output is correct |
71 |
Correct |
328 ms |
90900 KB |
Output is correct |
72 |
Correct |
476 ms |
98640 KB |
Output is correct |
73 |
Correct |
462 ms |
98640 KB |
Output is correct |
74 |
Correct |
1113 ms |
153684 KB |
Output is correct |
75 |
Correct |
903 ms |
110476 KB |
Output is correct |
76 |
Correct |
943 ms |
130536 KB |
Output is correct |
77 |
Correct |
926 ms |
130880 KB |
Output is correct |
78 |
Correct |
377 ms |
101460 KB |
Output is correct |
79 |
Correct |
631 ms |
109700 KB |
Output is correct |
80 |
Correct |
382 ms |
100176 KB |
Output is correct |
81 |
Correct |
657 ms |
107092 KB |
Output is correct |
82 |
Correct |
946 ms |
136268 KB |
Output is correct |
83 |
Correct |
892 ms |
136276 KB |
Output is correct |
84 |
Correct |
937 ms |
153172 KB |
Output is correct |
85 |
Correct |
951 ms |
153412 KB |
Output is correct |
86 |
Correct |
335 ms |
89936 KB |
Output is correct |
87 |
Correct |
466 ms |
93780 KB |
Output is correct |
88 |
Correct |
463 ms |
99108 KB |
Output is correct |
89 |
Correct |
891 ms |
126772 KB |
Output is correct |