Submission #212145

# Submission time Handle Problem Language Result Execution time Memory
212145 2020-03-22T11:12:55 Z zimpha Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
642 ms 40772 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

using int64 = long long;

const int N = 1e5 + 10;

std::set<int> in[N];
std::set<std::pair<int, int>> out[N];
int dsu[N], size[N];
int64 ret;

int get(int x) {
  if (x != dsu[x]) dsu[x] = get(dsu[x]);
  return dsu[x];
}

void add(int u, int v) {
  int ru = get(u), rv = get(v);
  if (ru == rv) return;
  auto it = out[rv].lower_bound({ru, 0});
  if (it != out[rv].end() && it->first == ru) { // merge
    ret += (int64)size[ru] * size[rv] * 2;
    if (in[ru].size() + out[ru].size() > in[rv].size() + out[rv].size()) {
      std::swap(ru, rv);
    }
    std::vector<std::pair<int, int>> new_out;
    for (auto &e: out[ru]) {
      in[e.first].erase(e.second);
      ret -= size[e.first];
      if (e.first != rv) new_out.push_back(e);
    }
    out[ru].clear();
    ret -= (int64)size[ru] * in[ru].size();
    ret += (int64)size[ru] * in[rv].size();
    std::vector<int> new_in;
    for (auto &x: in[ru]) {
      int rx = get(x);
      out[rx].erase({ru, x});
      if (rx != rv) new_in.push_back(x);
    }
    in[ru].clear();
    dsu[ru] = rv; size[rv] += size[ru];
    for (auto &x: new_in) add(x, rv);
    for (auto &e: new_out) add(e.second, e.first);
  } else if (!in[rv].count(u)) {
    ret += size[rv];
    out[ru].emplace(rv, u);
    in[rv].insert(u);
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) dsu[i] = i, size[i] = 1;
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u - 1, v - 1);
    printf("%lld\n", ret);
  }
  return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &u, &v);
     ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 13 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 12 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9728 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 11 ms 9728 KB Output is correct
29 Correct 12 ms 9728 KB Output is correct
30 Correct 9 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 13 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 12 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9728 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 11 ms 9728 KB Output is correct
29 Correct 12 ms 9728 KB Output is correct
30 Correct 9 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 10 ms 9728 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 133 ms 12920 KB Output is correct
36 Correct 137 ms 14584 KB Output is correct
37 Correct 141 ms 14584 KB Output is correct
38 Correct 150 ms 14488 KB Output is correct
39 Correct 11 ms 9728 KB Output is correct
40 Correct 13 ms 9856 KB Output is correct
41 Correct 12 ms 9856 KB Output is correct
42 Correct 12 ms 9728 KB Output is correct
43 Correct 12 ms 9856 KB Output is correct
44 Correct 14 ms 9856 KB Output is correct
45 Correct 12 ms 9728 KB Output is correct
46 Correct 12 ms 9728 KB Output is correct
47 Correct 12 ms 9856 KB Output is correct
48 Correct 12 ms 9856 KB Output is correct
49 Correct 18 ms 10240 KB Output is correct
50 Correct 143 ms 14712 KB Output is correct
51 Correct 18 ms 9984 KB Output is correct
52 Correct 132 ms 13560 KB Output is correct
53 Correct 18 ms 10240 KB Output is correct
54 Correct 135 ms 14200 KB Output is correct
55 Correct 14 ms 10112 KB Output is correct
56 Correct 15 ms 10112 KB Output is correct
57 Correct 14 ms 10112 KB Output is correct
58 Correct 14 ms 10112 KB Output is correct
59 Correct 12 ms 9728 KB Output is correct
60 Correct 122 ms 12152 KB Output is correct
61 Correct 13 ms 9856 KB Output is correct
62 Correct 151 ms 14456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 9 ms 9728 KB Output is correct
3 Correct 9 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 9 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 11 ms 9728 KB Output is correct
9 Correct 13 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 12 ms 9728 KB Output is correct
14 Correct 10 ms 9728 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 9 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 11 ms 9728 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 10 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 10 ms 9728 KB Output is correct
26 Correct 11 ms 9728 KB Output is correct
27 Correct 10 ms 9728 KB Output is correct
28 Correct 11 ms 9728 KB Output is correct
29 Correct 12 ms 9728 KB Output is correct
30 Correct 9 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 10 ms 9728 KB Output is correct
33 Correct 10 ms 9728 KB Output is correct
34 Correct 13 ms 9856 KB Output is correct
35 Correct 133 ms 12920 KB Output is correct
36 Correct 137 ms 14584 KB Output is correct
37 Correct 141 ms 14584 KB Output is correct
38 Correct 150 ms 14488 KB Output is correct
39 Correct 11 ms 9728 KB Output is correct
40 Correct 13 ms 9856 KB Output is correct
41 Correct 12 ms 9856 KB Output is correct
42 Correct 12 ms 9728 KB Output is correct
43 Correct 12 ms 9856 KB Output is correct
44 Correct 14 ms 9856 KB Output is correct
45 Correct 12 ms 9728 KB Output is correct
46 Correct 12 ms 9728 KB Output is correct
47 Correct 12 ms 9856 KB Output is correct
48 Correct 12 ms 9856 KB Output is correct
49 Correct 18 ms 10240 KB Output is correct
50 Correct 143 ms 14712 KB Output is correct
51 Correct 18 ms 9984 KB Output is correct
52 Correct 132 ms 13560 KB Output is correct
53 Correct 18 ms 10240 KB Output is correct
54 Correct 135 ms 14200 KB Output is correct
55 Correct 14 ms 10112 KB Output is correct
56 Correct 15 ms 10112 KB Output is correct
57 Correct 14 ms 10112 KB Output is correct
58 Correct 14 ms 10112 KB Output is correct
59 Correct 12 ms 9728 KB Output is correct
60 Correct 122 ms 12152 KB Output is correct
61 Correct 13 ms 9856 KB Output is correct
62 Correct 151 ms 14456 KB Output is correct
63 Correct 436 ms 40672 KB Output is correct
64 Correct 437 ms 40772 KB Output is correct
65 Correct 422 ms 40696 KB Output is correct
66 Correct 117 ms 12536 KB Output is correct
67 Correct 192 ms 16504 KB Output is correct
68 Correct 116 ms 12536 KB Output is correct
69 Correct 227 ms 17272 KB Output is correct
70 Correct 118 ms 12536 KB Output is correct
71 Correct 132 ms 12620 KB Output is correct
72 Correct 212 ms 16632 KB Output is correct
73 Correct 215 ms 16632 KB Output is correct
74 Correct 642 ms 24644 KB Output is correct
75 Correct 412 ms 20856 KB Output is correct
76 Correct 500 ms 23672 KB Output is correct
77 Correct 493 ms 23672 KB Output is correct
78 Correct 188 ms 16760 KB Output is correct
79 Correct 283 ms 18552 KB Output is correct
80 Correct 156 ms 16632 KB Output is correct
81 Correct 261 ms 18168 KB Output is correct
82 Correct 450 ms 30584 KB Output is correct
83 Correct 449 ms 30584 KB Output is correct
84 Correct 434 ms 30968 KB Output is correct
85 Correct 457 ms 30816 KB Output is correct
86 Correct 129 ms 11896 KB Output is correct
87 Correct 168 ms 12920 KB Output is correct
88 Correct 250 ms 16760 KB Output is correct
89 Correct 544 ms 23160 KB Output is correct