//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
#include <queue>
#define maxn 100005
#define ll long long
using namespace std;
set<int> adj[maxn], rev[maxn], chi[maxn];
int par[maxn];
ll siz[maxn], cnt[maxn];
ll ans = 0;
inline int find(int a) {
return a == par[a] ? a : par[a] = find(par[a]);
}
void Union(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
par[b] = a;
siz[a] += siz[b];
}
void addEdge(int a, int b);
queue<pair<int, int> > que;
void combine(int a, int b) {
//cout << a << " " << b << endl;
if (find(a) == find(b)) return;
a = find(a), b = find(b);
if (adj[a].size() + rev[a].size() + chi[a].size() < adj[b].size() + rev[b].size() + chi[b].size()) swap(a, b);
//cout << a << " " << b << endl;
//cout << siz[a] << " " << chi[b].size() << " " << siz[b] << " " << chi[a].size()<< endl;
ans += siz[a] * (chi[b].size()) + siz[b] * (chi[a].size());
adj[a].erase(b);rev[b].erase(a);
adj[b].erase(a);rev[a].erase(b);
Union(a, b);
for (int c:chi[b]) {
if (chi[a].count(c)) ans -= siz[a];
else chi[a].insert(c);
}
for (int v:adj[b]) {
rev[v].erase(b);
addEdge(a, v);
}
for (int v:rev[b]) {
adj[v].erase(b);
addEdge(v, a);
}
adj[b].clear(), rev[b].clear();
//cnt[a] = rev[a].size();
//cout << cnt[a] - ob << " " << siz[b] << " " << cnt[a] - oa << " " << siz[a] << endl;
}
void addEdge(int a, int b) {
adj[a].insert(b);
rev[b].insert(a);
if (rev[a].count(b)) {
que.push(make_pair(a, b));
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1;i <= n;i++) {
par[i] = i, siz[i] = 1;
chi[i].insert(i);
}
for (int i = 0;i < m;i++) {
int a, b;
cin >> a >> b;
b = find(b);
if (find(a) == find(b) || chi[b].count(a)) {
cout << ans << "\n";
continue;
}
chi[b].insert(a);
a = find(a);
ans += siz[b];
addEdge(a, b);
while (que.size()) {
combine(que.front().first, que.front().second);
que.pop();
}
cout << ans << "\n";
}
}
/*
4 6
1 2
2 3
3 2
1 3
3 4
4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
9 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
9 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14572 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
10 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14720 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
9 ms |
14444 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
10 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14464 KB |
Output is correct |
33 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
9 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
9 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14572 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
10 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14720 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
9 ms |
14444 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
10 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14464 KB |
Output is correct |
33 |
Correct |
10 ms |
14444 KB |
Output is correct |
34 |
Correct |
12 ms |
14572 KB |
Output is correct |
35 |
Correct |
103 ms |
20388 KB |
Output is correct |
36 |
Correct |
139 ms |
23024 KB |
Output is correct |
37 |
Correct |
134 ms |
23148 KB |
Output is correct |
38 |
Correct |
131 ms |
22764 KB |
Output is correct |
39 |
Correct |
13 ms |
14828 KB |
Output is correct |
40 |
Correct |
14 ms |
15084 KB |
Output is correct |
41 |
Correct |
14 ms |
15084 KB |
Output is correct |
42 |
Correct |
12 ms |
14828 KB |
Output is correct |
43 |
Correct |
14 ms |
14828 KB |
Output is correct |
44 |
Correct |
14 ms |
14828 KB |
Output is correct |
45 |
Correct |
13 ms |
14828 KB |
Output is correct |
46 |
Correct |
12 ms |
14828 KB |
Output is correct |
47 |
Correct |
14 ms |
14956 KB |
Output is correct |
48 |
Correct |
14 ms |
14956 KB |
Output is correct |
49 |
Correct |
22 ms |
15596 KB |
Output is correct |
50 |
Correct |
135 ms |
23148 KB |
Output is correct |
51 |
Correct |
17 ms |
15084 KB |
Output is correct |
52 |
Correct |
115 ms |
21484 KB |
Output is correct |
53 |
Correct |
21 ms |
15468 KB |
Output is correct |
54 |
Correct |
122 ms |
22252 KB |
Output is correct |
55 |
Correct |
15 ms |
15212 KB |
Output is correct |
56 |
Correct |
16 ms |
15212 KB |
Output is correct |
57 |
Correct |
15 ms |
15212 KB |
Output is correct |
58 |
Correct |
18 ms |
15212 KB |
Output is correct |
59 |
Correct |
16 ms |
15212 KB |
Output is correct |
60 |
Correct |
98 ms |
20204 KB |
Output is correct |
61 |
Correct |
15 ms |
14956 KB |
Output is correct |
62 |
Correct |
124 ms |
22712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
9 ms |
14444 KB |
Output is correct |
3 |
Correct |
9 ms |
14444 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14572 KB |
Output is correct |
9 |
Correct |
10 ms |
14572 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
9 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
9 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14572 KB |
Output is correct |
22 |
Correct |
10 ms |
14464 KB |
Output is correct |
23 |
Correct |
10 ms |
14444 KB |
Output is correct |
24 |
Correct |
10 ms |
14444 KB |
Output is correct |
25 |
Correct |
12 ms |
14720 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
9 ms |
14444 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
10 ms |
14444 KB |
Output is correct |
32 |
Correct |
10 ms |
14464 KB |
Output is correct |
33 |
Correct |
10 ms |
14444 KB |
Output is correct |
34 |
Correct |
12 ms |
14572 KB |
Output is correct |
35 |
Correct |
103 ms |
20388 KB |
Output is correct |
36 |
Correct |
139 ms |
23024 KB |
Output is correct |
37 |
Correct |
134 ms |
23148 KB |
Output is correct |
38 |
Correct |
131 ms |
22764 KB |
Output is correct |
39 |
Correct |
13 ms |
14828 KB |
Output is correct |
40 |
Correct |
14 ms |
15084 KB |
Output is correct |
41 |
Correct |
14 ms |
15084 KB |
Output is correct |
42 |
Correct |
12 ms |
14828 KB |
Output is correct |
43 |
Correct |
14 ms |
14828 KB |
Output is correct |
44 |
Correct |
14 ms |
14828 KB |
Output is correct |
45 |
Correct |
13 ms |
14828 KB |
Output is correct |
46 |
Correct |
12 ms |
14828 KB |
Output is correct |
47 |
Correct |
14 ms |
14956 KB |
Output is correct |
48 |
Correct |
14 ms |
14956 KB |
Output is correct |
49 |
Correct |
22 ms |
15596 KB |
Output is correct |
50 |
Correct |
135 ms |
23148 KB |
Output is correct |
51 |
Correct |
17 ms |
15084 KB |
Output is correct |
52 |
Correct |
115 ms |
21484 KB |
Output is correct |
53 |
Correct |
21 ms |
15468 KB |
Output is correct |
54 |
Correct |
122 ms |
22252 KB |
Output is correct |
55 |
Correct |
15 ms |
15212 KB |
Output is correct |
56 |
Correct |
16 ms |
15212 KB |
Output is correct |
57 |
Correct |
15 ms |
15212 KB |
Output is correct |
58 |
Correct |
18 ms |
15212 KB |
Output is correct |
59 |
Correct |
16 ms |
15212 KB |
Output is correct |
60 |
Correct |
98 ms |
20204 KB |
Output is correct |
61 |
Correct |
15 ms |
14956 KB |
Output is correct |
62 |
Correct |
124 ms |
22712 KB |
Output is correct |
63 |
Correct |
630 ms |
68460 KB |
Output is correct |
64 |
Correct |
621 ms |
68076 KB |
Output is correct |
65 |
Correct |
633 ms |
68180 KB |
Output is correct |
66 |
Correct |
289 ms |
34284 KB |
Output is correct |
67 |
Correct |
552 ms |
57964 KB |
Output is correct |
68 |
Correct |
276 ms |
34156 KB |
Output is correct |
69 |
Correct |
632 ms |
35308 KB |
Output is correct |
70 |
Correct |
358 ms |
34156 KB |
Output is correct |
71 |
Correct |
315 ms |
34156 KB |
Output is correct |
72 |
Correct |
570 ms |
42436 KB |
Output is correct |
73 |
Correct |
606 ms |
43992 KB |
Output is correct |
74 |
Correct |
1234 ms |
57192 KB |
Output is correct |
75 |
Correct |
761 ms |
40836 KB |
Output is correct |
76 |
Correct |
961 ms |
49004 KB |
Output is correct |
77 |
Correct |
997 ms |
49196 KB |
Output is correct |
78 |
Correct |
288 ms |
36716 KB |
Output is correct |
79 |
Correct |
519 ms |
40940 KB |
Output is correct |
80 |
Correct |
290 ms |
35436 KB |
Output is correct |
81 |
Correct |
496 ms |
39020 KB |
Output is correct |
82 |
Correct |
1066 ms |
52392 KB |
Output is correct |
83 |
Correct |
1071 ms |
52448 KB |
Output is correct |
84 |
Correct |
809 ms |
52076 KB |
Output is correct |
85 |
Correct |
823 ms |
52204 KB |
Output is correct |
86 |
Correct |
420 ms |
66960 KB |
Output is correct |
87 |
Correct |
440 ms |
69228 KB |
Output is correct |
88 |
Correct |
603 ms |
43244 KB |
Output is correct |
89 |
Correct |
959 ms |
47348 KB |
Output is correct |