// Solution:
// Let a component denote a set of vertices such that each vertex has an outgoing edge to every
// other vertex in the component
//
// There are 2 cases when adding an edge:
//
// If we add an edge from a vertex v to a component c, then there will form an edge from v to
// every node in component c.
//
// If we add an edge from a component a to a component b, and there exists an edge from b to a,
// then a and b will be merged into one component (a complete directed simple graph).
//
// We can maintain these edges and components using a set, merging them with the weighted union
// heuristic to achieve O(n log n) merges. If we use a map of sets, each merge takes O(log^2).
// This can be sped up using gp_hash_table of gp_hash_tables, speeding up each merge to O(1)
// amortized complexity.
//
// Time: O(n log n)
// Memory: O(n)
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 100005;
const int MAXM = 300005;
int N, M;
int A[MAXM], B[MAXM];
long long Answer = 0;
void read() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
A[i]--, B[i]--;
}
}
namespace disjoint_set { // maintain disjoint set of components
int p[MAXN];
int component_size[MAXN];
void Init() {
iota(p, p + MAXN, 0);
fill(component_size, component_size + MAXN, 1);
}
int Find(int x) {
return p[x] == x ? x : p[x] = Find(p[x]);
}
}
namespace ingoing_edges {
map<int, set<int>> ingoing_edges_from_component[MAXN]; // set of ingoing edges from other components to component[] (a list oh them), thus number of edges = size() * component[].size()
int total_size_ingoing_edges_from_component[MAXN]; // number of all element of elements of ingoing_edges_from_component[]
void Insert(int base, int ingoing_component, int ingoing_id) {
assert(ingoing_component == disjoint_set::Find(ingoing_id));
if (ingoing_edges_from_component[base][ingoing_component].count(ingoing_id) == 0) {
total_size_ingoing_edges_from_component[base]++;
ingoing_edges_from_component[base][ingoing_component].emplace(ingoing_id);
}
}
void Delete(int base, int ingoing_component, int ingoing_id) {
assert(ingoing_component == disjoint_set::Find(ingoing_id));
if (ingoing_edges_from_component[base][ingoing_component].count(ingoing_id) == 1) {
total_size_ingoing_edges_from_component[base]--;
ingoing_edges_from_component[base][ingoing_component].erase(ingoing_id);
}
}
void Delete(int base, int ingoing_component) {
total_size_ingoing_edges_from_component[base] -= ingoing_edges_from_component[base][ingoing_component].size();
ingoing_edges_from_component[base].erase(ingoing_component);
}
}
namespace outgoing_edges {
map<int, int> edges_between_components[MAXN]; // count how many edges are there from component[] to another component divided by another component.size()
int total_size_outgoing_edges_to_component[MAXN]; // sum of all edges_between_component[]'s value
void Insert(int from, int to, int x) {
total_size_outgoing_edges_to_component[from] += x;
edges_between_components[from][to] += x;
}
void Delete(int from, int to, int x) {
total_size_outgoing_edges_to_component[from] -= x;
edges_between_components[from][to] -= x;
if (edges_between_components[from][to] == 0) {
edges_between_components[from].erase(to);
}
}
void Delete(int from, int to) {
total_size_outgoing_edges_to_component[from] -= edges_between_components[from][to];
edges_between_components[from].erase(to);
}
}
using ingoing_edges::ingoing_edges_from_component;
using ingoing_edges::total_size_ingoing_edges_from_component;
using outgoing_edges::edges_between_components;
using outgoing_edges::total_size_outgoing_edges_to_component;
bool IsThereAnEdgeBetweenComponent(int x, int y) {
return (edges_between_components[x].count(y) == 1);
}
long long ComponentAnswer(int sz) { // count number of edges in one component (a component is a complete directed graph)
return 1ll * sz * (sz - 1);
}
set<pair<int, int>> todo; // pending to use ConnectComponent(x, y)
void ConnectComponent(int x, int y) {
x = disjoint_set::Find(x);
y = disjoint_set::Find(y);
if (x == y) return;
int components_x_size = disjoint_set::component_size[x];
int components_y_size = disjoint_set::component_size[y];
{ // delete edges between components to be connected
Answer -= 1ll * edges_between_components[y][x] * components_x_size;
Answer -= 1ll * edges_between_components[x][y] * components_y_size;
outgoing_edges::Delete(x, y);
outgoing_edges::Delete(y, x);
ingoing_edges::Delete(x, y);
ingoing_edges::Delete(y, x);
}
{ // maintain weighted union heuristic and update disjoint set
int total_size_x = total_size_ingoing_edges_from_component[x] + total_size_outgoing_edges_to_component[x];
int total_size_y = total_size_ingoing_edges_from_component[y] + total_size_outgoing_edges_to_component[y];
if (total_size_x < total_size_y) {
swap(x, y); // This still is affected by the weighted union heuristic, thus the will take O(n log n) merges total
swap(components_x_size, components_y_size);
}
// Union in disjoint set
disjoint_set::p[y] = x;
disjoint_set::component_size[x] += disjoint_set::component_size[y];
disjoint_set::component_size[y] = 0;
}
{ // update answer for full component
Answer -= ComponentAnswer(components_x_size);
Answer -= ComponentAnswer(components_y_size);
Answer += ComponentAnswer(components_x_size + components_y_size);
}
{ // handle all merges of ingoing edge of x and y
Answer += 1ll * total_size_ingoing_edges_from_component[x] * components_y_size;
Answer += 1ll * total_size_ingoing_edges_from_component[y] * components_x_size;
while (!ingoing_edges_from_component[y].empty()) {
auto ingoing_edges_from_component_y_key_value_pair = begin(ingoing_edges_from_component[y]);
int current_ingoing_component = ingoing_edges_from_component_y_key_value_pair->first;
for (auto ¤t_ingoer : ingoing_edges_from_component_y_key_value_pair->second) {
if (ingoing_edges_from_component[x][current_ingoing_component].count(current_ingoer) == 1) {
outgoing_edges::Delete(current_ingoing_component, y, 1); // when merging edge_between_components[][x] and [][y], this will be double counted, so we subtract it
Answer -= components_x_size + components_y_size; // Answer is also double counted
} else {
ingoing_edges::Insert(x, current_ingoing_component, current_ingoer);
}
}
if (IsThereAnEdgeBetweenComponent(x, current_ingoing_component)) {
todo.emplace(x, current_ingoing_component);
}
outgoing_edges::Insert(current_ingoing_component, x, edges_between_components[current_ingoing_component][y]);
outgoing_edges::Delete(current_ingoing_component, y);
ingoing_edges::Delete(y, current_ingoing_component);
}
}
{ // handle all merges of outgoing edge of x and y
while (!edges_between_components[y].empty()) {
auto outgoing_edges_y_key_value_pair = begin(edges_between_components[y]);
int nxt_component = outgoing_edges_y_key_value_pair->first;
for (auto ¤t_ingoer : ingoing_edges_from_component[nxt_component][y]) {
ingoing_edges::Insert(nxt_component, x, current_ingoer);
}
if (IsThereAnEdgeBetweenComponent(nxt_component, x)) {
todo.emplace(x, nxt_component); // there is an edge from nxt_component to x and vice versa, so we need to merge them into one component
}
outgoing_edges::Insert(x, nxt_component, edges_between_components[y][nxt_component]);
outgoing_edges::Delete(y, nxt_component);
ingoing_edges::Delete(nxt_component, y);
}
}
}
void AddEdge(int x, int y) {
int real_x = x;
int real_y = y;
x = disjoint_set::Find(x);
y = disjoint_set::Find(y);
if (x == y) return;
if (IsThereAnEdgeBetweenComponent(y, x)) {
todo.emplace(x, y);
while (!todo.empty()) {
int f, s;
tie(f, s) = *begin(todo);
todo.erase(make_pair(f, s));
ConnectComponent(f, s);
}
} else {
if (ingoing_edges_from_component[y][x].count(real_x) == 0) { // if edge doesn't exist already
Answer += disjoint_set::component_size[y];
outgoing_edges::Insert(x, y, 1);
ingoing_edges::Insert(y, x, real_x);
}
}
}
void init() {
disjoint_set::Init();
}
void solve() {
for (int i = 0; i < M; i++) {
AddEdge(A[i], B[i]);
cout << Answer << "\n";
}
}
int main() {
read();
init();
solve();
return 0;
}
Compilation message
joitter2.cpp: In function 'void AddEdge(int, int)':
joitter2.cpp:221:7: warning: unused variable 'real_y' [-Wunused-variable]
int real_y = y;
^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10528 KB |
Output is correct |
3 |
Correct |
10 ms |
10496 KB |
Output is correct |
4 |
Correct |
10 ms |
10496 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
12 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10624 KB |
Output is correct |
9 |
Correct |
11 ms |
10624 KB |
Output is correct |
10 |
Correct |
10 ms |
10496 KB |
Output is correct |
11 |
Correct |
10 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10624 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
11 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
10 ms |
10496 KB |
Output is correct |
17 |
Correct |
10 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10624 KB |
Output is correct |
19 |
Correct |
11 ms |
10496 KB |
Output is correct |
20 |
Correct |
11 ms |
10496 KB |
Output is correct |
21 |
Correct |
11 ms |
10624 KB |
Output is correct |
22 |
Correct |
11 ms |
10624 KB |
Output is correct |
23 |
Correct |
11 ms |
10624 KB |
Output is correct |
24 |
Correct |
10 ms |
10496 KB |
Output is correct |
25 |
Correct |
12 ms |
10624 KB |
Output is correct |
26 |
Correct |
10 ms |
10496 KB |
Output is correct |
27 |
Correct |
10 ms |
10624 KB |
Output is correct |
28 |
Correct |
10 ms |
10496 KB |
Output is correct |
29 |
Correct |
10 ms |
10496 KB |
Output is correct |
30 |
Correct |
10 ms |
10496 KB |
Output is correct |
31 |
Correct |
11 ms |
10496 KB |
Output is correct |
32 |
Correct |
12 ms |
10496 KB |
Output is correct |
33 |
Correct |
11 ms |
10624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10528 KB |
Output is correct |
3 |
Correct |
10 ms |
10496 KB |
Output is correct |
4 |
Correct |
10 ms |
10496 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
12 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10624 KB |
Output is correct |
9 |
Correct |
11 ms |
10624 KB |
Output is correct |
10 |
Correct |
10 ms |
10496 KB |
Output is correct |
11 |
Correct |
10 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10624 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
11 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
10 ms |
10496 KB |
Output is correct |
17 |
Correct |
10 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10624 KB |
Output is correct |
19 |
Correct |
11 ms |
10496 KB |
Output is correct |
20 |
Correct |
11 ms |
10496 KB |
Output is correct |
21 |
Correct |
11 ms |
10624 KB |
Output is correct |
22 |
Correct |
11 ms |
10624 KB |
Output is correct |
23 |
Correct |
11 ms |
10624 KB |
Output is correct |
24 |
Correct |
10 ms |
10496 KB |
Output is correct |
25 |
Correct |
12 ms |
10624 KB |
Output is correct |
26 |
Correct |
10 ms |
10496 KB |
Output is correct |
27 |
Correct |
10 ms |
10624 KB |
Output is correct |
28 |
Correct |
10 ms |
10496 KB |
Output is correct |
29 |
Correct |
10 ms |
10496 KB |
Output is correct |
30 |
Correct |
10 ms |
10496 KB |
Output is correct |
31 |
Correct |
11 ms |
10496 KB |
Output is correct |
32 |
Correct |
12 ms |
10496 KB |
Output is correct |
33 |
Correct |
11 ms |
10624 KB |
Output is correct |
34 |
Correct |
12 ms |
10752 KB |
Output is correct |
35 |
Correct |
106 ms |
16760 KB |
Output is correct |
36 |
Correct |
128 ms |
19576 KB |
Output is correct |
37 |
Correct |
123 ms |
19704 KB |
Output is correct |
38 |
Correct |
127 ms |
19320 KB |
Output is correct |
39 |
Correct |
12 ms |
10624 KB |
Output is correct |
40 |
Correct |
13 ms |
10752 KB |
Output is correct |
41 |
Correct |
14 ms |
10752 KB |
Output is correct |
42 |
Correct |
13 ms |
10624 KB |
Output is correct |
43 |
Correct |
16 ms |
10880 KB |
Output is correct |
44 |
Correct |
13 ms |
10752 KB |
Output is correct |
45 |
Correct |
12 ms |
10624 KB |
Output is correct |
46 |
Correct |
13 ms |
10656 KB |
Output is correct |
47 |
Correct |
17 ms |
10752 KB |
Output is correct |
48 |
Correct |
14 ms |
10752 KB |
Output is correct |
49 |
Correct |
27 ms |
11768 KB |
Output is correct |
50 |
Correct |
128 ms |
19832 KB |
Output is correct |
51 |
Correct |
19 ms |
11136 KB |
Output is correct |
52 |
Correct |
108 ms |
17784 KB |
Output is correct |
53 |
Correct |
22 ms |
11520 KB |
Output is correct |
54 |
Correct |
118 ms |
18808 KB |
Output is correct |
55 |
Correct |
16 ms |
11392 KB |
Output is correct |
56 |
Correct |
17 ms |
11392 KB |
Output is correct |
57 |
Correct |
16 ms |
11392 KB |
Output is correct |
58 |
Correct |
16 ms |
11392 KB |
Output is correct |
59 |
Correct |
12 ms |
10624 KB |
Output is correct |
60 |
Correct |
107 ms |
15356 KB |
Output is correct |
61 |
Correct |
15 ms |
10880 KB |
Output is correct |
62 |
Correct |
120 ms |
19064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10496 KB |
Output is correct |
2 |
Correct |
10 ms |
10528 KB |
Output is correct |
3 |
Correct |
10 ms |
10496 KB |
Output is correct |
4 |
Correct |
10 ms |
10496 KB |
Output is correct |
5 |
Correct |
10 ms |
10496 KB |
Output is correct |
6 |
Correct |
10 ms |
10496 KB |
Output is correct |
7 |
Correct |
12 ms |
10752 KB |
Output is correct |
8 |
Correct |
11 ms |
10624 KB |
Output is correct |
9 |
Correct |
11 ms |
10624 KB |
Output is correct |
10 |
Correct |
10 ms |
10496 KB |
Output is correct |
11 |
Correct |
10 ms |
10496 KB |
Output is correct |
12 |
Correct |
11 ms |
10624 KB |
Output is correct |
13 |
Correct |
11 ms |
10496 KB |
Output is correct |
14 |
Correct |
11 ms |
10496 KB |
Output is correct |
15 |
Correct |
10 ms |
10496 KB |
Output is correct |
16 |
Correct |
10 ms |
10496 KB |
Output is correct |
17 |
Correct |
10 ms |
10496 KB |
Output is correct |
18 |
Correct |
10 ms |
10624 KB |
Output is correct |
19 |
Correct |
11 ms |
10496 KB |
Output is correct |
20 |
Correct |
11 ms |
10496 KB |
Output is correct |
21 |
Correct |
11 ms |
10624 KB |
Output is correct |
22 |
Correct |
11 ms |
10624 KB |
Output is correct |
23 |
Correct |
11 ms |
10624 KB |
Output is correct |
24 |
Correct |
10 ms |
10496 KB |
Output is correct |
25 |
Correct |
12 ms |
10624 KB |
Output is correct |
26 |
Correct |
10 ms |
10496 KB |
Output is correct |
27 |
Correct |
10 ms |
10624 KB |
Output is correct |
28 |
Correct |
10 ms |
10496 KB |
Output is correct |
29 |
Correct |
10 ms |
10496 KB |
Output is correct |
30 |
Correct |
10 ms |
10496 KB |
Output is correct |
31 |
Correct |
11 ms |
10496 KB |
Output is correct |
32 |
Correct |
12 ms |
10496 KB |
Output is correct |
33 |
Correct |
11 ms |
10624 KB |
Output is correct |
34 |
Correct |
12 ms |
10752 KB |
Output is correct |
35 |
Correct |
106 ms |
16760 KB |
Output is correct |
36 |
Correct |
128 ms |
19576 KB |
Output is correct |
37 |
Correct |
123 ms |
19704 KB |
Output is correct |
38 |
Correct |
127 ms |
19320 KB |
Output is correct |
39 |
Correct |
12 ms |
10624 KB |
Output is correct |
40 |
Correct |
13 ms |
10752 KB |
Output is correct |
41 |
Correct |
14 ms |
10752 KB |
Output is correct |
42 |
Correct |
13 ms |
10624 KB |
Output is correct |
43 |
Correct |
16 ms |
10880 KB |
Output is correct |
44 |
Correct |
13 ms |
10752 KB |
Output is correct |
45 |
Correct |
12 ms |
10624 KB |
Output is correct |
46 |
Correct |
13 ms |
10656 KB |
Output is correct |
47 |
Correct |
17 ms |
10752 KB |
Output is correct |
48 |
Correct |
14 ms |
10752 KB |
Output is correct |
49 |
Correct |
27 ms |
11768 KB |
Output is correct |
50 |
Correct |
128 ms |
19832 KB |
Output is correct |
51 |
Correct |
19 ms |
11136 KB |
Output is correct |
52 |
Correct |
108 ms |
17784 KB |
Output is correct |
53 |
Correct |
22 ms |
11520 KB |
Output is correct |
54 |
Correct |
118 ms |
18808 KB |
Output is correct |
55 |
Correct |
16 ms |
11392 KB |
Output is correct |
56 |
Correct |
17 ms |
11392 KB |
Output is correct |
57 |
Correct |
16 ms |
11392 KB |
Output is correct |
58 |
Correct |
16 ms |
11392 KB |
Output is correct |
59 |
Correct |
12 ms |
10624 KB |
Output is correct |
60 |
Correct |
107 ms |
15356 KB |
Output is correct |
61 |
Correct |
15 ms |
10880 KB |
Output is correct |
62 |
Correct |
120 ms |
19064 KB |
Output is correct |
63 |
Correct |
480 ms |
72184 KB |
Output is correct |
64 |
Correct |
459 ms |
72056 KB |
Output is correct |
65 |
Correct |
468 ms |
71976 KB |
Output is correct |
66 |
Correct |
148 ms |
14968 KB |
Output is correct |
67 |
Correct |
266 ms |
23672 KB |
Output is correct |
68 |
Correct |
135 ms |
14968 KB |
Output is correct |
69 |
Correct |
403 ms |
24396 KB |
Output is correct |
70 |
Correct |
156 ms |
14972 KB |
Output is correct |
71 |
Correct |
161 ms |
15060 KB |
Output is correct |
72 |
Correct |
305 ms |
23800 KB |
Output is correct |
73 |
Correct |
346 ms |
23836 KB |
Output is correct |
74 |
Correct |
943 ms |
37112 KB |
Output is correct |
75 |
Correct |
624 ms |
31172 KB |
Output is correct |
76 |
Correct |
659 ms |
37240 KB |
Output is correct |
77 |
Correct |
663 ms |
37368 KB |
Output is correct |
78 |
Correct |
219 ms |
23416 KB |
Output is correct |
79 |
Correct |
361 ms |
26104 KB |
Output is correct |
80 |
Correct |
211 ms |
23416 KB |
Output is correct |
81 |
Correct |
342 ms |
26104 KB |
Output is correct |
82 |
Correct |
690 ms |
51820 KB |
Output is correct |
83 |
Correct |
702 ms |
51704 KB |
Output is correct |
84 |
Correct |
619 ms |
51832 KB |
Output is correct |
85 |
Correct |
610 ms |
51708 KB |
Output is correct |
86 |
Correct |
151 ms |
14200 KB |
Output is correct |
87 |
Correct |
194 ms |
16248 KB |
Output is correct |
88 |
Correct |
314 ms |
23800 KB |
Output is correct |
89 |
Correct |
637 ms |
36856 KB |
Output is correct |