#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const int MAXN = 100'009;
int cnt[MAXN];
int p[MAXN];
int sz[MAXN];
int sum[MAXN];
vector<int> V[MAXN];
map<int, int> in[MAXN];
map<int, int> out[MAXN];
set<int> In[MAXN];
set<int> Out[MAXN];
//set<int> in[MAXN];
//set<int> out[MAXN];
int ans;
int Find(int x) {
return x==p[x]?x:p[x]=Find(p[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if(a==b) return;
//cout << "before: " << sz[a] << " " << sum[a] << "\n";
//cout << "before: " << sz[b] << " " << sum[b] << "\n";
ans -= sz[a]*sz(In[a]);
ans -= sz[b]*sz(In[b]);
ans -= sz[a]*(sz[a]-1);
ans -= sz[b]*(sz[b]-1);
//cerr << "started ans: " << ans << "\n";
vector<int> union_queue;
if(sz(out[a])+sz(in[a])<sz(out[b])+sz(in[b])) swap(a, b);
//cerr << "SDOJFDSF: "<< a << " " << b << "\n";
for(int x:In[a]) {
//cerr << "? " << x << "\n";
}
for(int x:In[b]) {
//cerr << "! " << x << "\n";
}
for(pii x:out[b]) {
if(x.st==a) continue;
if(in[a].count(x.st)) {
union_queue.pb(x.st);
}
out[a][x.st]+=x.nd;
//out[a].insert(x);
in[x.st][a] += in[x.st][b];
in[x.st].erase(b);
//in[x].erase(b);
//in[x].insert(a);
}
for(int x:In[b]) {
if(Find(x)!=a) {
In[a].insert(x);
}
}
for(int x:V[b]) {
//cerr << " we will be thereby erasing: " << x << "\n";
In[a].erase(x);
V[a].pb(x);
}
V[b].clear();
out[b].clear();
for(pii x:in[b]) {
if(x.st==a) continue;
if(out[a].count(x.st)) {
union_queue.pb(x.st);
}
in[a][x.st]+=x.nd;
//cerr << "some susus\n";
//cerr << x.st << " " << x.nd << "\n";
sum[a]+=x.nd;
//in[a].insert(x);
out[x.st][a] += out[x.st][b];
out[x.st].erase(b);
//out[x].erase(b);
//out[x].insert(a);
}
in[b].clear();
p[b] = a;
sz[a] += sz[b];
//cerr <<" here : " << ans << "\n";
ans += sz[a]*(sz[a]-1);
ans += sz[a]*sz(In[a]);
for(int x:In[a]) {
//cout << "-> " << x << "\n";
}
//cerr << "here: " << ans << "\n";
//cerr << "after: " << sz[a] << " " << sz(In[a]) << "\n";
//cerr << "after mergin\n";
for(int x:union_queue) {
Union(a, x);
}
}
set<pii> S;
set<int> used[MAXN];
int32_t main() {
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int n, m;
cin >> n >> m;
iota(p, p+n+1, 0);
fill(sz, sz+n+1, 1);
for(int i=1;i<=n;i++) {
V[i].pb(i);
}
for(int i=0;i<m;i++) {
int a, b;
cin >> a >> b;
S.insert({a, b});
int A = Find(a);
int B = Find(b);
if(A==B) goto skip;
//out[A].insert(B);
out[A][B]++;
//in[B].insert(A);
if(in[A].count(B)) {
Union(A, B);
} else {
//cerr << "HERE\n";
ans -= sz[B]*sz(In[B]);
in[B][A]++;
sum[B]++;
In[B].insert(a);
Out[a].insert(B);
//cerr << "new sum: "<< B << " " << sum[B] << "\n";
ans += sz[B]*sz(In[B]);
}
skip:
//int cnt = 0;
//for(int i=1;i<=n;i++) {
//used[i].clear();
//}
//for(pii x:S) {
//if(Find(x.st)==Find(x.nd)) continue;
//if(used[x.st].count(Find(x.nd))) continue;
//cnt += sz[Find(x.nd)];
//used[x.st].insert(Find(x.nd));
//}
//cerr << ans << " " << cnt << "\n";
cout << ans << "\n";
}
}
Compilation message
joitter2.cpp: In function 'void Union(int64_t, int64_t)':
joitter2.cpp:45:10: warning: unused variable 'x' [-Wunused-variable]
45 | for(int x:In[a]) {
| ^
joitter2.cpp:48:10: warning: unused variable 'x' [-Wunused-variable]
48 | for(int x:In[b]) {
| ^
joitter2.cpp:97:10: warning: unused variable 'x' [-Wunused-variable]
97 | for(int x:In[a]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29272 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29464 KB |
Output is correct |
7 |
Correct |
10 ms |
29276 KB |
Output is correct |
8 |
Correct |
10 ms |
29276 KB |
Output is correct |
9 |
Correct |
10 ms |
29368 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29144 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29276 KB |
Output is correct |
15 |
Correct |
7 ms |
29276 KB |
Output is correct |
16 |
Correct |
7 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
7 ms |
29276 KB |
Output is correct |
21 |
Correct |
10 ms |
29532 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
8 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
12 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29272 KB |
Output is correct |
27 |
Correct |
6 ms |
29300 KB |
Output is correct |
28 |
Correct |
6 ms |
29272 KB |
Output is correct |
29 |
Correct |
6 ms |
29272 KB |
Output is correct |
30 |
Correct |
6 ms |
29320 KB |
Output is correct |
31 |
Correct |
10 ms |
29276 KB |
Output is correct |
32 |
Correct |
6 ms |
29276 KB |
Output is correct |
33 |
Correct |
11 ms |
29276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29272 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29464 KB |
Output is correct |
7 |
Correct |
10 ms |
29276 KB |
Output is correct |
8 |
Correct |
10 ms |
29276 KB |
Output is correct |
9 |
Correct |
10 ms |
29368 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29144 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29276 KB |
Output is correct |
15 |
Correct |
7 ms |
29276 KB |
Output is correct |
16 |
Correct |
7 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
7 ms |
29276 KB |
Output is correct |
21 |
Correct |
10 ms |
29532 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
8 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
12 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29272 KB |
Output is correct |
27 |
Correct |
6 ms |
29300 KB |
Output is correct |
28 |
Correct |
6 ms |
29272 KB |
Output is correct |
29 |
Correct |
6 ms |
29272 KB |
Output is correct |
30 |
Correct |
6 ms |
29320 KB |
Output is correct |
31 |
Correct |
10 ms |
29276 KB |
Output is correct |
32 |
Correct |
6 ms |
29276 KB |
Output is correct |
33 |
Correct |
11 ms |
29276 KB |
Output is correct |
34 |
Correct |
23 ms |
30044 KB |
Output is correct |
35 |
Correct |
637 ms |
51352 KB |
Output is correct |
36 |
Correct |
683 ms |
53368 KB |
Output is correct |
37 |
Correct |
679 ms |
53264 KB |
Output is correct |
38 |
Correct |
744 ms |
53076 KB |
Output is correct |
39 |
Correct |
14 ms |
29784 KB |
Output is correct |
40 |
Correct |
19 ms |
30300 KB |
Output is correct |
41 |
Correct |
18 ms |
30300 KB |
Output is correct |
42 |
Correct |
14 ms |
29904 KB |
Output is correct |
43 |
Correct |
15 ms |
29788 KB |
Output is correct |
44 |
Correct |
17 ms |
29784 KB |
Output is correct |
45 |
Correct |
15 ms |
29784 KB |
Output is correct |
46 |
Correct |
14 ms |
29788 KB |
Output is correct |
47 |
Correct |
17 ms |
30228 KB |
Output is correct |
48 |
Correct |
17 ms |
30044 KB |
Output is correct |
49 |
Correct |
33 ms |
31324 KB |
Output is correct |
50 |
Correct |
670 ms |
53360 KB |
Output is correct |
51 |
Correct |
35 ms |
30648 KB |
Output is correct |
52 |
Correct |
663 ms |
52304 KB |
Output is correct |
53 |
Correct |
32 ms |
31068 KB |
Output is correct |
54 |
Correct |
673 ms |
52740 KB |
Output is correct |
55 |
Correct |
17 ms |
30552 KB |
Output is correct |
56 |
Correct |
16 ms |
30556 KB |
Output is correct |
57 |
Correct |
17 ms |
30812 KB |
Output is correct |
58 |
Correct |
17 ms |
30940 KB |
Output is correct |
59 |
Correct |
20 ms |
30444 KB |
Output is correct |
60 |
Correct |
651 ms |
51364 KB |
Output is correct |
61 |
Correct |
17 ms |
30300 KB |
Output is correct |
62 |
Correct |
698 ms |
53264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29272 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
7 ms |
29464 KB |
Output is correct |
7 |
Correct |
10 ms |
29276 KB |
Output is correct |
8 |
Correct |
10 ms |
29276 KB |
Output is correct |
9 |
Correct |
10 ms |
29368 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
7 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29144 KB |
Output is correct |
13 |
Correct |
7 ms |
29276 KB |
Output is correct |
14 |
Correct |
6 ms |
29276 KB |
Output is correct |
15 |
Correct |
7 ms |
29276 KB |
Output is correct |
16 |
Correct |
7 ms |
29276 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29276 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
7 ms |
29276 KB |
Output is correct |
21 |
Correct |
10 ms |
29532 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
8 ms |
29276 KB |
Output is correct |
24 |
Correct |
7 ms |
29276 KB |
Output is correct |
25 |
Correct |
12 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29272 KB |
Output is correct |
27 |
Correct |
6 ms |
29300 KB |
Output is correct |
28 |
Correct |
6 ms |
29272 KB |
Output is correct |
29 |
Correct |
6 ms |
29272 KB |
Output is correct |
30 |
Correct |
6 ms |
29320 KB |
Output is correct |
31 |
Correct |
10 ms |
29276 KB |
Output is correct |
32 |
Correct |
6 ms |
29276 KB |
Output is correct |
33 |
Correct |
11 ms |
29276 KB |
Output is correct |
34 |
Correct |
23 ms |
30044 KB |
Output is correct |
35 |
Correct |
637 ms |
51352 KB |
Output is correct |
36 |
Correct |
683 ms |
53368 KB |
Output is correct |
37 |
Correct |
679 ms |
53264 KB |
Output is correct |
38 |
Correct |
744 ms |
53076 KB |
Output is correct |
39 |
Correct |
14 ms |
29784 KB |
Output is correct |
40 |
Correct |
19 ms |
30300 KB |
Output is correct |
41 |
Correct |
18 ms |
30300 KB |
Output is correct |
42 |
Correct |
14 ms |
29904 KB |
Output is correct |
43 |
Correct |
15 ms |
29788 KB |
Output is correct |
44 |
Correct |
17 ms |
29784 KB |
Output is correct |
45 |
Correct |
15 ms |
29784 KB |
Output is correct |
46 |
Correct |
14 ms |
29788 KB |
Output is correct |
47 |
Correct |
17 ms |
30228 KB |
Output is correct |
48 |
Correct |
17 ms |
30044 KB |
Output is correct |
49 |
Correct |
33 ms |
31324 KB |
Output is correct |
50 |
Correct |
670 ms |
53360 KB |
Output is correct |
51 |
Correct |
35 ms |
30648 KB |
Output is correct |
52 |
Correct |
663 ms |
52304 KB |
Output is correct |
53 |
Correct |
32 ms |
31068 KB |
Output is correct |
54 |
Correct |
673 ms |
52740 KB |
Output is correct |
55 |
Correct |
17 ms |
30552 KB |
Output is correct |
56 |
Correct |
16 ms |
30556 KB |
Output is correct |
57 |
Correct |
17 ms |
30812 KB |
Output is correct |
58 |
Correct |
17 ms |
30940 KB |
Output is correct |
59 |
Correct |
20 ms |
30444 KB |
Output is correct |
60 |
Correct |
651 ms |
51364 KB |
Output is correct |
61 |
Correct |
17 ms |
30300 KB |
Output is correct |
62 |
Correct |
698 ms |
53264 KB |
Output is correct |
63 |
Correct |
1301 ms |
118972 KB |
Output is correct |
64 |
Correct |
1249 ms |
119212 KB |
Output is correct |
65 |
Correct |
1224 ms |
119076 KB |
Output is correct |
66 |
Correct |
590 ms |
63448 KB |
Output is correct |
67 |
Correct |
1456 ms |
84020 KB |
Output is correct |
68 |
Correct |
634 ms |
63432 KB |
Output is correct |
69 |
Correct |
772 ms |
64708 KB |
Output is correct |
70 |
Correct |
658 ms |
63428 KB |
Output is correct |
71 |
Correct |
645 ms |
63424 KB |
Output is correct |
72 |
Correct |
1138 ms |
76876 KB |
Output is correct |
73 |
Correct |
1240 ms |
77772 KB |
Output is correct |
74 |
Correct |
1946 ms |
105840 KB |
Output is correct |
75 |
Correct |
1272 ms |
76820 KB |
Output is correct |
76 |
Correct |
1507 ms |
87776 KB |
Output is correct |
77 |
Correct |
1511 ms |
87932 KB |
Output is correct |
78 |
Correct |
566 ms |
67464 KB |
Output is correct |
79 |
Correct |
1065 ms |
80904 KB |
Output is correct |
80 |
Correct |
561 ms |
66112 KB |
Output is correct |
81 |
Correct |
1064 ms |
77268 KB |
Output is correct |
82 |
Correct |
1237 ms |
91592 KB |
Output is correct |
83 |
Correct |
1191 ms |
91456 KB |
Output is correct |
84 |
Correct |
1195 ms |
110176 KB |
Output is correct |
85 |
Correct |
1096 ms |
110016 KB |
Output is correct |
86 |
Correct |
1421 ms |
85792 KB |
Output is correct |
87 |
Correct |
1719 ms |
90212 KB |
Output is correct |
88 |
Correct |
1212 ms |
76652 KB |
Output is correct |
89 |
Correct |
1518 ms |
86132 KB |
Output is correct |