#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100005
#define fi first
#define se second
typedef pair<int, int> ii;
set<int> adj[N], radj[N], st[N];
int pr[N], siz[N], n, m, res = 0;
queue<ii> q;
int find(int u){
if (u == pr[u]) return u;
return pr[u] = find(pr[u]);
}
void bl(int a, int b){
adj[a].insert(b);
radj[b].insert(a);
if (adj[b].count(a)){
q.push({a, b});
}
}
int getsiz(int a){
return st[a].size() + adj[a].size() + radj[a].size();
}
void unin(int a, int b){
if (a == b) return;
res += siz[a] * st[b].size() + siz[b] * st[a].size();
if (getsiz(a) < getsiz(b)) swap(a, b);
pr[b] = a;
siz[a] += siz[b];
for (auto x : st[b]){
if (st[a].count(x)) res -= siz[a];
else st[a].insert(x);
}
adj[a].erase(b);
radj[a].erase(b);
adj[b].erase(a);
radj[b].erase(a);
for (auto x : adj[b]){
radj[x].erase(b);
bl(a, x);
}
for (auto x : radj[b]){
adj[x].erase(b);
bl(x, a);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++){
siz[i] = 1;
pr[i] = i;
st[i].insert(i);
}
while (m--){
int a, b;
cin >> a >> b;
b = find(b);
if (find(a) != b && !st[b].count(a)){
st[b].insert(a);
res += siz[b];
a = find(a);
bl(a, b);
while (!q.empty()){
a = q.front().fi;
b = q.front().se;
q.pop();
unin(find(a), find(b));
}
}
cout << res << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15712 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15908 KB |
Output is correct |
5 |
Correct |
4 ms |
15704 KB |
Output is correct |
6 |
Correct |
4 ms |
15704 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
5 ms |
15900 KB |
Output is correct |
10 |
Correct |
3 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
4 ms |
15708 KB |
Output is correct |
13 |
Correct |
4 ms |
15708 KB |
Output is correct |
14 |
Correct |
4 ms |
15708 KB |
Output is correct |
15 |
Correct |
4 ms |
15704 KB |
Output is correct |
16 |
Correct |
4 ms |
15944 KB |
Output is correct |
17 |
Correct |
4 ms |
15708 KB |
Output is correct |
18 |
Correct |
4 ms |
15964 KB |
Output is correct |
19 |
Correct |
4 ms |
15708 KB |
Output is correct |
20 |
Correct |
5 ms |
15964 KB |
Output is correct |
21 |
Correct |
5 ms |
15796 KB |
Output is correct |
22 |
Correct |
4 ms |
15960 KB |
Output is correct |
23 |
Correct |
4 ms |
15988 KB |
Output is correct |
24 |
Correct |
4 ms |
15960 KB |
Output is correct |
25 |
Correct |
6 ms |
15964 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
5 ms |
15960 KB |
Output is correct |
29 |
Correct |
4 ms |
15704 KB |
Output is correct |
30 |
Correct |
4 ms |
15704 KB |
Output is correct |
31 |
Correct |
5 ms |
15964 KB |
Output is correct |
32 |
Correct |
4 ms |
15708 KB |
Output is correct |
33 |
Correct |
4 ms |
16216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15712 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15908 KB |
Output is correct |
5 |
Correct |
4 ms |
15704 KB |
Output is correct |
6 |
Correct |
4 ms |
15704 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
5 ms |
15900 KB |
Output is correct |
10 |
Correct |
3 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
4 ms |
15708 KB |
Output is correct |
13 |
Correct |
4 ms |
15708 KB |
Output is correct |
14 |
Correct |
4 ms |
15708 KB |
Output is correct |
15 |
Correct |
4 ms |
15704 KB |
Output is correct |
16 |
Correct |
4 ms |
15944 KB |
Output is correct |
17 |
Correct |
4 ms |
15708 KB |
Output is correct |
18 |
Correct |
4 ms |
15964 KB |
Output is correct |
19 |
Correct |
4 ms |
15708 KB |
Output is correct |
20 |
Correct |
5 ms |
15964 KB |
Output is correct |
21 |
Correct |
5 ms |
15796 KB |
Output is correct |
22 |
Correct |
4 ms |
15960 KB |
Output is correct |
23 |
Correct |
4 ms |
15988 KB |
Output is correct |
24 |
Correct |
4 ms |
15960 KB |
Output is correct |
25 |
Correct |
6 ms |
15964 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
5 ms |
15960 KB |
Output is correct |
29 |
Correct |
4 ms |
15704 KB |
Output is correct |
30 |
Correct |
4 ms |
15704 KB |
Output is correct |
31 |
Correct |
5 ms |
15964 KB |
Output is correct |
32 |
Correct |
4 ms |
15708 KB |
Output is correct |
33 |
Correct |
4 ms |
16216 KB |
Output is correct |
34 |
Correct |
7 ms |
15960 KB |
Output is correct |
35 |
Correct |
65 ms |
21844 KB |
Output is correct |
36 |
Correct |
88 ms |
24716 KB |
Output is correct |
37 |
Correct |
109 ms |
24800 KB |
Output is correct |
38 |
Correct |
82 ms |
24400 KB |
Output is correct |
39 |
Correct |
6 ms |
16220 KB |
Output is correct |
40 |
Correct |
9 ms |
16548 KB |
Output is correct |
41 |
Correct |
7 ms |
16476 KB |
Output is correct |
42 |
Correct |
6 ms |
16216 KB |
Output is correct |
43 |
Correct |
6 ms |
16220 KB |
Output is correct |
44 |
Correct |
10 ms |
16220 KB |
Output is correct |
45 |
Correct |
8 ms |
16220 KB |
Output is correct |
46 |
Correct |
6 ms |
16220 KB |
Output is correct |
47 |
Correct |
7 ms |
16472 KB |
Output is correct |
48 |
Correct |
8 ms |
16476 KB |
Output is correct |
49 |
Correct |
12 ms |
17192 KB |
Output is correct |
50 |
Correct |
85 ms |
24912 KB |
Output is correct |
51 |
Correct |
11 ms |
16472 KB |
Output is correct |
52 |
Correct |
75 ms |
22852 KB |
Output is correct |
53 |
Correct |
11 ms |
16984 KB |
Output is correct |
54 |
Correct |
77 ms |
23864 KB |
Output is correct |
55 |
Correct |
8 ms |
16732 KB |
Output is correct |
56 |
Correct |
11 ms |
16732 KB |
Output is correct |
57 |
Correct |
7 ms |
16476 KB |
Output is correct |
58 |
Correct |
9 ms |
16476 KB |
Output is correct |
59 |
Correct |
7 ms |
16732 KB |
Output is correct |
60 |
Correct |
61 ms |
21440 KB |
Output is correct |
61 |
Correct |
10 ms |
16476 KB |
Output is correct |
62 |
Correct |
80 ms |
24260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15712 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15708 KB |
Output is correct |
4 |
Correct |
4 ms |
15908 KB |
Output is correct |
5 |
Correct |
4 ms |
15704 KB |
Output is correct |
6 |
Correct |
4 ms |
15704 KB |
Output is correct |
7 |
Correct |
4 ms |
15964 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
5 ms |
15900 KB |
Output is correct |
10 |
Correct |
3 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
4 ms |
15708 KB |
Output is correct |
13 |
Correct |
4 ms |
15708 KB |
Output is correct |
14 |
Correct |
4 ms |
15708 KB |
Output is correct |
15 |
Correct |
4 ms |
15704 KB |
Output is correct |
16 |
Correct |
4 ms |
15944 KB |
Output is correct |
17 |
Correct |
4 ms |
15708 KB |
Output is correct |
18 |
Correct |
4 ms |
15964 KB |
Output is correct |
19 |
Correct |
4 ms |
15708 KB |
Output is correct |
20 |
Correct |
5 ms |
15964 KB |
Output is correct |
21 |
Correct |
5 ms |
15796 KB |
Output is correct |
22 |
Correct |
4 ms |
15960 KB |
Output is correct |
23 |
Correct |
4 ms |
15988 KB |
Output is correct |
24 |
Correct |
4 ms |
15960 KB |
Output is correct |
25 |
Correct |
6 ms |
15964 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
5 ms |
15960 KB |
Output is correct |
29 |
Correct |
4 ms |
15704 KB |
Output is correct |
30 |
Correct |
4 ms |
15704 KB |
Output is correct |
31 |
Correct |
5 ms |
15964 KB |
Output is correct |
32 |
Correct |
4 ms |
15708 KB |
Output is correct |
33 |
Correct |
4 ms |
16216 KB |
Output is correct |
34 |
Correct |
7 ms |
15960 KB |
Output is correct |
35 |
Correct |
65 ms |
21844 KB |
Output is correct |
36 |
Correct |
88 ms |
24716 KB |
Output is correct |
37 |
Correct |
109 ms |
24800 KB |
Output is correct |
38 |
Correct |
82 ms |
24400 KB |
Output is correct |
39 |
Correct |
6 ms |
16220 KB |
Output is correct |
40 |
Correct |
9 ms |
16548 KB |
Output is correct |
41 |
Correct |
7 ms |
16476 KB |
Output is correct |
42 |
Correct |
6 ms |
16216 KB |
Output is correct |
43 |
Correct |
6 ms |
16220 KB |
Output is correct |
44 |
Correct |
10 ms |
16220 KB |
Output is correct |
45 |
Correct |
8 ms |
16220 KB |
Output is correct |
46 |
Correct |
6 ms |
16220 KB |
Output is correct |
47 |
Correct |
7 ms |
16472 KB |
Output is correct |
48 |
Correct |
8 ms |
16476 KB |
Output is correct |
49 |
Correct |
12 ms |
17192 KB |
Output is correct |
50 |
Correct |
85 ms |
24912 KB |
Output is correct |
51 |
Correct |
11 ms |
16472 KB |
Output is correct |
52 |
Correct |
75 ms |
22852 KB |
Output is correct |
53 |
Correct |
11 ms |
16984 KB |
Output is correct |
54 |
Correct |
77 ms |
23864 KB |
Output is correct |
55 |
Correct |
8 ms |
16732 KB |
Output is correct |
56 |
Correct |
11 ms |
16732 KB |
Output is correct |
57 |
Correct |
7 ms |
16476 KB |
Output is correct |
58 |
Correct |
9 ms |
16476 KB |
Output is correct |
59 |
Correct |
7 ms |
16732 KB |
Output is correct |
60 |
Correct |
61 ms |
21440 KB |
Output is correct |
61 |
Correct |
10 ms |
16476 KB |
Output is correct |
62 |
Correct |
80 ms |
24260 KB |
Output is correct |
63 |
Correct |
497 ms |
68432 KB |
Output is correct |
64 |
Correct |
491 ms |
68436 KB |
Output is correct |
65 |
Correct |
509 ms |
68452 KB |
Output is correct |
66 |
Correct |
186 ms |
34460 KB |
Output is correct |
67 |
Correct |
370 ms |
61380 KB |
Output is correct |
68 |
Correct |
160 ms |
34600 KB |
Output is correct |
69 |
Correct |
294 ms |
35744 KB |
Output is correct |
70 |
Correct |
185 ms |
34384 KB |
Output is correct |
71 |
Correct |
208 ms |
34548 KB |
Output is correct |
72 |
Correct |
388 ms |
45348 KB |
Output is correct |
73 |
Correct |
385 ms |
47188 KB |
Output is correct |
74 |
Correct |
856 ms |
69424 KB |
Output is correct |
75 |
Correct |
440 ms |
42324 KB |
Output is correct |
76 |
Correct |
628 ms |
54308 KB |
Output is correct |
77 |
Correct |
745 ms |
54724 KB |
Output is correct |
78 |
Correct |
197 ms |
38272 KB |
Output is correct |
79 |
Correct |
333 ms |
42600 KB |
Output is correct |
80 |
Correct |
176 ms |
37200 KB |
Output is correct |
81 |
Correct |
283 ms |
40528 KB |
Output is correct |
82 |
Correct |
696 ms |
55284 KB |
Output is correct |
83 |
Correct |
723 ms |
55376 KB |
Output is correct |
84 |
Correct |
540 ms |
53772 KB |
Output is correct |
85 |
Correct |
608 ms |
53720 KB |
Output is correct |
86 |
Correct |
279 ms |
67324 KB |
Output is correct |
87 |
Correct |
423 ms |
69452 KB |
Output is correct |
88 |
Correct |
437 ms |
46416 KB |
Output is correct |
89 |
Correct |
670 ms |
52748 KB |
Output is correct |