#define ii pair<int , int>
#define ll long long
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n , m;
ll ans = 0;
int parent[N];
ll sz[N];
set<ii> edge[N];
set<int> group[N];
int find_set(int u)
{
return parent[u] == u ? u : find_set(parent[u]);
}
void join(int U , int V)
{
int u = find_set(U) , v = find_set(V);
if (u == v) return;
auto it = edge[v].lower_bound(make_pair(u , 0));
if (it != edge[v].end() && it->first == u)
{
ans += sz[u] * sz[v] * 2;
if (edge[u].size() + group[u].size() > edge[v].size() + group[v].size()) swap(u , v);
vector<ii> new_edge;
for (auto e : edge[u])
{
group[e.first].erase(e.second);
ans -= sz[e.first];
if (e.first != v) new_edge.push_back(e);
}
edge[u].clear();
ans -= sz[u] * group[u].size();
ans += sz[u] * group[v].size();
vector<int> new_group;
for (auto X : group[u])
{
int x = find_set(X);
edge[x].erase({u , X});
if (x != v) new_group.push_back(X);
}
group[u].clear();
parent[u] = v , sz[v] += sz[u];
for (auto e : new_edge) join(e.second , e.first);
for (auto X : new_group) join(X , v);
}
else if (!group[v].count(U))
{
ans += sz[v];
edge[u].emplace(make_pair(v , U));
group[v].insert(U);
}
}
int main()
{
// freopen(".inp","r",stdin);
// freopen(".out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1 ; i <= n ; ++i)
{
parent[i] = i;
sz[i] = 1;
}
while(m--)
{
int U , V;
cin >> U >> V;
join(U , V);
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9852 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
9 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
9 ms |
9836 KB |
Output is correct |
22 |
Correct |
8 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9708 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9708 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
8 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9856 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
7 ms |
9784 KB |
Output is correct |
33 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9852 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
9 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
9 ms |
9836 KB |
Output is correct |
22 |
Correct |
8 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9708 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9708 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
8 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9856 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
7 ms |
9784 KB |
Output is correct |
33 |
Correct |
7 ms |
9708 KB |
Output is correct |
34 |
Correct |
9 ms |
9836 KB |
Output is correct |
35 |
Correct |
90 ms |
12908 KB |
Output is correct |
36 |
Correct |
113 ms |
14848 KB |
Output is correct |
37 |
Correct |
112 ms |
14828 KB |
Output is correct |
38 |
Correct |
112 ms |
14572 KB |
Output is correct |
39 |
Correct |
8 ms |
9836 KB |
Output is correct |
40 |
Correct |
9 ms |
9836 KB |
Output is correct |
41 |
Correct |
10 ms |
9836 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9836 KB |
Output is correct |
44 |
Correct |
9 ms |
9836 KB |
Output is correct |
45 |
Correct |
8 ms |
9836 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
9 ms |
9836 KB |
Output is correct |
48 |
Correct |
9 ms |
9836 KB |
Output is correct |
49 |
Correct |
15 ms |
10348 KB |
Output is correct |
50 |
Correct |
112 ms |
14828 KB |
Output is correct |
51 |
Correct |
12 ms |
10092 KB |
Output is correct |
52 |
Correct |
100 ms |
13676 KB |
Output is correct |
53 |
Correct |
17 ms |
10348 KB |
Output is correct |
54 |
Correct |
121 ms |
14188 KB |
Output is correct |
55 |
Correct |
10 ms |
10220 KB |
Output is correct |
56 |
Correct |
10 ms |
10220 KB |
Output is correct |
57 |
Correct |
11 ms |
10220 KB |
Output is correct |
58 |
Correct |
11 ms |
10220 KB |
Output is correct |
59 |
Correct |
8 ms |
9836 KB |
Output is correct |
60 |
Correct |
100 ms |
12140 KB |
Output is correct |
61 |
Correct |
10 ms |
9964 KB |
Output is correct |
62 |
Correct |
108 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9852 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
8 ms |
9708 KB |
Output is correct |
10 |
Correct |
9 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
7 ms |
9708 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9708 KB |
Output is correct |
19 |
Correct |
7 ms |
9708 KB |
Output is correct |
20 |
Correct |
7 ms |
9708 KB |
Output is correct |
21 |
Correct |
9 ms |
9836 KB |
Output is correct |
22 |
Correct |
8 ms |
9856 KB |
Output is correct |
23 |
Correct |
7 ms |
9708 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9708 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
8 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9856 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
7 ms |
9784 KB |
Output is correct |
33 |
Correct |
7 ms |
9708 KB |
Output is correct |
34 |
Correct |
9 ms |
9836 KB |
Output is correct |
35 |
Correct |
90 ms |
12908 KB |
Output is correct |
36 |
Correct |
113 ms |
14848 KB |
Output is correct |
37 |
Correct |
112 ms |
14828 KB |
Output is correct |
38 |
Correct |
112 ms |
14572 KB |
Output is correct |
39 |
Correct |
8 ms |
9836 KB |
Output is correct |
40 |
Correct |
9 ms |
9836 KB |
Output is correct |
41 |
Correct |
10 ms |
9836 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9836 KB |
Output is correct |
44 |
Correct |
9 ms |
9836 KB |
Output is correct |
45 |
Correct |
8 ms |
9836 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
9 ms |
9836 KB |
Output is correct |
48 |
Correct |
9 ms |
9836 KB |
Output is correct |
49 |
Correct |
15 ms |
10348 KB |
Output is correct |
50 |
Correct |
112 ms |
14828 KB |
Output is correct |
51 |
Correct |
12 ms |
10092 KB |
Output is correct |
52 |
Correct |
100 ms |
13676 KB |
Output is correct |
53 |
Correct |
17 ms |
10348 KB |
Output is correct |
54 |
Correct |
121 ms |
14188 KB |
Output is correct |
55 |
Correct |
10 ms |
10220 KB |
Output is correct |
56 |
Correct |
10 ms |
10220 KB |
Output is correct |
57 |
Correct |
11 ms |
10220 KB |
Output is correct |
58 |
Correct |
11 ms |
10220 KB |
Output is correct |
59 |
Correct |
8 ms |
9836 KB |
Output is correct |
60 |
Correct |
100 ms |
12140 KB |
Output is correct |
61 |
Correct |
10 ms |
9964 KB |
Output is correct |
62 |
Correct |
108 ms |
14444 KB |
Output is correct |
63 |
Correct |
413 ms |
41068 KB |
Output is correct |
64 |
Correct |
418 ms |
41068 KB |
Output is correct |
65 |
Correct |
423 ms |
41184 KB |
Output is correct |
66 |
Correct |
93 ms |
13036 KB |
Output is correct |
67 |
Correct |
175 ms |
19308 KB |
Output is correct |
68 |
Correct |
98 ms |
15596 KB |
Output is correct |
69 |
Correct |
284 ms |
19948 KB |
Output is correct |
70 |
Correct |
98 ms |
15340 KB |
Output is correct |
71 |
Correct |
96 ms |
15340 KB |
Output is correct |
72 |
Correct |
228 ms |
19436 KB |
Output is correct |
73 |
Correct |
222 ms |
19436 KB |
Output is correct |
74 |
Correct |
717 ms |
28652 KB |
Output is correct |
75 |
Correct |
396 ms |
24684 KB |
Output is correct |
76 |
Correct |
522 ms |
27756 KB |
Output is correct |
77 |
Correct |
546 ms |
27884 KB |
Output is correct |
78 |
Correct |
163 ms |
18924 KB |
Output is correct |
79 |
Correct |
286 ms |
22764 KB |
Output is correct |
80 |
Correct |
145 ms |
18924 KB |
Output is correct |
81 |
Correct |
264 ms |
22380 KB |
Output is correct |
82 |
Correct |
508 ms |
33388 KB |
Output is correct |
83 |
Correct |
506 ms |
33260 KB |
Output is correct |
84 |
Correct |
471 ms |
33772 KB |
Output is correct |
85 |
Correct |
444 ms |
33644 KB |
Output is correct |
86 |
Correct |
99 ms |
14572 KB |
Output is correct |
87 |
Correct |
139 ms |
16748 KB |
Output is correct |
88 |
Correct |
233 ms |
19564 KB |
Output is correct |
89 |
Correct |
481 ms |
27116 KB |
Output is correct |