#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 |
6 ms |
15188 KB |
Output is correct |
3 |
Correct |
6 ms |
15188 KB |
Output is correct |
4 |
Correct |
6 ms |
15188 KB |
Output is correct |
5 |
Correct |
7 ms |
15188 KB |
Output is correct |
6 |
Correct |
6 ms |
15188 KB |
Output is correct |
7 |
Correct |
7 ms |
15240 KB |
Output is correct |
8 |
Correct |
7 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
6 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
8 ms |
15188 KB |
Output is correct |
13 |
Correct |
7 ms |
15136 KB |
Output is correct |
14 |
Correct |
6 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15188 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
7 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
7 ms |
15188 KB |
Output is correct |
20 |
Correct |
7 ms |
15204 KB |
Output is correct |
21 |
Correct |
8 ms |
15248 KB |
Output is correct |
22 |
Correct |
7 ms |
15188 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15188 KB |
Output is correct |
25 |
Correct |
8 ms |
15188 KB |
Output is correct |
26 |
Correct |
7 ms |
15188 KB |
Output is correct |
27 |
Correct |
7 ms |
15188 KB |
Output is correct |
28 |
Correct |
7 ms |
15188 KB |
Output is correct |
29 |
Correct |
6 ms |
15188 KB |
Output is correct |
30 |
Correct |
6 ms |
15188 KB |
Output is correct |
31 |
Correct |
7 ms |
15152 KB |
Output is correct |
32 |
Correct |
6 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
6 ms |
15188 KB |
Output is correct |
3 |
Correct |
6 ms |
15188 KB |
Output is correct |
4 |
Correct |
6 ms |
15188 KB |
Output is correct |
5 |
Correct |
7 ms |
15188 KB |
Output is correct |
6 |
Correct |
6 ms |
15188 KB |
Output is correct |
7 |
Correct |
7 ms |
15240 KB |
Output is correct |
8 |
Correct |
7 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
6 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
8 ms |
15188 KB |
Output is correct |
13 |
Correct |
7 ms |
15136 KB |
Output is correct |
14 |
Correct |
6 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15188 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
7 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
7 ms |
15188 KB |
Output is correct |
20 |
Correct |
7 ms |
15204 KB |
Output is correct |
21 |
Correct |
8 ms |
15248 KB |
Output is correct |
22 |
Correct |
7 ms |
15188 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15188 KB |
Output is correct |
25 |
Correct |
8 ms |
15188 KB |
Output is correct |
26 |
Correct |
7 ms |
15188 KB |
Output is correct |
27 |
Correct |
7 ms |
15188 KB |
Output is correct |
28 |
Correct |
7 ms |
15188 KB |
Output is correct |
29 |
Correct |
6 ms |
15188 KB |
Output is correct |
30 |
Correct |
6 ms |
15188 KB |
Output is correct |
31 |
Correct |
7 ms |
15152 KB |
Output is correct |
32 |
Correct |
6 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15188 KB |
Output is correct |
34 |
Correct |
8 ms |
15316 KB |
Output is correct |
35 |
Correct |
73 ms |
18876 KB |
Output is correct |
36 |
Correct |
83 ms |
21672 KB |
Output is correct |
37 |
Correct |
85 ms |
21768 KB |
Output is correct |
38 |
Correct |
84 ms |
21400 KB |
Output is correct |
39 |
Correct |
9 ms |
15508 KB |
Output is correct |
40 |
Correct |
12 ms |
16340 KB |
Output is correct |
41 |
Correct |
12 ms |
16264 KB |
Output is correct |
42 |
Correct |
9 ms |
15444 KB |
Output is correct |
43 |
Correct |
9 ms |
15444 KB |
Output is correct |
44 |
Correct |
9 ms |
15444 KB |
Output is correct |
45 |
Correct |
9 ms |
15428 KB |
Output is correct |
46 |
Correct |
9 ms |
15444 KB |
Output is correct |
47 |
Correct |
11 ms |
15876 KB |
Output is correct |
48 |
Correct |
11 ms |
15852 KB |
Output is correct |
49 |
Correct |
17 ms |
16852 KB |
Output is correct |
50 |
Correct |
91 ms |
22096 KB |
Output is correct |
51 |
Correct |
13 ms |
15956 KB |
Output is correct |
52 |
Correct |
75 ms |
20048 KB |
Output is correct |
53 |
Correct |
16 ms |
16724 KB |
Output is correct |
54 |
Correct |
78 ms |
20948 KB |
Output is correct |
55 |
Correct |
12 ms |
16084 KB |
Output is correct |
56 |
Correct |
13 ms |
16084 KB |
Output is correct |
57 |
Correct |
12 ms |
16224 KB |
Output is correct |
58 |
Correct |
12 ms |
16212 KB |
Output is correct |
59 |
Correct |
10 ms |
15828 KB |
Output is correct |
60 |
Correct |
61 ms |
18224 KB |
Output is correct |
61 |
Correct |
11 ms |
15868 KB |
Output is correct |
62 |
Correct |
80 ms |
21208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15188 KB |
Output is correct |
2 |
Correct |
6 ms |
15188 KB |
Output is correct |
3 |
Correct |
6 ms |
15188 KB |
Output is correct |
4 |
Correct |
6 ms |
15188 KB |
Output is correct |
5 |
Correct |
7 ms |
15188 KB |
Output is correct |
6 |
Correct |
6 ms |
15188 KB |
Output is correct |
7 |
Correct |
7 ms |
15240 KB |
Output is correct |
8 |
Correct |
7 ms |
15188 KB |
Output is correct |
9 |
Correct |
7 ms |
15188 KB |
Output is correct |
10 |
Correct |
6 ms |
15188 KB |
Output is correct |
11 |
Correct |
7 ms |
15188 KB |
Output is correct |
12 |
Correct |
8 ms |
15188 KB |
Output is correct |
13 |
Correct |
7 ms |
15136 KB |
Output is correct |
14 |
Correct |
6 ms |
15188 KB |
Output is correct |
15 |
Correct |
6 ms |
15188 KB |
Output is correct |
16 |
Correct |
6 ms |
15188 KB |
Output is correct |
17 |
Correct |
7 ms |
15188 KB |
Output is correct |
18 |
Correct |
7 ms |
15188 KB |
Output is correct |
19 |
Correct |
7 ms |
15188 KB |
Output is correct |
20 |
Correct |
7 ms |
15204 KB |
Output is correct |
21 |
Correct |
8 ms |
15248 KB |
Output is correct |
22 |
Correct |
7 ms |
15188 KB |
Output is correct |
23 |
Correct |
7 ms |
15188 KB |
Output is correct |
24 |
Correct |
7 ms |
15188 KB |
Output is correct |
25 |
Correct |
8 ms |
15188 KB |
Output is correct |
26 |
Correct |
7 ms |
15188 KB |
Output is correct |
27 |
Correct |
7 ms |
15188 KB |
Output is correct |
28 |
Correct |
7 ms |
15188 KB |
Output is correct |
29 |
Correct |
6 ms |
15188 KB |
Output is correct |
30 |
Correct |
6 ms |
15188 KB |
Output is correct |
31 |
Correct |
7 ms |
15152 KB |
Output is correct |
32 |
Correct |
6 ms |
15188 KB |
Output is correct |
33 |
Correct |
7 ms |
15188 KB |
Output is correct |
34 |
Correct |
8 ms |
15316 KB |
Output is correct |
35 |
Correct |
73 ms |
18876 KB |
Output is correct |
36 |
Correct |
83 ms |
21672 KB |
Output is correct |
37 |
Correct |
85 ms |
21768 KB |
Output is correct |
38 |
Correct |
84 ms |
21400 KB |
Output is correct |
39 |
Correct |
9 ms |
15508 KB |
Output is correct |
40 |
Correct |
12 ms |
16340 KB |
Output is correct |
41 |
Correct |
12 ms |
16264 KB |
Output is correct |
42 |
Correct |
9 ms |
15444 KB |
Output is correct |
43 |
Correct |
9 ms |
15444 KB |
Output is correct |
44 |
Correct |
9 ms |
15444 KB |
Output is correct |
45 |
Correct |
9 ms |
15428 KB |
Output is correct |
46 |
Correct |
9 ms |
15444 KB |
Output is correct |
47 |
Correct |
11 ms |
15876 KB |
Output is correct |
48 |
Correct |
11 ms |
15852 KB |
Output is correct |
49 |
Correct |
17 ms |
16852 KB |
Output is correct |
50 |
Correct |
91 ms |
22096 KB |
Output is correct |
51 |
Correct |
13 ms |
15956 KB |
Output is correct |
52 |
Correct |
75 ms |
20048 KB |
Output is correct |
53 |
Correct |
16 ms |
16724 KB |
Output is correct |
54 |
Correct |
78 ms |
20948 KB |
Output is correct |
55 |
Correct |
12 ms |
16084 KB |
Output is correct |
56 |
Correct |
13 ms |
16084 KB |
Output is correct |
57 |
Correct |
12 ms |
16224 KB |
Output is correct |
58 |
Correct |
12 ms |
16212 KB |
Output is correct |
59 |
Correct |
10 ms |
15828 KB |
Output is correct |
60 |
Correct |
61 ms |
18224 KB |
Output is correct |
61 |
Correct |
11 ms |
15868 KB |
Output is correct |
62 |
Correct |
80 ms |
21208 KB |
Output is correct |
63 |
Correct |
481 ms |
64212 KB |
Output is correct |
64 |
Correct |
498 ms |
64076 KB |
Output is correct |
65 |
Correct |
480 ms |
64092 KB |
Output is correct |
66 |
Correct |
166 ms |
31408 KB |
Output is correct |
67 |
Correct |
686 ms |
94320 KB |
Output is correct |
68 |
Correct |
161 ms |
31308 KB |
Output is correct |
69 |
Correct |
304 ms |
32420 KB |
Output is correct |
70 |
Correct |
175 ms |
31384 KB |
Output is correct |
71 |
Correct |
181 ms |
31332 KB |
Output is correct |
72 |
Correct |
454 ms |
51872 KB |
Output is correct |
73 |
Correct |
469 ms |
54660 KB |
Output is correct |
74 |
Correct |
1424 ms |
103548 KB |
Output is correct |
75 |
Correct |
517 ms |
42572 KB |
Output is correct |
76 |
Correct |
848 ms |
67108 KB |
Output is correct |
77 |
Correct |
912 ms |
67352 KB |
Output is correct |
78 |
Correct |
218 ms |
38948 KB |
Output is correct |
79 |
Correct |
385 ms |
43164 KB |
Output is correct |
80 |
Correct |
213 ms |
37836 KB |
Output is correct |
81 |
Correct |
315 ms |
40312 KB |
Output is correct |
82 |
Correct |
866 ms |
63544 KB |
Output is correct |
83 |
Correct |
894 ms |
63592 KB |
Output is correct |
84 |
Correct |
714 ms |
66500 KB |
Output is correct |
85 |
Correct |
702 ms |
66516 KB |
Output is correct |
86 |
Correct |
281 ms |
64140 KB |
Output is correct |
87 |
Correct |
285 ms |
65200 KB |
Output is correct |
88 |
Correct |
476 ms |
53108 KB |
Output is correct |
89 |
Correct |
868 ms |
64504 KB |
Output is correct |