// In the name of God
//-MILY-
#pragma GCC optimize("O2", "unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define fi firsst
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
const int N = 1e5 + 5;
int n, m;
set<pair<int, int> > st;
vector<int> Q;
int par[N], sz[N];
ll ans;
vector<int> ver[N];
set<int> out[N], in[N], OUT[N];
map<int, int> cnt[N];
int get(int v) {
return par[v] == v ? v : par[v] = get(par[v]);
}
inline void unite(int x, int y) {
int dx = out[x].size() + in[x].size();
int dy = out[y].size() + in[y].size();
if (sz[x] > sz[y])
swap(x, y);
par[x] = y;
unordered_set<int> vin, vout;
unordered_map<int, int> tmp;
ll delta = 2LL * sz[x] * sz[y];
delta -= 1LL * cnt[x][y] * sz[y] + 1LL * cnt[y][x] * sz[x];
int inBoth = 0;
for (auto v : in[x]) {
if (get(v) == y) {
continue;
}
if (!in[y].count(v)) {
cnt[get(v)][y]++;
delta += sz[y];
}
else
inBoth++;
OUT[v].erase(x);
OUT[v].insert(y);
vin.insert(get(v));
}
delta += 1LL * sz[x] * (in[y].size() - inBoth - cnt[x][y]);
for (auto v : out[x]) {
vout.insert(get(v));
}
for (auto v : vin) {
if (st.count(mp(v, x)) && st.count(mp(y, v))) {
Q.pb(v);
}
if (st.count(mp(v, x)))
st.erase(mp(v, x));
st.insert(mp(v, y));
}
for (auto v : vout) {
if (st.count(mp(x, v)) && st.count(mp(v, y))) {
Q.pb(v);
}
cnt[y][v] += cnt[x][v];
if (st.count(mp(x, v)))
st.erase(mp(x, v));
st.insert(mp(y, v));
}
for (auto v : in[x])
if (get(v) != y)
in[y].insert(v);
for (auto v : out[x])
if (get(v) != y)
out[y].insert(v);
ans += delta;
for (auto v : ver[x])
if (in[y].count(v))
in[y].erase(v);
if (ver[x].size() > ver[y].size())
ver[x].swap(ver[y]);
for (auto v : ver[x])
ver[y].pb(v);
sz[y] += sz[x];
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
ver[i].pb(i);
}
while (m--) {
int v, u; cin >> v >> u;
int y = get(u), x = get(v);
if (OUT[v].count(y)) {
cout << ans << '\n';
continue;
}
if (st.count(mp(y, x))) {
Q.pb(y);
}
else {
ans += sz[y];
OUT[v].insert(y);
st.insert(mp(x, y));
in[y].insert(v);
out[x].insert(u);
cnt[x][y]++;
}
while (!Q.empty()) {
int y = Q.back();
Q.pop_back();
if (get(x) == get(y))
continue;
unite(x, y);
x = get(x);
}
cout << ans << '\n';
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message
joitter2.cpp: In function 'void unite(int, int)':
joitter2.cpp:26:9: warning: unused variable 'dx' [-Wunused-variable]
26 | int dx = out[x].size() + in[x].size();
| ^~
joitter2.cpp:27:9: warning: unused variable 'dy' [-Wunused-variable]
27 | int dy = out[y].size() + in[y].size();
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21360 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
12 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
12 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
12 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21476 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21464 KB |
Output is correct |
12 |
Correct |
12 ms |
21464 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
12 ms |
21404 KB |
Output is correct |
16 |
Correct |
11 ms |
21424 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21452 KB |
Output is correct |
20 |
Correct |
11 ms |
21456 KB |
Output is correct |
21 |
Correct |
13 ms |
21460 KB |
Output is correct |
22 |
Correct |
12 ms |
21460 KB |
Output is correct |
23 |
Correct |
12 ms |
21468 KB |
Output is correct |
24 |
Correct |
12 ms |
21468 KB |
Output is correct |
25 |
Correct |
13 ms |
21472 KB |
Output is correct |
26 |
Correct |
13 ms |
21460 KB |
Output is correct |
27 |
Correct |
14 ms |
21452 KB |
Output is correct |
28 |
Correct |
12 ms |
21480 KB |
Output is correct |
29 |
Correct |
11 ms |
21432 KB |
Output is correct |
30 |
Correct |
11 ms |
21372 KB |
Output is correct |
31 |
Correct |
12 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21468 KB |
Output is correct |
33 |
Correct |
12 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21360 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
12 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
12 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
12 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21476 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21464 KB |
Output is correct |
12 |
Correct |
12 ms |
21464 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
12 ms |
21404 KB |
Output is correct |
16 |
Correct |
11 ms |
21424 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21452 KB |
Output is correct |
20 |
Correct |
11 ms |
21456 KB |
Output is correct |
21 |
Correct |
13 ms |
21460 KB |
Output is correct |
22 |
Correct |
12 ms |
21460 KB |
Output is correct |
23 |
Correct |
12 ms |
21468 KB |
Output is correct |
24 |
Correct |
12 ms |
21468 KB |
Output is correct |
25 |
Correct |
13 ms |
21472 KB |
Output is correct |
26 |
Correct |
13 ms |
21460 KB |
Output is correct |
27 |
Correct |
14 ms |
21452 KB |
Output is correct |
28 |
Correct |
12 ms |
21480 KB |
Output is correct |
29 |
Correct |
11 ms |
21432 KB |
Output is correct |
30 |
Correct |
11 ms |
21372 KB |
Output is correct |
31 |
Correct |
12 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21468 KB |
Output is correct |
33 |
Correct |
12 ms |
21460 KB |
Output is correct |
34 |
Correct |
15 ms |
21588 KB |
Output is correct |
35 |
Correct |
107 ms |
28204 KB |
Output is correct |
36 |
Correct |
159 ms |
32420 KB |
Output is correct |
37 |
Correct |
167 ms |
32588 KB |
Output is correct |
38 |
Correct |
161 ms |
31820 KB |
Output is correct |
39 |
Correct |
15 ms |
22108 KB |
Output is correct |
40 |
Correct |
19 ms |
22300 KB |
Output is correct |
41 |
Correct |
18 ms |
22240 KB |
Output is correct |
42 |
Correct |
15 ms |
22100 KB |
Output is correct |
43 |
Correct |
16 ms |
22100 KB |
Output is correct |
44 |
Correct |
16 ms |
22044 KB |
Output is correct |
45 |
Correct |
17 ms |
22112 KB |
Output is correct |
46 |
Correct |
15 ms |
22108 KB |
Output is correct |
47 |
Correct |
19 ms |
22228 KB |
Output is correct |
48 |
Correct |
18 ms |
22272 KB |
Output is correct |
49 |
Correct |
32 ms |
23520 KB |
Output is correct |
50 |
Correct |
169 ms |
32836 KB |
Output is correct |
51 |
Correct |
22 ms |
22484 KB |
Output is correct |
52 |
Correct |
125 ms |
29796 KB |
Output is correct |
53 |
Correct |
32 ms |
23320 KB |
Output is correct |
54 |
Correct |
151 ms |
31208 KB |
Output is correct |
55 |
Correct |
23 ms |
22628 KB |
Output is correct |
56 |
Correct |
22 ms |
22696 KB |
Output is correct |
57 |
Correct |
22 ms |
22704 KB |
Output is correct |
58 |
Correct |
22 ms |
22740 KB |
Output is correct |
59 |
Correct |
16 ms |
22168 KB |
Output is correct |
60 |
Correct |
95 ms |
27140 KB |
Output is correct |
61 |
Correct |
24 ms |
22356 KB |
Output is correct |
62 |
Correct |
150 ms |
31564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
21360 KB |
Output is correct |
2 |
Correct |
12 ms |
21460 KB |
Output is correct |
3 |
Correct |
12 ms |
21460 KB |
Output is correct |
4 |
Correct |
12 ms |
21460 KB |
Output is correct |
5 |
Correct |
12 ms |
21460 KB |
Output is correct |
6 |
Correct |
11 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21460 KB |
Output is correct |
8 |
Correct |
12 ms |
21472 KB |
Output is correct |
9 |
Correct |
13 ms |
21476 KB |
Output is correct |
10 |
Correct |
12 ms |
21460 KB |
Output is correct |
11 |
Correct |
12 ms |
21464 KB |
Output is correct |
12 |
Correct |
12 ms |
21464 KB |
Output is correct |
13 |
Correct |
11 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21460 KB |
Output is correct |
15 |
Correct |
12 ms |
21404 KB |
Output is correct |
16 |
Correct |
11 ms |
21424 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
11 ms |
21460 KB |
Output is correct |
19 |
Correct |
11 ms |
21452 KB |
Output is correct |
20 |
Correct |
11 ms |
21456 KB |
Output is correct |
21 |
Correct |
13 ms |
21460 KB |
Output is correct |
22 |
Correct |
12 ms |
21460 KB |
Output is correct |
23 |
Correct |
12 ms |
21468 KB |
Output is correct |
24 |
Correct |
12 ms |
21468 KB |
Output is correct |
25 |
Correct |
13 ms |
21472 KB |
Output is correct |
26 |
Correct |
13 ms |
21460 KB |
Output is correct |
27 |
Correct |
14 ms |
21452 KB |
Output is correct |
28 |
Correct |
12 ms |
21480 KB |
Output is correct |
29 |
Correct |
11 ms |
21432 KB |
Output is correct |
30 |
Correct |
11 ms |
21372 KB |
Output is correct |
31 |
Correct |
12 ms |
21460 KB |
Output is correct |
32 |
Correct |
11 ms |
21468 KB |
Output is correct |
33 |
Correct |
12 ms |
21460 KB |
Output is correct |
34 |
Correct |
15 ms |
21588 KB |
Output is correct |
35 |
Correct |
107 ms |
28204 KB |
Output is correct |
36 |
Correct |
159 ms |
32420 KB |
Output is correct |
37 |
Correct |
167 ms |
32588 KB |
Output is correct |
38 |
Correct |
161 ms |
31820 KB |
Output is correct |
39 |
Correct |
15 ms |
22108 KB |
Output is correct |
40 |
Correct |
19 ms |
22300 KB |
Output is correct |
41 |
Correct |
18 ms |
22240 KB |
Output is correct |
42 |
Correct |
15 ms |
22100 KB |
Output is correct |
43 |
Correct |
16 ms |
22100 KB |
Output is correct |
44 |
Correct |
16 ms |
22044 KB |
Output is correct |
45 |
Correct |
17 ms |
22112 KB |
Output is correct |
46 |
Correct |
15 ms |
22108 KB |
Output is correct |
47 |
Correct |
19 ms |
22228 KB |
Output is correct |
48 |
Correct |
18 ms |
22272 KB |
Output is correct |
49 |
Correct |
32 ms |
23520 KB |
Output is correct |
50 |
Correct |
169 ms |
32836 KB |
Output is correct |
51 |
Correct |
22 ms |
22484 KB |
Output is correct |
52 |
Correct |
125 ms |
29796 KB |
Output is correct |
53 |
Correct |
32 ms |
23320 KB |
Output is correct |
54 |
Correct |
151 ms |
31208 KB |
Output is correct |
55 |
Correct |
23 ms |
22628 KB |
Output is correct |
56 |
Correct |
22 ms |
22696 KB |
Output is correct |
57 |
Correct |
22 ms |
22704 KB |
Output is correct |
58 |
Correct |
22 ms |
22740 KB |
Output is correct |
59 |
Correct |
16 ms |
22168 KB |
Output is correct |
60 |
Correct |
95 ms |
27140 KB |
Output is correct |
61 |
Correct |
24 ms |
22356 KB |
Output is correct |
62 |
Correct |
150 ms |
31564 KB |
Output is correct |
63 |
Correct |
923 ms |
101208 KB |
Output is correct |
64 |
Correct |
970 ms |
101352 KB |
Output is correct |
65 |
Correct |
921 ms |
101324 KB |
Output is correct |
66 |
Correct |
329 ms |
58244 KB |
Output is correct |
67 |
Correct |
818 ms |
66680 KB |
Output is correct |
68 |
Correct |
347 ms |
58312 KB |
Output is correct |
69 |
Correct |
564 ms |
53608 KB |
Output is correct |
70 |
Correct |
383 ms |
58320 KB |
Output is correct |
71 |
Correct |
338 ms |
58244 KB |
Output is correct |
72 |
Correct |
875 ms |
63732 KB |
Output is correct |
73 |
Correct |
858 ms |
64084 KB |
Output is correct |
74 |
Correct |
1988 ms |
106496 KB |
Output is correct |
75 |
Correct |
899 ms |
64696 KB |
Output is correct |
76 |
Correct |
1428 ms |
84000 KB |
Output is correct |
77 |
Correct |
1444 ms |
84152 KB |
Output is correct |
78 |
Correct |
555 ms |
62008 KB |
Output is correct |
79 |
Correct |
877 ms |
72644 KB |
Output is correct |
80 |
Correct |
563 ms |
63448 KB |
Output is correct |
81 |
Correct |
843 ms |
68532 KB |
Output is correct |
82 |
Correct |
1466 ms |
85800 KB |
Output is correct |
83 |
Correct |
1490 ms |
85748 KB |
Output is correct |
84 |
Correct |
1505 ms |
85872 KB |
Output is correct |
85 |
Correct |
1532 ms |
85724 KB |
Output is correct |
86 |
Correct |
489 ms |
64244 KB |
Output is correct |
87 |
Correct |
533 ms |
66456 KB |
Output is correct |
88 |
Correct |
859 ms |
64280 KB |
Output is correct |
89 |
Correct |
1397 ms |
82492 KB |
Output is correct |