#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
template<class T> using V = vector<T>;
#define arr array
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#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 rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
#define int long long
const int maxn = 1e5;
int N, M, ans;
queue<pi> q;
int bulgem[maxn], ataman[maxn];
set<int> radj[maxn], adj[maxn], K[maxn];
int parent(int A) {
return (A == ataman[A] ? A : ataman[A] = parent(ataman[A]));
}
void merge_comp(int A, int B) {
A = parent(A); B = parent(B);
if(A == B) return;
ans += 1LL * bulgem[A] * sz(K[B]) + 1LL * bulgem[B] * sz(K[A]);
if(sz(K[A]) < sz(K[B])) swap(A, B);
trav(u, K[B]) {
if(K[A].find(u) != end(K[A])) ans -= 1LL * (bulgem[A] + bulgem[B]);
else K[A].ins(u);
}
adj[A].erase(B);
adj[B].erase(A);
radj[A].erase(B);
radj[B].erase(A);
trav(u, adj[B]) {
radj[u].erase(B);
adj[A].ins(u);
radj[u].ins(A);
if(adj[u].find(A) != end(adj[u])) q.push({u, A});
}
trav(u, radj[B]) {
adj[u].erase(B);
radj[A].ins(u);
adj[u].ins(A);
if(adj[A].find(u) != end(adj[A])) q.push({u, A});
}
bulgem[A] += bulgem[B];
ataman[B] = A;
}
void add_edge(int A, int B) {
B = parent(B);
if(K[B].find(A) != end(K[B])) return;
ans += bulgem[B];
K[B].ins(A);
A = parent(A);
adj[A].ins(B);
radj[B].ins(A);
if(adj[B].count(A)) merge_comp(A, B);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M; cin >> N >> M;
F0R(i, N) {
ataman[i] = i;
bulgem[i] = 1;
K[i].ins(i);
}
F0R(i, M) {
int u, v; cin >> u >> v;
--u; --v;
add_edge(u, v);
while(!q.empty()) {
tie(u, v) = q.ft;
q.pop();
merge_comp(u, v);
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14408 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14388 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14424 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
10 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14388 KB |
Output is correct |
22 |
Correct |
8 ms |
14424 KB |
Output is correct |
23 |
Correct |
8 ms |
14368 KB |
Output is correct |
24 |
Correct |
8 ms |
14416 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14368 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
9 ms |
14440 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14352 KB |
Output is correct |
32 |
Correct |
9 ms |
14424 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14408 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14388 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14424 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
10 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14388 KB |
Output is correct |
22 |
Correct |
8 ms |
14424 KB |
Output is correct |
23 |
Correct |
8 ms |
14368 KB |
Output is correct |
24 |
Correct |
8 ms |
14416 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14368 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
9 ms |
14440 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14352 KB |
Output is correct |
32 |
Correct |
9 ms |
14424 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
34 |
Correct |
11 ms |
14548 KB |
Output is correct |
35 |
Correct |
102 ms |
18876 KB |
Output is correct |
36 |
Correct |
131 ms |
21340 KB |
Output is correct |
37 |
Correct |
132 ms |
21384 KB |
Output is correct |
38 |
Correct |
127 ms |
21028 KB |
Output is correct |
39 |
Correct |
10 ms |
14784 KB |
Output is correct |
40 |
Correct |
11 ms |
15064 KB |
Output is correct |
41 |
Correct |
13 ms |
15128 KB |
Output is correct |
42 |
Correct |
9 ms |
14804 KB |
Output is correct |
43 |
Correct |
16 ms |
14736 KB |
Output is correct |
44 |
Correct |
11 ms |
14808 KB |
Output is correct |
45 |
Correct |
11 ms |
14688 KB |
Output is correct |
46 |
Correct |
10 ms |
14676 KB |
Output is correct |
47 |
Correct |
11 ms |
14932 KB |
Output is correct |
48 |
Correct |
11 ms |
14932 KB |
Output is correct |
49 |
Correct |
17 ms |
15720 KB |
Output is correct |
50 |
Correct |
133 ms |
21592 KB |
Output is correct |
51 |
Correct |
14 ms |
15072 KB |
Output is correct |
52 |
Correct |
123 ms |
19708 KB |
Output is correct |
53 |
Correct |
18 ms |
15572 KB |
Output is correct |
54 |
Correct |
125 ms |
20556 KB |
Output is correct |
55 |
Correct |
13 ms |
15224 KB |
Output is correct |
56 |
Correct |
12 ms |
15188 KB |
Output is correct |
57 |
Correct |
12 ms |
15064 KB |
Output is correct |
58 |
Correct |
13 ms |
15168 KB |
Output is correct |
59 |
Correct |
13 ms |
15108 KB |
Output is correct |
60 |
Correct |
99 ms |
18360 KB |
Output is correct |
61 |
Correct |
12 ms |
14932 KB |
Output is correct |
62 |
Correct |
124 ms |
20920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14408 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14388 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14428 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14424 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
10 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14388 KB |
Output is correct |
22 |
Correct |
8 ms |
14424 KB |
Output is correct |
23 |
Correct |
8 ms |
14368 KB |
Output is correct |
24 |
Correct |
8 ms |
14416 KB |
Output is correct |
25 |
Correct |
9 ms |
14420 KB |
Output is correct |
26 |
Correct |
9 ms |
14420 KB |
Output is correct |
27 |
Correct |
8 ms |
14368 KB |
Output is correct |
28 |
Correct |
7 ms |
14420 KB |
Output is correct |
29 |
Correct |
9 ms |
14440 KB |
Output is correct |
30 |
Correct |
8 ms |
14412 KB |
Output is correct |
31 |
Correct |
8 ms |
14352 KB |
Output is correct |
32 |
Correct |
9 ms |
14424 KB |
Output is correct |
33 |
Correct |
8 ms |
14420 KB |
Output is correct |
34 |
Correct |
11 ms |
14548 KB |
Output is correct |
35 |
Correct |
102 ms |
18876 KB |
Output is correct |
36 |
Correct |
131 ms |
21340 KB |
Output is correct |
37 |
Correct |
132 ms |
21384 KB |
Output is correct |
38 |
Correct |
127 ms |
21028 KB |
Output is correct |
39 |
Correct |
10 ms |
14784 KB |
Output is correct |
40 |
Correct |
11 ms |
15064 KB |
Output is correct |
41 |
Correct |
13 ms |
15128 KB |
Output is correct |
42 |
Correct |
9 ms |
14804 KB |
Output is correct |
43 |
Correct |
16 ms |
14736 KB |
Output is correct |
44 |
Correct |
11 ms |
14808 KB |
Output is correct |
45 |
Correct |
11 ms |
14688 KB |
Output is correct |
46 |
Correct |
10 ms |
14676 KB |
Output is correct |
47 |
Correct |
11 ms |
14932 KB |
Output is correct |
48 |
Correct |
11 ms |
14932 KB |
Output is correct |
49 |
Correct |
17 ms |
15720 KB |
Output is correct |
50 |
Correct |
133 ms |
21592 KB |
Output is correct |
51 |
Correct |
14 ms |
15072 KB |
Output is correct |
52 |
Correct |
123 ms |
19708 KB |
Output is correct |
53 |
Correct |
18 ms |
15572 KB |
Output is correct |
54 |
Correct |
125 ms |
20556 KB |
Output is correct |
55 |
Correct |
13 ms |
15224 KB |
Output is correct |
56 |
Correct |
12 ms |
15188 KB |
Output is correct |
57 |
Correct |
12 ms |
15064 KB |
Output is correct |
58 |
Correct |
13 ms |
15168 KB |
Output is correct |
59 |
Correct |
13 ms |
15108 KB |
Output is correct |
60 |
Correct |
99 ms |
18360 KB |
Output is correct |
61 |
Correct |
12 ms |
14932 KB |
Output is correct |
62 |
Correct |
124 ms |
20920 KB |
Output is correct |
63 |
Correct |
528 ms |
65764 KB |
Output is correct |
64 |
Correct |
560 ms |
65784 KB |
Output is correct |
65 |
Correct |
529 ms |
65868 KB |
Output is correct |
66 |
Correct |
242 ms |
33356 KB |
Output is correct |
67 |
Correct |
471 ms |
61548 KB |
Output is correct |
68 |
Correct |
214 ms |
34424 KB |
Output is correct |
69 |
Correct |
493 ms |
35484 KB |
Output is correct |
70 |
Correct |
275 ms |
34408 KB |
Output is correct |
71 |
Correct |
297 ms |
34460 KB |
Output is correct |
72 |
Correct |
551 ms |
45672 KB |
Output is correct |
73 |
Correct |
514 ms |
47404 KB |
Output is correct |
74 |
Correct |
1126 ms |
69748 KB |
Output is correct |
75 |
Correct |
683 ms |
42400 KB |
Output is correct |
76 |
Correct |
847 ms |
54584 KB |
Output is correct |
77 |
Correct |
863 ms |
54824 KB |
Output is correct |
78 |
Correct |
261 ms |
40396 KB |
Output is correct |
79 |
Correct |
504 ms |
44792 KB |
Output is correct |
80 |
Correct |
210 ms |
36872 KB |
Output is correct |
81 |
Correct |
416 ms |
40528 KB |
Output is correct |
82 |
Correct |
856 ms |
55040 KB |
Output is correct |
83 |
Correct |
895 ms |
55020 KB |
Output is correct |
84 |
Correct |
702 ms |
53716 KB |
Output is correct |
85 |
Correct |
660 ms |
53708 KB |
Output is correct |
86 |
Correct |
333 ms |
67244 KB |
Output is correct |
87 |
Correct |
360 ms |
69488 KB |
Output is correct |
88 |
Correct |
468 ms |
46660 KB |
Output is correct |
89 |
Correct |
784 ms |
52704 KB |
Output is correct |