#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
const int MAXN = 1e5 + 5;
int n, m;
ll followings = 0;
set<int> adj[MAXN], radj[MAXN], child[MAXN];
vector<int> parent(MAXN), sz(MAXN);
queue<pair<int,int>> q;
void init(int v) {
parent[v] = v;
sz[v] = 1;
child[v].insert(v);
}
int find(int v) {
if (parent[v] == v)
return v;
return parent[v] = find(parent[v]);
}
int totalSize(int v) {
int s = adj[v].size() + radj[v].size() + child[v].size();
return s;
}
void follow(int a, int b) {
adj[a].insert(b);
radj[b].insert(a);
if (adj[b].find(a) != adj[b].end()) {
q.push({a, b});
}
}
void mutualFollow(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (totalSize(a) < totalSize(b)) swap(a, b);
followings += sz[a] * child[b].size() + sz[b] * child[a].size();
parent[b] = a;
sz[a] += sz[b];
for (int i : child[b]) {
if (child[a].find(i) == child[a].end()) {
child[a].insert(i);
} else {
followings -= sz[a];
}
}
adj[a].erase(b);
adj[b].erase(a);
radj[a].erase(b);
radj[b].erase(a);
for (int i : adj[b]) {
follow(a, i);
}
for (int i : radj[b]) {
follow(i, a);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
init(i);
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
int compA = find(a), compB = find(b);
if (compA != compB && child[compB].find(a) == child[compB].end()) {
child[compB].insert(a);
followings += sz[compB];
follow(compA, compB);
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
mutualFollow(x, y);
}
}
cout << followings << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15128 KB |
Output is correct |
3 |
Correct |
6 ms |
15212 KB |
Output is correct |
4 |
Correct |
6 ms |
15116 KB |
Output is correct |
5 |
Correct |
7 ms |
15084 KB |
Output is correct |
6 |
Correct |
8 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15168 KB |
Output is correct |
8 |
Correct |
7 ms |
15184 KB |
Output is correct |
9 |
Correct |
7 ms |
15220 KB |
Output is correct |
10 |
Correct |
7 ms |
15208 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15148 KB |
Output is correct |
13 |
Correct |
7 ms |
15188 KB |
Output is correct |
14 |
Correct |
7 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15212 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
8 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
8 ms |
15204 KB |
Output is correct |
20 |
Correct |
7 ms |
15148 KB |
Output is correct |
21 |
Correct |
7 ms |
15188 KB |
Output is correct |
22 |
Correct |
7 ms |
15212 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15172 KB |
Output is correct |
25 |
Correct |
7 ms |
15220 KB |
Output is correct |
26 |
Correct |
7 ms |
15208 KB |
Output is correct |
27 |
Correct |
7 ms |
15212 KB |
Output is correct |
28 |
Correct |
8 ms |
15216 KB |
Output is correct |
29 |
Correct |
7 ms |
15188 KB |
Output is correct |
30 |
Correct |
7 ms |
15208 KB |
Output is correct |
31 |
Correct |
8 ms |
15196 KB |
Output is correct |
32 |
Correct |
7 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15128 KB |
Output is correct |
3 |
Correct |
6 ms |
15212 KB |
Output is correct |
4 |
Correct |
6 ms |
15116 KB |
Output is correct |
5 |
Correct |
7 ms |
15084 KB |
Output is correct |
6 |
Correct |
8 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15168 KB |
Output is correct |
8 |
Correct |
7 ms |
15184 KB |
Output is correct |
9 |
Correct |
7 ms |
15220 KB |
Output is correct |
10 |
Correct |
7 ms |
15208 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15148 KB |
Output is correct |
13 |
Correct |
7 ms |
15188 KB |
Output is correct |
14 |
Correct |
7 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15212 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
8 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
8 ms |
15204 KB |
Output is correct |
20 |
Correct |
7 ms |
15148 KB |
Output is correct |
21 |
Correct |
7 ms |
15188 KB |
Output is correct |
22 |
Correct |
7 ms |
15212 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15172 KB |
Output is correct |
25 |
Correct |
7 ms |
15220 KB |
Output is correct |
26 |
Correct |
7 ms |
15208 KB |
Output is correct |
27 |
Correct |
7 ms |
15212 KB |
Output is correct |
28 |
Correct |
8 ms |
15216 KB |
Output is correct |
29 |
Correct |
7 ms |
15188 KB |
Output is correct |
30 |
Correct |
7 ms |
15208 KB |
Output is correct |
31 |
Correct |
8 ms |
15196 KB |
Output is correct |
32 |
Correct |
7 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15212 KB |
Output is correct |
34 |
Correct |
8 ms |
15360 KB |
Output is correct |
35 |
Correct |
63 ms |
21348 KB |
Output is correct |
36 |
Correct |
86 ms |
24300 KB |
Output is correct |
37 |
Correct |
84 ms |
24428 KB |
Output is correct |
38 |
Correct |
87 ms |
24012 KB |
Output is correct |
39 |
Correct |
11 ms |
15560 KB |
Output is correct |
40 |
Correct |
12 ms |
16340 KB |
Output is correct |
41 |
Correct |
12 ms |
16372 KB |
Output is correct |
42 |
Correct |
9 ms |
15508 KB |
Output is correct |
43 |
Correct |
9 ms |
15540 KB |
Output is correct |
44 |
Correct |
9 ms |
15572 KB |
Output is correct |
45 |
Correct |
9 ms |
15444 KB |
Output is correct |
46 |
Correct |
9 ms |
15516 KB |
Output is correct |
47 |
Correct |
10 ms |
15864 KB |
Output is correct |
48 |
Correct |
10 ms |
15828 KB |
Output is correct |
49 |
Correct |
17 ms |
16980 KB |
Output is correct |
50 |
Correct |
89 ms |
24512 KB |
Output is correct |
51 |
Correct |
14 ms |
16084 KB |
Output is correct |
52 |
Correct |
70 ms |
22644 KB |
Output is correct |
53 |
Correct |
16 ms |
16756 KB |
Output is correct |
54 |
Correct |
77 ms |
23580 KB |
Output is correct |
55 |
Correct |
12 ms |
16120 KB |
Output is correct |
56 |
Correct |
12 ms |
16212 KB |
Output is correct |
57 |
Correct |
12 ms |
16212 KB |
Output is correct |
58 |
Correct |
11 ms |
16212 KB |
Output is correct |
59 |
Correct |
11 ms |
15956 KB |
Output is correct |
60 |
Correct |
63 ms |
20776 KB |
Output is correct |
61 |
Correct |
11 ms |
15924 KB |
Output is correct |
62 |
Correct |
79 ms |
23792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
7 ms |
15128 KB |
Output is correct |
3 |
Correct |
6 ms |
15212 KB |
Output is correct |
4 |
Correct |
6 ms |
15116 KB |
Output is correct |
5 |
Correct |
7 ms |
15084 KB |
Output is correct |
6 |
Correct |
8 ms |
15188 KB |
Output is correct |
7 |
Correct |
8 ms |
15168 KB |
Output is correct |
8 |
Correct |
7 ms |
15184 KB |
Output is correct |
9 |
Correct |
7 ms |
15220 KB |
Output is correct |
10 |
Correct |
7 ms |
15208 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
7 ms |
15148 KB |
Output is correct |
13 |
Correct |
7 ms |
15188 KB |
Output is correct |
14 |
Correct |
7 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15212 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
8 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
8 ms |
15204 KB |
Output is correct |
20 |
Correct |
7 ms |
15148 KB |
Output is correct |
21 |
Correct |
7 ms |
15188 KB |
Output is correct |
22 |
Correct |
7 ms |
15212 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15172 KB |
Output is correct |
25 |
Correct |
7 ms |
15220 KB |
Output is correct |
26 |
Correct |
7 ms |
15208 KB |
Output is correct |
27 |
Correct |
7 ms |
15212 KB |
Output is correct |
28 |
Correct |
8 ms |
15216 KB |
Output is correct |
29 |
Correct |
7 ms |
15188 KB |
Output is correct |
30 |
Correct |
7 ms |
15208 KB |
Output is correct |
31 |
Correct |
8 ms |
15196 KB |
Output is correct |
32 |
Correct |
7 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15212 KB |
Output is correct |
34 |
Correct |
8 ms |
15360 KB |
Output is correct |
35 |
Correct |
63 ms |
21348 KB |
Output is correct |
36 |
Correct |
86 ms |
24300 KB |
Output is correct |
37 |
Correct |
84 ms |
24428 KB |
Output is correct |
38 |
Correct |
87 ms |
24012 KB |
Output is correct |
39 |
Correct |
11 ms |
15560 KB |
Output is correct |
40 |
Correct |
12 ms |
16340 KB |
Output is correct |
41 |
Correct |
12 ms |
16372 KB |
Output is correct |
42 |
Correct |
9 ms |
15508 KB |
Output is correct |
43 |
Correct |
9 ms |
15540 KB |
Output is correct |
44 |
Correct |
9 ms |
15572 KB |
Output is correct |
45 |
Correct |
9 ms |
15444 KB |
Output is correct |
46 |
Correct |
9 ms |
15516 KB |
Output is correct |
47 |
Correct |
10 ms |
15864 KB |
Output is correct |
48 |
Correct |
10 ms |
15828 KB |
Output is correct |
49 |
Correct |
17 ms |
16980 KB |
Output is correct |
50 |
Correct |
89 ms |
24512 KB |
Output is correct |
51 |
Correct |
14 ms |
16084 KB |
Output is correct |
52 |
Correct |
70 ms |
22644 KB |
Output is correct |
53 |
Correct |
16 ms |
16756 KB |
Output is correct |
54 |
Correct |
77 ms |
23580 KB |
Output is correct |
55 |
Correct |
12 ms |
16120 KB |
Output is correct |
56 |
Correct |
12 ms |
16212 KB |
Output is correct |
57 |
Correct |
12 ms |
16212 KB |
Output is correct |
58 |
Correct |
11 ms |
16212 KB |
Output is correct |
59 |
Correct |
11 ms |
15956 KB |
Output is correct |
60 |
Correct |
63 ms |
20776 KB |
Output is correct |
61 |
Correct |
11 ms |
15924 KB |
Output is correct |
62 |
Correct |
79 ms |
23792 KB |
Output is correct |
63 |
Correct |
478 ms |
67688 KB |
Output is correct |
64 |
Correct |
467 ms |
67584 KB |
Output is correct |
65 |
Correct |
499 ms |
67532 KB |
Output is correct |
66 |
Correct |
168 ms |
33696 KB |
Output is correct |
67 |
Correct |
647 ms |
96588 KB |
Output is correct |
68 |
Correct |
184 ms |
33728 KB |
Output is correct |
69 |
Correct |
311 ms |
34828 KB |
Output is correct |
70 |
Correct |
192 ms |
33624 KB |
Output is correct |
71 |
Correct |
177 ms |
33612 KB |
Output is correct |
72 |
Correct |
471 ms |
54084 KB |
Output is correct |
73 |
Correct |
472 ms |
56904 KB |
Output is correct |
74 |
Correct |
1461 ms |
107024 KB |
Output is correct |
75 |
Correct |
528 ms |
45980 KB |
Output is correct |
76 |
Correct |
849 ms |
70608 KB |
Output is correct |
77 |
Correct |
884 ms |
70928 KB |
Output is correct |
78 |
Correct |
211 ms |
40780 KB |
Output is correct |
79 |
Correct |
394 ms |
46632 KB |
Output is correct |
80 |
Correct |
193 ms |
39644 KB |
Output is correct |
81 |
Correct |
338 ms |
43808 KB |
Output is correct |
82 |
Correct |
812 ms |
65964 KB |
Output is correct |
83 |
Correct |
818 ms |
65944 KB |
Output is correct |
84 |
Correct |
693 ms |
68888 KB |
Output is correct |
85 |
Correct |
688 ms |
68868 KB |
Output is correct |
86 |
Correct |
281 ms |
66464 KB |
Output is correct |
87 |
Correct |
287 ms |
68724 KB |
Output is correct |
88 |
Correct |
479 ms |
55480 KB |
Output is correct |
89 |
Correct |
803 ms |
68064 KB |
Output is correct |