#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 1e5 + 10;
const ll INF = 1e18;
int N, M; ll ans; int par[MX], sz[MX]; queue<pi> todo;
unordered_set<int> ext[MX], adj[MX], radj[MX];
int dsu_sz(int A) { return sz(ext[A]) + sz(adj[A]) + sz(radj[A]); }
int find(int A) { return (par[A] == A ? A : (par[A] = find(par[A]))); }
void connect(int A, int B) {
adj[A].insert(B); radj[B].insert(A);
if (adj[B].count(A)) todo.push({A, B});
}
void onion(int A, int B) {
if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B);
ans += 1LL * sz[B] * sz(ext[A]) + 1LL * sz[A] * sz(ext[B]);
par[B] = A; sz[A] += sz[B];
EACH(C, ext[B]) { if (ext[A].count(C)) ans -= sz[A]; else ext[A].insert(C); }
adj[A].erase(B); adj[B].erase(A); radj[A].erase(B); radj[B].erase(A);
EACH(C, adj[B]) { radj[C].erase(B); connect(A, C); }
EACH(C, radj[B]) { adj[C].erase(B); connect(C, A); }
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M; FOR(i, 1, N + 1) par[i] = i, sz[i] = 1, ext[i].insert(i);
F0R(i, M) {
int A, B; cin >> A >> B; B = find(B);
if (find(A) != B && !ext[B].count(A)) {
ext[B].insert(A); ans += sz[B];
A = find(A); connect(A, B);
while (!todo.empty()) {
pi cur = todo.front(); todo.pop();
onion(find(cur.f), find(cur.s));
}
}
cout << ans << "\n";
}
}
Compilation message
joitter2.cpp: In function 'void onion(int, int)':
joitter2.cpp:62:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
62 | if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B);
| ^~
joitter2.cpp:62:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
62 | if (A == B) return ; if (dsu_sz(A) < dsu_sz(B)) swap(A, B);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
12 ms |
16776 KB |
Output is correct |
3 |
Correct |
12 ms |
16768 KB |
Output is correct |
4 |
Correct |
12 ms |
16692 KB |
Output is correct |
5 |
Correct |
10 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16776 KB |
Output is correct |
8 |
Correct |
11 ms |
16772 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
10 ms |
16784 KB |
Output is correct |
11 |
Correct |
11 ms |
16792 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16716 KB |
Output is correct |
15 |
Correct |
10 ms |
16772 KB |
Output is correct |
16 |
Correct |
12 ms |
16900 KB |
Output is correct |
17 |
Correct |
10 ms |
16716 KB |
Output is correct |
18 |
Correct |
10 ms |
16764 KB |
Output is correct |
19 |
Correct |
11 ms |
16792 KB |
Output is correct |
20 |
Correct |
13 ms |
16804 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
11 ms |
16716 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
11 ms |
16716 KB |
Output is correct |
25 |
Correct |
11 ms |
16716 KB |
Output is correct |
26 |
Correct |
13 ms |
16716 KB |
Output is correct |
27 |
Correct |
11 ms |
16708 KB |
Output is correct |
28 |
Correct |
12 ms |
16752 KB |
Output is correct |
29 |
Correct |
10 ms |
16768 KB |
Output is correct |
30 |
Correct |
10 ms |
16768 KB |
Output is correct |
31 |
Correct |
11 ms |
16780 KB |
Output is correct |
32 |
Correct |
12 ms |
16844 KB |
Output is correct |
33 |
Correct |
11 ms |
16852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
12 ms |
16776 KB |
Output is correct |
3 |
Correct |
12 ms |
16768 KB |
Output is correct |
4 |
Correct |
12 ms |
16692 KB |
Output is correct |
5 |
Correct |
10 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16776 KB |
Output is correct |
8 |
Correct |
11 ms |
16772 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
10 ms |
16784 KB |
Output is correct |
11 |
Correct |
11 ms |
16792 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16716 KB |
Output is correct |
15 |
Correct |
10 ms |
16772 KB |
Output is correct |
16 |
Correct |
12 ms |
16900 KB |
Output is correct |
17 |
Correct |
10 ms |
16716 KB |
Output is correct |
18 |
Correct |
10 ms |
16764 KB |
Output is correct |
19 |
Correct |
11 ms |
16792 KB |
Output is correct |
20 |
Correct |
13 ms |
16804 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
11 ms |
16716 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
11 ms |
16716 KB |
Output is correct |
25 |
Correct |
11 ms |
16716 KB |
Output is correct |
26 |
Correct |
13 ms |
16716 KB |
Output is correct |
27 |
Correct |
11 ms |
16708 KB |
Output is correct |
28 |
Correct |
12 ms |
16752 KB |
Output is correct |
29 |
Correct |
10 ms |
16768 KB |
Output is correct |
30 |
Correct |
10 ms |
16768 KB |
Output is correct |
31 |
Correct |
11 ms |
16780 KB |
Output is correct |
32 |
Correct |
12 ms |
16844 KB |
Output is correct |
33 |
Correct |
11 ms |
16852 KB |
Output is correct |
34 |
Correct |
13 ms |
16956 KB |
Output is correct |
35 |
Correct |
98 ms |
22536 KB |
Output is correct |
36 |
Correct |
123 ms |
25216 KB |
Output is correct |
37 |
Correct |
125 ms |
25236 KB |
Output is correct |
38 |
Correct |
120 ms |
24952 KB |
Output is correct |
39 |
Correct |
14 ms |
17696 KB |
Output is correct |
40 |
Correct |
17 ms |
17928 KB |
Output is correct |
41 |
Correct |
19 ms |
17932 KB |
Output is correct |
42 |
Correct |
15 ms |
17596 KB |
Output is correct |
43 |
Correct |
15 ms |
17740 KB |
Output is correct |
44 |
Correct |
14 ms |
17672 KB |
Output is correct |
45 |
Correct |
14 ms |
17612 KB |
Output is correct |
46 |
Correct |
14 ms |
17612 KB |
Output is correct |
47 |
Correct |
15 ms |
17868 KB |
Output is correct |
48 |
Correct |
15 ms |
17876 KB |
Output is correct |
49 |
Correct |
21 ms |
18320 KB |
Output is correct |
50 |
Correct |
122 ms |
25376 KB |
Output is correct |
51 |
Correct |
19 ms |
17928 KB |
Output is correct |
52 |
Correct |
120 ms |
23868 KB |
Output is correct |
53 |
Correct |
20 ms |
18328 KB |
Output is correct |
54 |
Correct |
114 ms |
24512 KB |
Output is correct |
55 |
Correct |
17 ms |
17928 KB |
Output is correct |
56 |
Correct |
17 ms |
17996 KB |
Output is correct |
57 |
Correct |
15 ms |
17868 KB |
Output is correct |
58 |
Correct |
16 ms |
17996 KB |
Output is correct |
59 |
Correct |
15 ms |
17996 KB |
Output is correct |
60 |
Correct |
100 ms |
23020 KB |
Output is correct |
61 |
Correct |
16 ms |
17868 KB |
Output is correct |
62 |
Correct |
125 ms |
24776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
12 ms |
16776 KB |
Output is correct |
3 |
Correct |
12 ms |
16768 KB |
Output is correct |
4 |
Correct |
12 ms |
16692 KB |
Output is correct |
5 |
Correct |
10 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16776 KB |
Output is correct |
8 |
Correct |
11 ms |
16772 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
10 ms |
16784 KB |
Output is correct |
11 |
Correct |
11 ms |
16792 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
12 ms |
16716 KB |
Output is correct |
15 |
Correct |
10 ms |
16772 KB |
Output is correct |
16 |
Correct |
12 ms |
16900 KB |
Output is correct |
17 |
Correct |
10 ms |
16716 KB |
Output is correct |
18 |
Correct |
10 ms |
16764 KB |
Output is correct |
19 |
Correct |
11 ms |
16792 KB |
Output is correct |
20 |
Correct |
13 ms |
16804 KB |
Output is correct |
21 |
Correct |
11 ms |
16716 KB |
Output is correct |
22 |
Correct |
11 ms |
16716 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
11 ms |
16716 KB |
Output is correct |
25 |
Correct |
11 ms |
16716 KB |
Output is correct |
26 |
Correct |
13 ms |
16716 KB |
Output is correct |
27 |
Correct |
11 ms |
16708 KB |
Output is correct |
28 |
Correct |
12 ms |
16752 KB |
Output is correct |
29 |
Correct |
10 ms |
16768 KB |
Output is correct |
30 |
Correct |
10 ms |
16768 KB |
Output is correct |
31 |
Correct |
11 ms |
16780 KB |
Output is correct |
32 |
Correct |
12 ms |
16844 KB |
Output is correct |
33 |
Correct |
11 ms |
16852 KB |
Output is correct |
34 |
Correct |
13 ms |
16956 KB |
Output is correct |
35 |
Correct |
98 ms |
22536 KB |
Output is correct |
36 |
Correct |
123 ms |
25216 KB |
Output is correct |
37 |
Correct |
125 ms |
25236 KB |
Output is correct |
38 |
Correct |
120 ms |
24952 KB |
Output is correct |
39 |
Correct |
14 ms |
17696 KB |
Output is correct |
40 |
Correct |
17 ms |
17928 KB |
Output is correct |
41 |
Correct |
19 ms |
17932 KB |
Output is correct |
42 |
Correct |
15 ms |
17596 KB |
Output is correct |
43 |
Correct |
15 ms |
17740 KB |
Output is correct |
44 |
Correct |
14 ms |
17672 KB |
Output is correct |
45 |
Correct |
14 ms |
17612 KB |
Output is correct |
46 |
Correct |
14 ms |
17612 KB |
Output is correct |
47 |
Correct |
15 ms |
17868 KB |
Output is correct |
48 |
Correct |
15 ms |
17876 KB |
Output is correct |
49 |
Correct |
21 ms |
18320 KB |
Output is correct |
50 |
Correct |
122 ms |
25376 KB |
Output is correct |
51 |
Correct |
19 ms |
17928 KB |
Output is correct |
52 |
Correct |
120 ms |
23868 KB |
Output is correct |
53 |
Correct |
20 ms |
18328 KB |
Output is correct |
54 |
Correct |
114 ms |
24512 KB |
Output is correct |
55 |
Correct |
17 ms |
17928 KB |
Output is correct |
56 |
Correct |
17 ms |
17996 KB |
Output is correct |
57 |
Correct |
15 ms |
17868 KB |
Output is correct |
58 |
Correct |
16 ms |
17996 KB |
Output is correct |
59 |
Correct |
15 ms |
17996 KB |
Output is correct |
60 |
Correct |
100 ms |
23020 KB |
Output is correct |
61 |
Correct |
16 ms |
17868 KB |
Output is correct |
62 |
Correct |
125 ms |
24776 KB |
Output is correct |
63 |
Correct |
659 ms |
86060 KB |
Output is correct |
64 |
Correct |
664 ms |
85980 KB |
Output is correct |
65 |
Correct |
702 ms |
86112 KB |
Output is correct |
66 |
Correct |
306 ms |
65724 KB |
Output is correct |
67 |
Correct |
725 ms |
87832 KB |
Output is correct |
68 |
Correct |
303 ms |
65716 KB |
Output is correct |
69 |
Correct |
441 ms |
66956 KB |
Output is correct |
70 |
Correct |
336 ms |
65588 KB |
Output is correct |
71 |
Correct |
347 ms |
65548 KB |
Output is correct |
72 |
Correct |
703 ms |
74160 KB |
Output is correct |
73 |
Correct |
691 ms |
75500 KB |
Output is correct |
74 |
Correct |
1298 ms |
92076 KB |
Output is correct |
75 |
Correct |
613 ms |
72264 KB |
Output is correct |
76 |
Correct |
881 ms |
80232 KB |
Output is correct |
77 |
Correct |
882 ms |
80444 KB |
Output is correct |
78 |
Correct |
290 ms |
62292 KB |
Output is correct |
79 |
Correct |
457 ms |
71580 KB |
Output is correct |
80 |
Correct |
274 ms |
62248 KB |
Output is correct |
81 |
Correct |
426 ms |
69788 KB |
Output is correct |
82 |
Correct |
771 ms |
80344 KB |
Output is correct |
83 |
Correct |
775 ms |
80336 KB |
Output is correct |
84 |
Correct |
649 ms |
79080 KB |
Output is correct |
85 |
Correct |
661 ms |
79060 KB |
Output is correct |
86 |
Correct |
559 ms |
93644 KB |
Output is correct |
87 |
Correct |
652 ms |
95732 KB |
Output is correct |
88 |
Correct |
718 ms |
74968 KB |
Output is correct |
89 |
Correct |
859 ms |
78700 KB |
Output is correct |