#define ii pair<int , int>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n , m , ans;
int parent[N] , 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 |
7 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 |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
8 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
9 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 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 |
9708 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 |
9708 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 |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9708 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 |
9836 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
7 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9708 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
8 ms |
9708 KB |
Output is correct |
33 |
Correct |
9 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 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 |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
8 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
9 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 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 |
9708 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 |
9708 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 |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9708 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 |
9836 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
7 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9708 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
8 ms |
9708 KB |
Output is correct |
33 |
Correct |
9 ms |
9836 KB |
Output is correct |
34 |
Correct |
10 ms |
9836 KB |
Output is correct |
35 |
Correct |
96 ms |
13476 KB |
Output is correct |
36 |
Correct |
114 ms |
15084 KB |
Output is correct |
37 |
Correct |
116 ms |
15084 KB |
Output is correct |
38 |
Correct |
127 ms |
14916 KB |
Output is correct |
39 |
Correct |
8 ms |
9836 KB |
Output is correct |
40 |
Correct |
9 ms |
9964 KB |
Output is correct |
41 |
Correct |
9 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9964 KB |
Output is correct |
44 |
Correct |
9 ms |
9964 KB |
Output is correct |
45 |
Correct |
8 ms |
9836 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
11 ms |
10112 KB |
Output is correct |
48 |
Correct |
11 ms |
9964 KB |
Output is correct |
49 |
Correct |
16 ms |
10476 KB |
Output is correct |
50 |
Correct |
116 ms |
15084 KB |
Output is correct |
51 |
Correct |
13 ms |
10092 KB |
Output is correct |
52 |
Correct |
103 ms |
14060 KB |
Output is correct |
53 |
Correct |
15 ms |
10476 KB |
Output is correct |
54 |
Correct |
109 ms |
14784 KB |
Output is correct |
55 |
Correct |
11 ms |
10220 KB |
Output is correct |
56 |
Correct |
11 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 |
103 ms |
12524 KB |
Output is correct |
61 |
Correct |
10 ms |
9964 KB |
Output is correct |
62 |
Correct |
114 ms |
14828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 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 |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
8 ms |
9708 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
9 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 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 |
9708 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 |
9708 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 |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9708 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 |
9836 KB |
Output is correct |
26 |
Correct |
7 ms |
9708 KB |
Output is correct |
27 |
Correct |
7 ms |
9708 KB |
Output is correct |
28 |
Correct |
7 ms |
9708 KB |
Output is correct |
29 |
Correct |
7 ms |
9708 KB |
Output is correct |
30 |
Correct |
7 ms |
9708 KB |
Output is correct |
31 |
Correct |
7 ms |
9708 KB |
Output is correct |
32 |
Correct |
8 ms |
9708 KB |
Output is correct |
33 |
Correct |
9 ms |
9836 KB |
Output is correct |
34 |
Correct |
10 ms |
9836 KB |
Output is correct |
35 |
Correct |
96 ms |
13476 KB |
Output is correct |
36 |
Correct |
114 ms |
15084 KB |
Output is correct |
37 |
Correct |
116 ms |
15084 KB |
Output is correct |
38 |
Correct |
127 ms |
14916 KB |
Output is correct |
39 |
Correct |
8 ms |
9836 KB |
Output is correct |
40 |
Correct |
9 ms |
9964 KB |
Output is correct |
41 |
Correct |
9 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9836 KB |
Output is correct |
43 |
Correct |
9 ms |
9964 KB |
Output is correct |
44 |
Correct |
9 ms |
9964 KB |
Output is correct |
45 |
Correct |
8 ms |
9836 KB |
Output is correct |
46 |
Correct |
8 ms |
9836 KB |
Output is correct |
47 |
Correct |
11 ms |
10112 KB |
Output is correct |
48 |
Correct |
11 ms |
9964 KB |
Output is correct |
49 |
Correct |
16 ms |
10476 KB |
Output is correct |
50 |
Correct |
116 ms |
15084 KB |
Output is correct |
51 |
Correct |
13 ms |
10092 KB |
Output is correct |
52 |
Correct |
103 ms |
14060 KB |
Output is correct |
53 |
Correct |
15 ms |
10476 KB |
Output is correct |
54 |
Correct |
109 ms |
14784 KB |
Output is correct |
55 |
Correct |
11 ms |
10220 KB |
Output is correct |
56 |
Correct |
11 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 |
103 ms |
12524 KB |
Output is correct |
61 |
Correct |
10 ms |
9964 KB |
Output is correct |
62 |
Correct |
114 ms |
14828 KB |
Output is correct |
63 |
Correct |
435 ms |
41092 KB |
Output is correct |
64 |
Correct |
434 ms |
41212 KB |
Output is correct |
65 |
Correct |
431 ms |
41228 KB |
Output is correct |
66 |
Incorrect |
97 ms |
13036 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |