#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define pb push_back
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y){cerr<<x<<", "; _do(y...);}
#define bug(...) cerr<<"#"<<__LINE__<<"- "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__)
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 1e5+5;
int par[maxn];
int sz[maxn];
set<int> here[maxn];
set<int> ing[maxn];
set<int> outg[maxn]; // which sets point out of me
set<int> intopt[maxn]; // which individual points point into me
int find(int x) {
return x == par[x]? x : par[x] = find(par[x]);
}
int re = 0;
queue<pii> tomerge;
void mrg(int A, int B){
A = find(A); B= find(B);
if (A == B) return;
bug(A,B);
re -= SZ(intopt[B]) * sz[B] + SZ(intopt[A]) * sz[A];
re -= (sz[B]) * (sz[B]-1) + (sz[A] * (sz[A]-1));
if (SZ(here[B]) < SZ(intopt[A])) {
for (int x : here[B]) {
if (intopt[A].count(x)) intopt[A].erase(x);
}
}else{
set<int> nset;
for( int x : intopt[A]) {
if (!here[B].count(x)) {
nset.insert(x);
}
}
intopt[A].swap(nset);
}
if (SZ(here[A]) < SZ(intopt[B])) {
for (int x : here[A]) {
if (intopt[B].count(x)) intopt[B].erase(x);
}
}else{
set<int> nset;
for( int x : intopt[B]) {
if (!here[A].count(x)) {
nset.insert(x);
}
}
intopt[B].swap(nset);
}
if (SZ(intopt[A]) + SZ(here[A]) + SZ(outg[A]) + SZ(ing[A]) > SZ(intopt[B]) + SZ(here[B]) + SZ(outg[B]) + SZ(ing[B])) {
swap(A,B); //swapped = 1;
}
// a into b
for (int x : intopt[A]) {
intopt[B].insert(x);
}
for (int x : here[A]) {
here[B].insert(x);
}
for (int x : outg[A]) {
if (x != B) {
outg[B].insert(x);
ing[x].erase(A); ing[x].insert(B);
if (ing[B].count(x)) {
bug("bruh? ", A, B, x);
tomerge.push({B,x});
}
}
}
for (int x : ing[A]) {
if (x != B) {
ing[B].insert(x);
outg[x].erase(A); outg[x].insert(B);
if (outg[B].count(x)) {
bug("yo? ", A, B, x);
tomerge.push({B,x});
}
}
}
outg[B].erase(A);
ing[B].erase(A);
sz[B] += sz[A];
par[A] = B;
bug(SZ(intopt[B]));
re += SZ(intopt[B]) * sz[B];
re += (sz[B] * (sz[B]-1));
bug(re);
}
void add(int a, int b) { // edge from A to B
int A = find(a), B = find(b);
if (A == B) return;
if (intopt[B].count(a)) return;
if (outg[B].count(A)) {
// merge sequence!
bool swapped = 0;
tomerge.push({A,B});
while (!tomerge.empty()) {
mrg(tomerge.front().f, tomerge.front().s);
tomerge.pop();
}
}else{
ing[B].insert(A);
outg[A].insert(B);
intopt[B].insert(a);
re += sz[B];
}
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,m; cin>>n>>m;
REP(i,n) {
par[i] = i; sz[i] = 1; here[i].insert(i);
}
REP(i,m) {
int a,b; cin>>a>>b; --a; --b;
add(a,b);
cout<<re<<endl;
}
}
Compilation message
joitter2.cpp: In function 'void add(int, int)':
joitter2.cpp:130:14: warning: unused variable 'swapped' [-Wunused-variable]
130 | bool swapped = 0;
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19104 KB |
Output is correct |
2 |
Correct |
10 ms |
19084 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
13 ms |
19192 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19112 KB |
Output is correct |
11 |
Correct |
10 ms |
19124 KB |
Output is correct |
12 |
Correct |
10 ms |
19028 KB |
Output is correct |
13 |
Correct |
11 ms |
19108 KB |
Output is correct |
14 |
Correct |
9 ms |
19020 KB |
Output is correct |
15 |
Correct |
9 ms |
19064 KB |
Output is correct |
16 |
Correct |
9 ms |
19020 KB |
Output is correct |
17 |
Correct |
9 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
9 ms |
19108 KB |
Output is correct |
20 |
Correct |
9 ms |
19016 KB |
Output is correct |
21 |
Correct |
10 ms |
19120 KB |
Output is correct |
22 |
Correct |
13 ms |
19112 KB |
Output is correct |
23 |
Correct |
10 ms |
19148 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
13 ms |
19148 KB |
Output is correct |
26 |
Correct |
10 ms |
19020 KB |
Output is correct |
27 |
Correct |
10 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19020 KB |
Output is correct |
29 |
Correct |
9 ms |
19020 KB |
Output is correct |
30 |
Correct |
10 ms |
19108 KB |
Output is correct |
31 |
Correct |
12 ms |
19120 KB |
Output is correct |
32 |
Correct |
9 ms |
19020 KB |
Output is correct |
33 |
Correct |
10 ms |
19112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19104 KB |
Output is correct |
2 |
Correct |
10 ms |
19084 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
13 ms |
19192 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19112 KB |
Output is correct |
11 |
Correct |
10 ms |
19124 KB |
Output is correct |
12 |
Correct |
10 ms |
19028 KB |
Output is correct |
13 |
Correct |
11 ms |
19108 KB |
Output is correct |
14 |
Correct |
9 ms |
19020 KB |
Output is correct |
15 |
Correct |
9 ms |
19064 KB |
Output is correct |
16 |
Correct |
9 ms |
19020 KB |
Output is correct |
17 |
Correct |
9 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
9 ms |
19108 KB |
Output is correct |
20 |
Correct |
9 ms |
19016 KB |
Output is correct |
21 |
Correct |
10 ms |
19120 KB |
Output is correct |
22 |
Correct |
13 ms |
19112 KB |
Output is correct |
23 |
Correct |
10 ms |
19148 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
13 ms |
19148 KB |
Output is correct |
26 |
Correct |
10 ms |
19020 KB |
Output is correct |
27 |
Correct |
10 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19020 KB |
Output is correct |
29 |
Correct |
9 ms |
19020 KB |
Output is correct |
30 |
Correct |
10 ms |
19108 KB |
Output is correct |
31 |
Correct |
12 ms |
19120 KB |
Output is correct |
32 |
Correct |
9 ms |
19020 KB |
Output is correct |
33 |
Correct |
10 ms |
19112 KB |
Output is correct |
34 |
Correct |
12 ms |
19276 KB |
Output is correct |
35 |
Correct |
85 ms |
24928 KB |
Output is correct |
36 |
Correct |
108 ms |
27856 KB |
Output is correct |
37 |
Correct |
107 ms |
28064 KB |
Output is correct |
38 |
Correct |
98 ms |
27464 KB |
Output is correct |
39 |
Correct |
11 ms |
19384 KB |
Output is correct |
40 |
Correct |
12 ms |
19788 KB |
Output is correct |
41 |
Correct |
14 ms |
19748 KB |
Output is correct |
42 |
Correct |
12 ms |
19404 KB |
Output is correct |
43 |
Correct |
13 ms |
19404 KB |
Output is correct |
44 |
Correct |
13 ms |
19404 KB |
Output is correct |
45 |
Correct |
11 ms |
19380 KB |
Output is correct |
46 |
Correct |
11 ms |
19380 KB |
Output is correct |
47 |
Correct |
13 ms |
19676 KB |
Output is correct |
48 |
Correct |
13 ms |
19632 KB |
Output is correct |
49 |
Correct |
19 ms |
20368 KB |
Output is correct |
50 |
Correct |
107 ms |
28100 KB |
Output is correct |
51 |
Correct |
19 ms |
19736 KB |
Output is correct |
52 |
Correct |
90 ms |
26152 KB |
Output is correct |
53 |
Correct |
19 ms |
20300 KB |
Output is correct |
54 |
Correct |
100 ms |
27088 KB |
Output is correct |
55 |
Correct |
15 ms |
19916 KB |
Output is correct |
56 |
Correct |
20 ms |
19892 KB |
Output is correct |
57 |
Correct |
15 ms |
19900 KB |
Output is correct |
58 |
Correct |
15 ms |
19892 KB |
Output is correct |
59 |
Correct |
16 ms |
19788 KB |
Output is correct |
60 |
Correct |
79 ms |
24772 KB |
Output is correct |
61 |
Correct |
13 ms |
19644 KB |
Output is correct |
62 |
Correct |
108 ms |
27436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19104 KB |
Output is correct |
2 |
Correct |
10 ms |
19084 KB |
Output is correct |
3 |
Correct |
9 ms |
19020 KB |
Output is correct |
4 |
Correct |
9 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
10 ms |
19148 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
13 ms |
19192 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
12 ms |
19112 KB |
Output is correct |
11 |
Correct |
10 ms |
19124 KB |
Output is correct |
12 |
Correct |
10 ms |
19028 KB |
Output is correct |
13 |
Correct |
11 ms |
19108 KB |
Output is correct |
14 |
Correct |
9 ms |
19020 KB |
Output is correct |
15 |
Correct |
9 ms |
19064 KB |
Output is correct |
16 |
Correct |
9 ms |
19020 KB |
Output is correct |
17 |
Correct |
9 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
9 ms |
19108 KB |
Output is correct |
20 |
Correct |
9 ms |
19016 KB |
Output is correct |
21 |
Correct |
10 ms |
19120 KB |
Output is correct |
22 |
Correct |
13 ms |
19112 KB |
Output is correct |
23 |
Correct |
10 ms |
19148 KB |
Output is correct |
24 |
Correct |
13 ms |
19148 KB |
Output is correct |
25 |
Correct |
13 ms |
19148 KB |
Output is correct |
26 |
Correct |
10 ms |
19020 KB |
Output is correct |
27 |
Correct |
10 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19020 KB |
Output is correct |
29 |
Correct |
9 ms |
19020 KB |
Output is correct |
30 |
Correct |
10 ms |
19108 KB |
Output is correct |
31 |
Correct |
12 ms |
19120 KB |
Output is correct |
32 |
Correct |
9 ms |
19020 KB |
Output is correct |
33 |
Correct |
10 ms |
19112 KB |
Output is correct |
34 |
Correct |
12 ms |
19276 KB |
Output is correct |
35 |
Correct |
85 ms |
24928 KB |
Output is correct |
36 |
Correct |
108 ms |
27856 KB |
Output is correct |
37 |
Correct |
107 ms |
28064 KB |
Output is correct |
38 |
Correct |
98 ms |
27464 KB |
Output is correct |
39 |
Correct |
11 ms |
19384 KB |
Output is correct |
40 |
Correct |
12 ms |
19788 KB |
Output is correct |
41 |
Correct |
14 ms |
19748 KB |
Output is correct |
42 |
Correct |
12 ms |
19404 KB |
Output is correct |
43 |
Correct |
13 ms |
19404 KB |
Output is correct |
44 |
Correct |
13 ms |
19404 KB |
Output is correct |
45 |
Correct |
11 ms |
19380 KB |
Output is correct |
46 |
Correct |
11 ms |
19380 KB |
Output is correct |
47 |
Correct |
13 ms |
19676 KB |
Output is correct |
48 |
Correct |
13 ms |
19632 KB |
Output is correct |
49 |
Correct |
19 ms |
20368 KB |
Output is correct |
50 |
Correct |
107 ms |
28100 KB |
Output is correct |
51 |
Correct |
19 ms |
19736 KB |
Output is correct |
52 |
Correct |
90 ms |
26152 KB |
Output is correct |
53 |
Correct |
19 ms |
20300 KB |
Output is correct |
54 |
Correct |
100 ms |
27088 KB |
Output is correct |
55 |
Correct |
15 ms |
19916 KB |
Output is correct |
56 |
Correct |
20 ms |
19892 KB |
Output is correct |
57 |
Correct |
15 ms |
19900 KB |
Output is correct |
58 |
Correct |
15 ms |
19892 KB |
Output is correct |
59 |
Correct |
16 ms |
19788 KB |
Output is correct |
60 |
Correct |
79 ms |
24772 KB |
Output is correct |
61 |
Correct |
13 ms |
19644 KB |
Output is correct |
62 |
Correct |
108 ms |
27436 KB |
Output is correct |
63 |
Correct |
413 ms |
72204 KB |
Output is correct |
64 |
Correct |
418 ms |
72208 KB |
Output is correct |
65 |
Correct |
419 ms |
72388 KB |
Output is correct |
66 |
Incorrect |
189 ms |
38340 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |