#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <set>
#include <map>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXM = 300000 + 10;
const int MOD = 1e9 + 7;
int n, m;
llong answer;
struct DSU
{
int sz[MAXN];
int par[MAXN];
std::map <int,int> fromComp[MAXN];
std::set <int> pointingNodes[MAXN];
std::set <int> fromNode[MAXN];
std::set <int> nodes[MAXN];
void build()
{
std::fill(sz + 1, sz + 1 + n, 1);
std::iota(par + 1, par + 1 + n, 1);
for (int i = 1 ; i <= n ; ++i)
{
nodes[i].insert(i);
}
}
int find(int node)
{
if (par[node] == node) return node;
return par[node] = find(par[node]);
}
void merge(int cU, int cV)
{
cU = find(cU);
cV = find(cV);
if (cU == cV)
{
return;
}
answer -= 1LL * fromComp[cV][cU] * nodes[cU].size();
answer -= 1LL * fromComp[cU][cV] * nodes[cV].size();
answer += 2LL * nodes[cU].size() * nodes[cV].size();
if (nodes[cV].size() > nodes[cU].size())
{
std::swap(cV, cU);
}
// std::cout << "merge: " << cU << ' ' << cV << '\n';
std::queue <int> waitlist;
for (const int &node : pointingNodes[cV])
{
if (nodes[cU].count(node))
{
waitlist.push(node);
}
}
for (const int &node : nodes[cV])
{
if (fromNode[node].count(cU))
{
// std::cout << "erase1: " << node << "\n";
pointingNodes[cU].erase(pointingNodes[cU].find(node));
fromNode[node].erase(fromNode[node].find(cU));
}
}
fromComp[cU][cV] = 0;
fromComp[cV][cU] = 0;
while (waitlist.size())
{
// std::cout << "erase2\n";
pointingNodes[cV].erase(pointingNodes[cV].find(waitlist.front()));
waitlist.pop();
}
std::queue <std::pair <int,int>> toMerge;
for (const int &node : pointingNodes[cV])
{
if (fromComp[cU][find(node)] > 0)
{
toMerge.push({cU, find(node)});
}
}
for (const auto &[key, value] : fromComp[cV])
{
if (value > 0 && fromComp[key][cU] > 0)
{
toMerge.push({cU, key});
}
}
answer -= 1LL * nodes[cV].size() * pointingNodes[cV].size();
for (const int &node : pointingNodes[cV])
{
fromNode[node].erase(fromNode[node].find(cV));
fromComp[find(node)][cV]--;
bool inserted = pointingNodes[cU].insert(node).second;
if (inserted)
{
answer += nodes[cU].size();
fromNode[node].insert(cU);
// std::cout << "add: " << find(node) << ' ' << cU << '\n';
fromComp[find(node)][cU]++;
}
}
answer += 1LL * pointingNodes[cU].size() * nodes[cV].size();
for (const auto &[key, value] : fromComp[cV])
{
// std::cout << "add: " << cU << ' ' << key << ' ' << value << '\n';
fromComp[cU][key] += value;
}
for (const int &node : nodes[cV])
{
nodes[cU].insert(node);
}
par[cV] = cU;
while (toMerge.size())
{
merge(toMerge.front().first, toMerge.front().second);
toMerge.pop();
}
}
void addEdge(int u, int v)
{
int cU = find(u);
int cV = find(v);
if (cU == cV)
{
return;
}
// std::cout << "add edge: " << u << ' ' << cV << '\n';
if (fromNode[u].count(cV))
{
return;
}
if (fromComp[cV][cU] == 0)
{
answer += nodes[cV].size();
pointingNodes[cV].insert(u);
fromNode[u].insert(cV);
fromComp[cU][cV]++;
return;
}
// assert(fromComp[cU][cV] == 0);
merge(cU, cV);
}
};
DSU dsu;
int from[MAXM];
int to[MAXM];
void solve()
{
dsu.build();
for (int i = 1 ; i <= m ; ++i)
{
dsu.addEdge(from[i], to[i]);
std::cout << answer << '\n';
}
}
void input()
{
std::cin >> n >> m;
for (int i = 1 ; i <= m ; ++i)
{
std::cin >> from[i] >> to[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
/*
14 13
10 14
3 10
14 13
1 3
3 5
3 11
12 14
14 6
11 8
2 3
7 8
9 7
1 4
5 6
4 2
1 5
2 3
2 5
3 2
3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
22104 KB |
Output is correct |
2 |
Correct |
6 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
6 ms |
22108 KB |
Output is correct |
5 |
Correct |
6 ms |
22108 KB |
Output is correct |
6 |
Correct |
5 ms |
22212 KB |
Output is correct |
7 |
Correct |
6 ms |
22108 KB |
Output is correct |
8 |
Correct |
7 ms |
22108 KB |
Output is correct |
9 |
Correct |
6 ms |
22108 KB |
Output is correct |
10 |
Correct |
6 ms |
22108 KB |
Output is correct |
11 |
Correct |
7 ms |
22108 KB |
Output is correct |
12 |
Correct |
5 ms |
22108 KB |
Output is correct |
13 |
Correct |
6 ms |
22108 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
6 ms |
22224 KB |
Output is correct |
16 |
Correct |
5 ms |
22108 KB |
Output is correct |
17 |
Correct |
5 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
22108 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
22180 KB |
Output is correct |
21 |
Correct |
6 ms |
22108 KB |
Output is correct |
22 |
Correct |
6 ms |
22204 KB |
Output is correct |
23 |
Correct |
6 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22216 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22108 KB |
Output is correct |
27 |
Correct |
6 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22360 KB |
Output is correct |
29 |
Correct |
6 ms |
22108 KB |
Output is correct |
30 |
Correct |
5 ms |
22196 KB |
Output is correct |
31 |
Correct |
5 ms |
22220 KB |
Output is correct |
32 |
Correct |
5 ms |
22196 KB |
Output is correct |
33 |
Correct |
6 ms |
22108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
22104 KB |
Output is correct |
2 |
Correct |
6 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
6 ms |
22108 KB |
Output is correct |
5 |
Correct |
6 ms |
22108 KB |
Output is correct |
6 |
Correct |
5 ms |
22212 KB |
Output is correct |
7 |
Correct |
6 ms |
22108 KB |
Output is correct |
8 |
Correct |
7 ms |
22108 KB |
Output is correct |
9 |
Correct |
6 ms |
22108 KB |
Output is correct |
10 |
Correct |
6 ms |
22108 KB |
Output is correct |
11 |
Correct |
7 ms |
22108 KB |
Output is correct |
12 |
Correct |
5 ms |
22108 KB |
Output is correct |
13 |
Correct |
6 ms |
22108 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
6 ms |
22224 KB |
Output is correct |
16 |
Correct |
5 ms |
22108 KB |
Output is correct |
17 |
Correct |
5 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
22108 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
22180 KB |
Output is correct |
21 |
Correct |
6 ms |
22108 KB |
Output is correct |
22 |
Correct |
6 ms |
22204 KB |
Output is correct |
23 |
Correct |
6 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22216 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22108 KB |
Output is correct |
27 |
Correct |
6 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22360 KB |
Output is correct |
29 |
Correct |
6 ms |
22108 KB |
Output is correct |
30 |
Correct |
5 ms |
22196 KB |
Output is correct |
31 |
Correct |
5 ms |
22220 KB |
Output is correct |
32 |
Correct |
5 ms |
22196 KB |
Output is correct |
33 |
Correct |
6 ms |
22108 KB |
Output is correct |
34 |
Correct |
8 ms |
22512 KB |
Output is correct |
35 |
Correct |
66 ms |
29780 KB |
Output is correct |
36 |
Correct |
90 ms |
34884 KB |
Output is correct |
37 |
Correct |
87 ms |
34920 KB |
Output is correct |
38 |
Correct |
84 ms |
34508 KB |
Output is correct |
39 |
Correct |
8 ms |
22816 KB |
Output is correct |
40 |
Correct |
11 ms |
23388 KB |
Output is correct |
41 |
Correct |
10 ms |
23244 KB |
Output is correct |
42 |
Correct |
8 ms |
22620 KB |
Output is correct |
43 |
Correct |
8 ms |
22732 KB |
Output is correct |
44 |
Correct |
9 ms |
22620 KB |
Output is correct |
45 |
Correct |
8 ms |
22620 KB |
Output is correct |
46 |
Correct |
8 ms |
22620 KB |
Output is correct |
47 |
Correct |
10 ms |
23132 KB |
Output is correct |
48 |
Correct |
10 ms |
23132 KB |
Output is correct |
49 |
Correct |
18 ms |
25120 KB |
Output is correct |
50 |
Correct |
90 ms |
35152 KB |
Output is correct |
51 |
Correct |
12 ms |
23128 KB |
Output is correct |
52 |
Correct |
74 ms |
32736 KB |
Output is correct |
53 |
Correct |
17 ms |
24668 KB |
Output is correct |
54 |
Correct |
81 ms |
33780 KB |
Output is correct |
55 |
Correct |
11 ms |
23128 KB |
Output is correct |
56 |
Correct |
11 ms |
23132 KB |
Output is correct |
57 |
Correct |
13 ms |
26352 KB |
Output is correct |
58 |
Correct |
13 ms |
26204 KB |
Output is correct |
59 |
Correct |
10 ms |
23388 KB |
Output is correct |
60 |
Correct |
58 ms |
28384 KB |
Output is correct |
61 |
Correct |
10 ms |
23128 KB |
Output is correct |
62 |
Correct |
84 ms |
34244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
22104 KB |
Output is correct |
2 |
Correct |
6 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
6 ms |
22108 KB |
Output is correct |
5 |
Correct |
6 ms |
22108 KB |
Output is correct |
6 |
Correct |
5 ms |
22212 KB |
Output is correct |
7 |
Correct |
6 ms |
22108 KB |
Output is correct |
8 |
Correct |
7 ms |
22108 KB |
Output is correct |
9 |
Correct |
6 ms |
22108 KB |
Output is correct |
10 |
Correct |
6 ms |
22108 KB |
Output is correct |
11 |
Correct |
7 ms |
22108 KB |
Output is correct |
12 |
Correct |
5 ms |
22108 KB |
Output is correct |
13 |
Correct |
6 ms |
22108 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
6 ms |
22224 KB |
Output is correct |
16 |
Correct |
5 ms |
22108 KB |
Output is correct |
17 |
Correct |
5 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
22108 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
22180 KB |
Output is correct |
21 |
Correct |
6 ms |
22108 KB |
Output is correct |
22 |
Correct |
6 ms |
22204 KB |
Output is correct |
23 |
Correct |
6 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22216 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22108 KB |
Output is correct |
27 |
Correct |
6 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22360 KB |
Output is correct |
29 |
Correct |
6 ms |
22108 KB |
Output is correct |
30 |
Correct |
5 ms |
22196 KB |
Output is correct |
31 |
Correct |
5 ms |
22220 KB |
Output is correct |
32 |
Correct |
5 ms |
22196 KB |
Output is correct |
33 |
Correct |
6 ms |
22108 KB |
Output is correct |
34 |
Correct |
8 ms |
22512 KB |
Output is correct |
35 |
Correct |
66 ms |
29780 KB |
Output is correct |
36 |
Correct |
90 ms |
34884 KB |
Output is correct |
37 |
Correct |
87 ms |
34920 KB |
Output is correct |
38 |
Correct |
84 ms |
34508 KB |
Output is correct |
39 |
Correct |
8 ms |
22816 KB |
Output is correct |
40 |
Correct |
11 ms |
23388 KB |
Output is correct |
41 |
Correct |
10 ms |
23244 KB |
Output is correct |
42 |
Correct |
8 ms |
22620 KB |
Output is correct |
43 |
Correct |
8 ms |
22732 KB |
Output is correct |
44 |
Correct |
9 ms |
22620 KB |
Output is correct |
45 |
Correct |
8 ms |
22620 KB |
Output is correct |
46 |
Correct |
8 ms |
22620 KB |
Output is correct |
47 |
Correct |
10 ms |
23132 KB |
Output is correct |
48 |
Correct |
10 ms |
23132 KB |
Output is correct |
49 |
Correct |
18 ms |
25120 KB |
Output is correct |
50 |
Correct |
90 ms |
35152 KB |
Output is correct |
51 |
Correct |
12 ms |
23128 KB |
Output is correct |
52 |
Correct |
74 ms |
32736 KB |
Output is correct |
53 |
Correct |
17 ms |
24668 KB |
Output is correct |
54 |
Correct |
81 ms |
33780 KB |
Output is correct |
55 |
Correct |
11 ms |
23128 KB |
Output is correct |
56 |
Correct |
11 ms |
23132 KB |
Output is correct |
57 |
Correct |
13 ms |
26352 KB |
Output is correct |
58 |
Correct |
13 ms |
26204 KB |
Output is correct |
59 |
Correct |
10 ms |
23388 KB |
Output is correct |
60 |
Correct |
58 ms |
28384 KB |
Output is correct |
61 |
Correct |
10 ms |
23128 KB |
Output is correct |
62 |
Correct |
84 ms |
34244 KB |
Output is correct |
63 |
Correct |
484 ms |
88876 KB |
Output is correct |
64 |
Correct |
499 ms |
88868 KB |
Output is correct |
65 |
Correct |
526 ms |
88872 KB |
Output is correct |
66 |
Correct |
255 ms |
50048 KB |
Output is correct |
67 |
Correct |
534 ms |
103344 KB |
Output is correct |
68 |
Correct |
244 ms |
50260 KB |
Output is correct |
69 |
Correct |
341 ms |
47924 KB |
Output is correct |
70 |
Correct |
275 ms |
50260 KB |
Output is correct |
71 |
Correct |
266 ms |
50260 KB |
Output is correct |
72 |
Correct |
511 ms |
72148 KB |
Output is correct |
73 |
Correct |
498 ms |
75564 KB |
Output is correct |
74 |
Correct |
1283 ms |
126816 KB |
Output is correct |
75 |
Correct |
531 ms |
57292 KB |
Output is correct |
76 |
Correct |
832 ms |
86116 KB |
Output is correct |
77 |
Correct |
836 ms |
86592 KB |
Output is correct |
78 |
Correct |
222 ms |
51540 KB |
Output is correct |
79 |
Correct |
376 ms |
59088 KB |
Output is correct |
80 |
Correct |
212 ms |
50256 KB |
Output is correct |
81 |
Correct |
346 ms |
54340 KB |
Output is correct |
82 |
Correct |
773 ms |
75092 KB |
Output is correct |
83 |
Correct |
748 ms |
75192 KB |
Output is correct |
84 |
Correct |
874 ms |
229460 KB |
Output is correct |
85 |
Correct |
862 ms |
229460 KB |
Output is correct |
86 |
Correct |
432 ms |
114040 KB |
Output is correct |
87 |
Correct |
453 ms |
116560 KB |
Output is correct |
88 |
Correct |
516 ms |
74320 KB |
Output is correct |
89 |
Correct |
824 ms |
80088 KB |
Output is correct |