Submission #1036462

# Submission time Handle Problem Language Result Execution time Memory
1036462 2024-07-27T11:52:25 Z juicy Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
463 ms 57940 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5; 

int n, m;
int lab[N];
set<int> s[N];
set<array<int, 2>> t[N];
vector<array<int, 2>> st;

int size(int u) {
  return -lab[u];
}

int get(int u) {
  return lab[u] < 0 ? u : lab[u] = get(lab[u]);
}

bool check(set<array<int, 2>> &st, int x) {
  auto it = st.lower_bound(array<int, 2>{x});
  return it != st.end() && (*it)[0] == x;
} 

int count(set<array<int, 2>> &st, int x) {
  auto it = st.lower_bound(array<int, 2>{x});
  int cnt = 0;
  for (; it != st.end() && (*it)[0] == x; it = st.erase(it)) {
    ++cnt;
  }
  return cnt;
}

long long res;

void mrg(int u, int v) {
  u = get(u), v = get(v);
  if (u == v) {
    return;
  }
  int cnt = count(t[u], v);
  res -= (long long) cnt * size(u);
  cnt = count(t[v], u);
  res -= (long long) cnt * size(v);
  res += (long long) 2 * size(u) * size(v);
  if (s[u].size() + t[u].size() < s[v].size() + t[v].size()) {
    swap(u, v);
  }
  for (int x : s[v]) {
    if (check(t[u], x)) {
      st.push_back({x, u});
    }
  }
  for (auto x : t[v]) {
    if (s[u].find(x[0]) != s[u].end()) {
      st.push_back({x[0], u});
    }
  }
  for (int x : s[v]) {
    s[u].insert(x);
    auto it = t[x].lower_bound(array<int, 2>{v});
    for (; it != t[x].end() && (*it)[0] == v; it = t[x].erase(it)) {
      t[x].insert({u, (*it)[1]});
    }
  }
  cnt = t[u].size();
  for (auto [x, y] : t[v]) {
    if (t[u].insert({x, y}).second) {
      res += size(u);
    } else {
      --cnt;
    }
    s[x].erase(v);
    s[x].insert(u);
  }
  res += (long long) cnt * size(v);
  lab[u] += lab[v];
  lab[v] = u;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> m;
  fill(lab + 1, lab + n + 1, -1);
  for (int i = 1; i <= m; ++i) {
    int a, b; cin >> a >> b;
    int u = get(a), v = get(b);
    if (u != v) {
      s[u].insert(v);
      res += t[v].insert({u, a}).second * size(v);
      if (s[v].find(u) != s[v].end()) {
        st.push_back({u, v});
      }
      while (st.size()) {
        auto [x, y] = st.back(); st.pop_back();
        mrg(x, y);
      }
    }
    cout << res << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9856 KB Output is correct
6 Correct 1 ms 9820 KB Output is correct
7 Correct 2 ms 9720 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 1 ms 9820 KB Output is correct
11 Correct 1 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 1 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 1 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9816 KB Output is correct
19 Correct 3 ms 9824 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9816 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9820 KB Output is correct
32 Correct 2 ms 9820 KB Output is correct
33 Correct 2 ms 9816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9856 KB Output is correct
6 Correct 1 ms 9820 KB Output is correct
7 Correct 2 ms 9720 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 1 ms 9820 KB Output is correct
11 Correct 1 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 1 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 1 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9816 KB Output is correct
19 Correct 3 ms 9824 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9816 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9820 KB Output is correct
32 Correct 2 ms 9820 KB Output is correct
33 Correct 2 ms 9816 KB Output is correct
34 Correct 3 ms 9816 KB Output is correct
35 Correct 45 ms 12880 KB Output is correct
36 Correct 57 ms 14800 KB Output is correct
37 Correct 62 ms 14820 KB Output is correct
38 Correct 59 ms 14420 KB Output is correct
39 Correct 3 ms 10076 KB Output is correct
40 Correct 4 ms 10332 KB Output is correct
41 Correct 4 ms 10332 KB Output is correct
42 Correct 3 ms 10092 KB Output is correct
43 Correct 3 ms 10076 KB Output is correct
44 Correct 3 ms 10076 KB Output is correct
45 Correct 3 ms 10076 KB Output is correct
46 Correct 3 ms 10076 KB Output is correct
47 Correct 4 ms 10076 KB Output is correct
48 Correct 4 ms 10232 KB Output is correct
49 Correct 8 ms 10840 KB Output is correct
50 Correct 72 ms 14836 KB Output is correct
51 Correct 6 ms 10072 KB Output is correct
52 Correct 55 ms 13492 KB Output is correct
53 Correct 8 ms 10584 KB Output is correct
54 Correct 56 ms 14164 KB Output is correct
55 Correct 5 ms 10072 KB Output is correct
56 Correct 5 ms 10076 KB Output is correct
57 Correct 5 ms 10072 KB Output is correct
58 Correct 5 ms 10076 KB Output is correct
59 Correct 4 ms 10332 KB Output is correct
60 Correct 48 ms 12848 KB Output is correct
61 Correct 5 ms 10072 KB Output is correct
62 Correct 59 ms 14444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9856 KB Output is correct
6 Correct 1 ms 9820 KB Output is correct
7 Correct 2 ms 9720 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9820 KB Output is correct
10 Correct 1 ms 9820 KB Output is correct
11 Correct 1 ms 9820 KB Output is correct
12 Correct 2 ms 9820 KB Output is correct
13 Correct 1 ms 9820 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9820 KB Output is correct
16 Correct 1 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9816 KB Output is correct
19 Correct 3 ms 9824 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 2 ms 9816 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 2 ms 9820 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9820 KB Output is correct
32 Correct 2 ms 9820 KB Output is correct
33 Correct 2 ms 9816 KB Output is correct
34 Correct 3 ms 9816 KB Output is correct
35 Correct 45 ms 12880 KB Output is correct
36 Correct 57 ms 14800 KB Output is correct
37 Correct 62 ms 14820 KB Output is correct
38 Correct 59 ms 14420 KB Output is correct
39 Correct 3 ms 10076 KB Output is correct
40 Correct 4 ms 10332 KB Output is correct
41 Correct 4 ms 10332 KB Output is correct
42 Correct 3 ms 10092 KB Output is correct
43 Correct 3 ms 10076 KB Output is correct
44 Correct 3 ms 10076 KB Output is correct
45 Correct 3 ms 10076 KB Output is correct
46 Correct 3 ms 10076 KB Output is correct
47 Correct 4 ms 10076 KB Output is correct
48 Correct 4 ms 10232 KB Output is correct
49 Correct 8 ms 10840 KB Output is correct
50 Correct 72 ms 14836 KB Output is correct
51 Correct 6 ms 10072 KB Output is correct
52 Correct 55 ms 13492 KB Output is correct
53 Correct 8 ms 10584 KB Output is correct
54 Correct 56 ms 14164 KB Output is correct
55 Correct 5 ms 10072 KB Output is correct
56 Correct 5 ms 10076 KB Output is correct
57 Correct 5 ms 10072 KB Output is correct
58 Correct 5 ms 10076 KB Output is correct
59 Correct 4 ms 10332 KB Output is correct
60 Correct 48 ms 12848 KB Output is correct
61 Correct 5 ms 10072 KB Output is correct
62 Correct 59 ms 14444 KB Output is correct
63 Correct 201 ms 40276 KB Output is correct
64 Correct 181 ms 40240 KB Output is correct
65 Correct 179 ms 40276 KB Output is correct
66 Correct 85 ms 21584 KB Output is correct
67 Correct 219 ms 48720 KB Output is correct
68 Correct 87 ms 21588 KB Output is correct
69 Correct 159 ms 21520 KB Output is correct
70 Correct 104 ms 21672 KB Output is correct
71 Correct 92 ms 21696 KB Output is correct
72 Correct 202 ms 32420 KB Output is correct
73 Correct 217 ms 34128 KB Output is correct
74 Correct 463 ms 47564 KB Output is correct
75 Correct 246 ms 25168 KB Output is correct
76 Correct 292 ms 34640 KB Output is correct
77 Correct 353 ms 34736 KB Output is correct
78 Correct 92 ms 22100 KB Output is correct
79 Correct 134 ms 25424 KB Output is correct
80 Correct 116 ms 22100 KB Output is correct
81 Correct 174 ms 24500 KB Output is correct
82 Correct 295 ms 32428 KB Output is correct
83 Correct 250 ms 32340 KB Output is correct
84 Correct 234 ms 31828 KB Output is correct
85 Correct 269 ms 31828 KB Output is correct
86 Correct 164 ms 56656 KB Output is correct
87 Correct 176 ms 57940 KB Output is correct
88 Correct 231 ms 33364 KB Output is correct
89 Correct 314 ms 33876 KB Output is correct