답안 #217164

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
217164 2020-03-29T07:11:24 Z rama_pang 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
971 ms 73240 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;

long long Answer = 0;

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; // all vertices that can reach x can now reach y as well
    Answer += 1ll * total_size_ingoing_edges_from_component[y] * components_x_size; // all vertices that can reach y can now reach x as well

    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 &current_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 &current_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()) {
      pair<int, int> cur = *begin(todo);
      todo.erase(cur);
      ConnectComponent(cur.first, cur.second);
    }
  } else if (ingoing_edges_from_component[y][x].count(real_x) == 0) { // if edge (real_x, real_y) doesn't already exist
    outgoing_edges::Insert(x, y, 1);
    ingoing_edges::Insert(y, x, real_x);
    Answer += disjoint_set::component_size[y]; // there forms an edge from real_x to all nodes in component y
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  disjoint_set::Init();

  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int A, B; 
    cin >> A >> B;
    AddEdge(--A, --B);

    cout << Answer << '\n';
  }

  return 0;
}

Compilation message

joitter2.cpp: In function 'void AddEdge(int, int)':
joitter2.cpp:176:7: warning: unused variable 'real_y' [-Wunused-variable]
   int real_y = y;
       ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10496 KB Output is correct
2 Correct 10 ms 10496 KB Output is correct
3 Correct 10 ms 10496 KB Output is correct
4 Correct 10 ms 10496 KB Output is correct
5 Correct 11 ms 10496 KB Output is correct
6 Correct 10 ms 10496 KB Output is correct
7 Correct 11 ms 10624 KB Output is correct
8 Correct 11 ms 10624 KB Output is correct
9 Correct 10 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 10496 KB Output is correct
13 Correct 11 ms 10496 KB Output is correct
14 Correct 11 ms 10496 KB Output is correct
15 Correct 11 ms 10496 KB Output is correct
16 Correct 11 ms 10496 KB Output is correct
17 Correct 11 ms 10496 KB Output is correct
18 Correct 12 ms 10496 KB Output is correct
19 Correct 11 ms 10496 KB Output is correct
20 Correct 11 ms 10624 KB Output is correct
21 Correct 11 ms 10624 KB Output is correct
22 Correct 11 ms 10496 KB Output is correct
23 Correct 13 ms 10624 KB Output is correct
24 Correct 12 ms 10624 KB Output is correct
25 Correct 10 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 10624 KB Output is correct
30 Correct 10 ms 10496 KB Output is correct
31 Correct 11 ms 10496 KB Output is correct
32 Correct 10 ms 10496 KB Output is correct
33 Correct 10 ms 10624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10496 KB Output is correct
2 Correct 10 ms 10496 KB Output is correct
3 Correct 10 ms 10496 KB Output is correct
4 Correct 10 ms 10496 KB Output is correct
5 Correct 11 ms 10496 KB Output is correct
6 Correct 10 ms 10496 KB Output is correct
7 Correct 11 ms 10624 KB Output is correct
8 Correct 11 ms 10624 KB Output is correct
9 Correct 10 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 10496 KB Output is correct
13 Correct 11 ms 10496 KB Output is correct
14 Correct 11 ms 10496 KB Output is correct
15 Correct 11 ms 10496 KB Output is correct
16 Correct 11 ms 10496 KB Output is correct
17 Correct 11 ms 10496 KB Output is correct
18 Correct 12 ms 10496 KB Output is correct
19 Correct 11 ms 10496 KB Output is correct
20 Correct 11 ms 10624 KB Output is correct
21 Correct 11 ms 10624 KB Output is correct
22 Correct 11 ms 10496 KB Output is correct
23 Correct 13 ms 10624 KB Output is correct
24 Correct 12 ms 10624 KB Output is correct
25 Correct 10 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 10624 KB Output is correct
30 Correct 10 ms 10496 KB Output is correct
31 Correct 11 ms 10496 KB Output is correct
32 Correct 10 ms 10496 KB Output is correct
33 Correct 10 ms 10624 KB Output is correct
34 Correct 13 ms 10752 KB Output is correct
35 Correct 110 ms 16632 KB Output is correct
36 Correct 132 ms 19960 KB Output is correct
37 Correct 136 ms 19992 KB Output is correct
38 Correct 130 ms 19576 KB Output is correct
39 Correct 12 ms 10624 KB Output is correct
40 Correct 13 ms 10880 KB Output is correct
41 Correct 13 ms 10752 KB Output is correct
42 Correct 12 ms 10624 KB Output is correct
43 Correct 13 ms 10752 KB Output is correct
44 Correct 13 ms 10752 KB Output is correct
45 Correct 12 ms 10624 KB Output is correct
46 Correct 16 ms 10624 KB Output is correct
47 Correct 13 ms 10752 KB Output is correct
48 Correct 14 ms 10880 KB Output is correct
49 Correct 24 ms 11648 KB Output is correct
50 Correct 141 ms 20088 KB Output is correct
51 Correct 17 ms 11136 KB Output is correct
52 Correct 110 ms 18040 KB Output is correct
53 Correct 22 ms 11648 KB Output is correct
54 Correct 145 ms 19068 KB Output is correct
55 Correct 22 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 13 ms 10624 KB Output is correct
60 Correct 115 ms 15712 KB Output is correct
61 Correct 25 ms 10880 KB Output is correct
62 Correct 127 ms 19320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 10496 KB Output is correct
2 Correct 10 ms 10496 KB Output is correct
3 Correct 10 ms 10496 KB Output is correct
4 Correct 10 ms 10496 KB Output is correct
5 Correct 11 ms 10496 KB Output is correct
6 Correct 10 ms 10496 KB Output is correct
7 Correct 11 ms 10624 KB Output is correct
8 Correct 11 ms 10624 KB Output is correct
9 Correct 10 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 10496 KB Output is correct
13 Correct 11 ms 10496 KB Output is correct
14 Correct 11 ms 10496 KB Output is correct
15 Correct 11 ms 10496 KB Output is correct
16 Correct 11 ms 10496 KB Output is correct
17 Correct 11 ms 10496 KB Output is correct
18 Correct 12 ms 10496 KB Output is correct
19 Correct 11 ms 10496 KB Output is correct
20 Correct 11 ms 10624 KB Output is correct
21 Correct 11 ms 10624 KB Output is correct
22 Correct 11 ms 10496 KB Output is correct
23 Correct 13 ms 10624 KB Output is correct
24 Correct 12 ms 10624 KB Output is correct
25 Correct 10 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 10624 KB Output is correct
30 Correct 10 ms 10496 KB Output is correct
31 Correct 11 ms 10496 KB Output is correct
32 Correct 10 ms 10496 KB Output is correct
33 Correct 10 ms 10624 KB Output is correct
34 Correct 13 ms 10752 KB Output is correct
35 Correct 110 ms 16632 KB Output is correct
36 Correct 132 ms 19960 KB Output is correct
37 Correct 136 ms 19992 KB Output is correct
38 Correct 130 ms 19576 KB Output is correct
39 Correct 12 ms 10624 KB Output is correct
40 Correct 13 ms 10880 KB Output is correct
41 Correct 13 ms 10752 KB Output is correct
42 Correct 12 ms 10624 KB Output is correct
43 Correct 13 ms 10752 KB Output is correct
44 Correct 13 ms 10752 KB Output is correct
45 Correct 12 ms 10624 KB Output is correct
46 Correct 16 ms 10624 KB Output is correct
47 Correct 13 ms 10752 KB Output is correct
48 Correct 14 ms 10880 KB Output is correct
49 Correct 24 ms 11648 KB Output is correct
50 Correct 141 ms 20088 KB Output is correct
51 Correct 17 ms 11136 KB Output is correct
52 Correct 110 ms 18040 KB Output is correct
53 Correct 22 ms 11648 KB Output is correct
54 Correct 145 ms 19068 KB Output is correct
55 Correct 22 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 13 ms 10624 KB Output is correct
60 Correct 115 ms 15712 KB Output is correct
61 Correct 25 ms 10880 KB Output is correct
62 Correct 127 ms 19320 KB Output is correct
63 Correct 509 ms 73240 KB Output is correct
64 Correct 512 ms 73208 KB Output is correct
65 Correct 495 ms 73080 KB Output is correct
66 Correct 178 ms 15864 KB Output is correct
67 Correct 281 ms 24312 KB Output is correct
68 Correct 154 ms 15740 KB Output is correct
69 Correct 444 ms 25080 KB Output is correct
70 Correct 171 ms 15736 KB Output is correct
71 Correct 169 ms 15736 KB Output is correct
72 Correct 338 ms 24440 KB Output is correct
73 Correct 346 ms 24440 KB Output is correct
74 Correct 971 ms 38136 KB Output is correct
75 Correct 651 ms 32188 KB Output is correct
76 Correct 736 ms 38104 KB Output is correct
77 Correct 699 ms 38212 KB Output is correct
78 Correct 234 ms 23884 KB Output is correct
79 Correct 401 ms 27364 KB Output is correct
80 Correct 254 ms 23928 KB Output is correct
81 Correct 379 ms 27192 KB Output is correct
82 Correct 755 ms 52472 KB Output is correct
83 Correct 780 ms 52472 KB Output is correct
84 Correct 682 ms 52472 KB Output is correct
85 Correct 635 ms 52472 KB Output is correct
86 Correct 204 ms 14968 KB Output is correct
87 Correct 238 ms 17144 KB Output is correct
88 Correct 442 ms 24696 KB Output is correct
89 Correct 799 ms 37776 KB Output is correct