#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]);
}
ll 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]) * (ll)sz[B] + SZ(intopt[A]) * (ll)sz[A];
re -= (sz[B]) * (ll)(sz[B]-1) + (sz[A] * (ll)(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]) * (ll)sz[B];
re += (sz[B] * (ll)(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;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19108 KB |
Output is correct |
3 |
Correct |
13 ms |
19116 KB |
Output is correct |
4 |
Correct |
13 ms |
19000 KB |
Output is correct |
5 |
Correct |
10 ms |
19020 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
11 ms |
19032 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
9 ms |
18992 KB |
Output is correct |
11 |
Correct |
9 ms |
19020 KB |
Output is correct |
12 |
Correct |
9 ms |
19032 KB |
Output is correct |
13 |
Correct |
9 ms |
19020 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
10 ms |
19096 KB |
Output is correct |
16 |
Correct |
10 ms |
19020 KB |
Output is correct |
17 |
Correct |
12 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19068 KB |
Output is correct |
19 |
Correct |
12 ms |
19020 KB |
Output is correct |
20 |
Correct |
13 ms |
19020 KB |
Output is correct |
21 |
Correct |
10 ms |
19080 KB |
Output is correct |
22 |
Correct |
11 ms |
19020 KB |
Output is correct |
23 |
Correct |
9 ms |
19148 KB |
Output is correct |
24 |
Correct |
9 ms |
19104 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
10 ms |
19112 KB |
Output is correct |
27 |
Correct |
9 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19132 KB |
Output is correct |
29 |
Correct |
10 ms |
19012 KB |
Output is correct |
30 |
Correct |
10 ms |
19020 KB |
Output is correct |
31 |
Correct |
13 ms |
19044 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
11 ms |
19148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19108 KB |
Output is correct |
3 |
Correct |
13 ms |
19116 KB |
Output is correct |
4 |
Correct |
13 ms |
19000 KB |
Output is correct |
5 |
Correct |
10 ms |
19020 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
11 ms |
19032 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
9 ms |
18992 KB |
Output is correct |
11 |
Correct |
9 ms |
19020 KB |
Output is correct |
12 |
Correct |
9 ms |
19032 KB |
Output is correct |
13 |
Correct |
9 ms |
19020 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
10 ms |
19096 KB |
Output is correct |
16 |
Correct |
10 ms |
19020 KB |
Output is correct |
17 |
Correct |
12 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19068 KB |
Output is correct |
19 |
Correct |
12 ms |
19020 KB |
Output is correct |
20 |
Correct |
13 ms |
19020 KB |
Output is correct |
21 |
Correct |
10 ms |
19080 KB |
Output is correct |
22 |
Correct |
11 ms |
19020 KB |
Output is correct |
23 |
Correct |
9 ms |
19148 KB |
Output is correct |
24 |
Correct |
9 ms |
19104 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
10 ms |
19112 KB |
Output is correct |
27 |
Correct |
9 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19132 KB |
Output is correct |
29 |
Correct |
10 ms |
19012 KB |
Output is correct |
30 |
Correct |
10 ms |
19020 KB |
Output is correct |
31 |
Correct |
13 ms |
19044 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
11 ms |
19148 KB |
Output is correct |
34 |
Correct |
12 ms |
19148 KB |
Output is correct |
35 |
Correct |
86 ms |
22800 KB |
Output is correct |
36 |
Correct |
108 ms |
25288 KB |
Output is correct |
37 |
Correct |
113 ms |
25308 KB |
Output is correct |
38 |
Correct |
107 ms |
24860 KB |
Output is correct |
39 |
Correct |
11 ms |
19428 KB |
Output is correct |
40 |
Correct |
13 ms |
19788 KB |
Output is correct |
41 |
Correct |
12 ms |
19788 KB |
Output is correct |
42 |
Correct |
12 ms |
19404 KB |
Output is correct |
43 |
Correct |
13 ms |
19404 KB |
Output is correct |
44 |
Correct |
15 ms |
19348 KB |
Output is correct |
45 |
Correct |
12 ms |
19388 KB |
Output is correct |
46 |
Correct |
12 ms |
19356 KB |
Output is correct |
47 |
Correct |
14 ms |
19532 KB |
Output is correct |
48 |
Correct |
14 ms |
19532 KB |
Output is correct |
49 |
Correct |
19 ms |
20300 KB |
Output is correct |
50 |
Correct |
109 ms |
25520 KB |
Output is correct |
51 |
Correct |
15 ms |
19660 KB |
Output is correct |
52 |
Correct |
96 ms |
23640 KB |
Output is correct |
53 |
Correct |
19 ms |
20172 KB |
Output is correct |
54 |
Correct |
97 ms |
24428 KB |
Output is correct |
55 |
Correct |
15 ms |
19924 KB |
Output is correct |
56 |
Correct |
16 ms |
19896 KB |
Output is correct |
57 |
Correct |
14 ms |
19776 KB |
Output is correct |
58 |
Correct |
14 ms |
19788 KB |
Output is correct |
59 |
Correct |
13 ms |
19848 KB |
Output is correct |
60 |
Correct |
101 ms |
22224 KB |
Output is correct |
61 |
Correct |
13 ms |
19624 KB |
Output is correct |
62 |
Correct |
106 ms |
24748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19020 KB |
Output is correct |
2 |
Correct |
13 ms |
19108 KB |
Output is correct |
3 |
Correct |
13 ms |
19116 KB |
Output is correct |
4 |
Correct |
13 ms |
19000 KB |
Output is correct |
5 |
Correct |
10 ms |
19020 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
10 ms |
19148 KB |
Output is correct |
8 |
Correct |
11 ms |
19032 KB |
Output is correct |
9 |
Correct |
10 ms |
19148 KB |
Output is correct |
10 |
Correct |
9 ms |
18992 KB |
Output is correct |
11 |
Correct |
9 ms |
19020 KB |
Output is correct |
12 |
Correct |
9 ms |
19032 KB |
Output is correct |
13 |
Correct |
9 ms |
19020 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
10 ms |
19096 KB |
Output is correct |
16 |
Correct |
10 ms |
19020 KB |
Output is correct |
17 |
Correct |
12 ms |
19020 KB |
Output is correct |
18 |
Correct |
10 ms |
19068 KB |
Output is correct |
19 |
Correct |
12 ms |
19020 KB |
Output is correct |
20 |
Correct |
13 ms |
19020 KB |
Output is correct |
21 |
Correct |
10 ms |
19080 KB |
Output is correct |
22 |
Correct |
11 ms |
19020 KB |
Output is correct |
23 |
Correct |
9 ms |
19148 KB |
Output is correct |
24 |
Correct |
9 ms |
19104 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
10 ms |
19112 KB |
Output is correct |
27 |
Correct |
9 ms |
19020 KB |
Output is correct |
28 |
Correct |
9 ms |
19132 KB |
Output is correct |
29 |
Correct |
10 ms |
19012 KB |
Output is correct |
30 |
Correct |
10 ms |
19020 KB |
Output is correct |
31 |
Correct |
13 ms |
19044 KB |
Output is correct |
32 |
Correct |
13 ms |
19020 KB |
Output is correct |
33 |
Correct |
11 ms |
19148 KB |
Output is correct |
34 |
Correct |
12 ms |
19148 KB |
Output is correct |
35 |
Correct |
86 ms |
22800 KB |
Output is correct |
36 |
Correct |
108 ms |
25288 KB |
Output is correct |
37 |
Correct |
113 ms |
25308 KB |
Output is correct |
38 |
Correct |
107 ms |
24860 KB |
Output is correct |
39 |
Correct |
11 ms |
19428 KB |
Output is correct |
40 |
Correct |
13 ms |
19788 KB |
Output is correct |
41 |
Correct |
12 ms |
19788 KB |
Output is correct |
42 |
Correct |
12 ms |
19404 KB |
Output is correct |
43 |
Correct |
13 ms |
19404 KB |
Output is correct |
44 |
Correct |
15 ms |
19348 KB |
Output is correct |
45 |
Correct |
12 ms |
19388 KB |
Output is correct |
46 |
Correct |
12 ms |
19356 KB |
Output is correct |
47 |
Correct |
14 ms |
19532 KB |
Output is correct |
48 |
Correct |
14 ms |
19532 KB |
Output is correct |
49 |
Correct |
19 ms |
20300 KB |
Output is correct |
50 |
Correct |
109 ms |
25520 KB |
Output is correct |
51 |
Correct |
15 ms |
19660 KB |
Output is correct |
52 |
Correct |
96 ms |
23640 KB |
Output is correct |
53 |
Correct |
19 ms |
20172 KB |
Output is correct |
54 |
Correct |
97 ms |
24428 KB |
Output is correct |
55 |
Correct |
15 ms |
19924 KB |
Output is correct |
56 |
Correct |
16 ms |
19896 KB |
Output is correct |
57 |
Correct |
14 ms |
19776 KB |
Output is correct |
58 |
Correct |
14 ms |
19788 KB |
Output is correct |
59 |
Correct |
13 ms |
19848 KB |
Output is correct |
60 |
Correct |
101 ms |
22224 KB |
Output is correct |
61 |
Correct |
13 ms |
19624 KB |
Output is correct |
62 |
Correct |
106 ms |
24748 KB |
Output is correct |
63 |
Correct |
456 ms |
68832 KB |
Output is correct |
64 |
Correct |
428 ms |
68840 KB |
Output is correct |
65 |
Correct |
438 ms |
68836 KB |
Output is correct |
66 |
Correct |
203 ms |
36180 KB |
Output is correct |
67 |
Correct |
430 ms |
65220 KB |
Output is correct |
68 |
Correct |
203 ms |
38504 KB |
Output is correct |
69 |
Correct |
462 ms |
39504 KB |
Output is correct |
70 |
Correct |
204 ms |
38372 KB |
Output is correct |
71 |
Correct |
201 ms |
38268 KB |
Output is correct |
72 |
Correct |
442 ms |
49240 KB |
Output is correct |
73 |
Correct |
421 ms |
50960 KB |
Output is correct |
74 |
Correct |
1079 ms |
73280 KB |
Output is correct |
75 |
Correct |
719 ms |
46496 KB |
Output is correct |
76 |
Correct |
782 ms |
58568 KB |
Output is correct |
77 |
Correct |
763 ms |
58772 KB |
Output is correct |
78 |
Correct |
239 ms |
43204 KB |
Output is correct |
79 |
Correct |
416 ms |
47452 KB |
Output is correct |
80 |
Correct |
241 ms |
41948 KB |
Output is correct |
81 |
Correct |
381 ms |
45628 KB |
Output is correct |
82 |
Correct |
919 ms |
60332 KB |
Output is correct |
83 |
Correct |
943 ms |
60192 KB |
Output is correct |
84 |
Correct |
716 ms |
58916 KB |
Output is correct |
85 |
Correct |
721 ms |
58912 KB |
Output is correct |
86 |
Correct |
295 ms |
71052 KB |
Output is correct |
87 |
Correct |
335 ms |
73324 KB |
Output is correct |
88 |
Correct |
481 ms |
50324 KB |
Output is correct |
89 |
Correct |
739 ms |
56868 KB |
Output is correct |