Submission #1060334

# Submission time Handle Problem Language Result Execution time Memory
1060334 2024-08-15T13:01:44 Z nima_aryan Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
347 ms 64848 KB
/**
 *    author:  NimaAryan
 *    created: 2024-08-15 13:30:23
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

i64 ans = 0;
vector<set<int>> S;
vector<set<int>> T;
vector<set<int>> G;
vector<int> par;
queue<pair<int, int>> to_merge;

int find(int x) {
  return par[x] == -1 ? x : par[x] = find(par[x]);
}

inline int size(int A) {
  return S[A].size() + T[A].size() + G[A].size();
}

void unite(int A, int B) {
  A = find(A), B = find(B);

  if (A == B) {
    return;
  }

  if (size(A) < size(B)) {
    swap(A, B);
  }

  ans -= 1LL * S[A].size() * (S[A].size() - 1);
  ans -= 1LL * S[A].size() * T[A].size();
  ans -= 1LL * S[B].size() * (S[B].size() - 1);
  ans -= 1LL * S[B].size() * T[B].size();

  G[A].erase(B);
  for (int X : G[B]) {
    if (X == A) {
      continue;
    }
    if (G[X].count(A)) {
      to_merge.emplace(A, X);
    }
    G[A].insert(X);
  }
  for (int x : T[B]) {
    int X = find(x);
    if (X == A) {
      continue;
    }
    T[A].insert(x);
    if (G[A].count(X)) {
      to_merge.emplace(A, X);
    }
    G[X].erase(B);
    G[X].insert(A);
  }
  for (int b : S[B]) {
    T[A].erase(b);
    S[A].insert(b);
  }
  par[B] = A;

  ans += 1LL * S[A].size() * (S[A].size() - 1);
  ans += 1LL * S[A].size() * T[A].size();
}

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

  int n, m;
  cin >> n >> m;

  S.resize(n);
  for (int x = 0; x < n; ++x) {
    S[x] = {x};
  }
  T.resize(n);
  G.resize(n);
  par.assign(n, -1);

  while (m--) {
    int a, b;
    cin >> a >> b;
    --a, --b;

    int A = find(a), B = find(b);
    if (A != B) {
      if (G[B].count(A)) {
        unite(A, B);
      } else if (!T[B].count(a)) {
        ans += S[B].size();
        T[B].insert(a);
        G[A].insert(B);
      }
      while (!to_merge.empty()) {
        auto [X, Y] = to_merge.front();
        to_merge.pop();
        unite(X, Y);
      }
    }

    cout << ans << "\n";
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 376 KB Output is correct
34 Correct 2 ms 344 KB Output is correct
35 Correct 46 ms 3664 KB Output is correct
36 Correct 56 ms 5648 KB Output is correct
37 Correct 57 ms 5456 KB Output is correct
38 Correct 54 ms 5280 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 3 ms 1372 KB Output is correct
41 Correct 3 ms 1372 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 6 ms 1592 KB Output is correct
50 Correct 59 ms 5712 KB Output is correct
51 Correct 4 ms 1116 KB Output is correct
52 Correct 50 ms 4412 KB Output is correct
53 Correct 5 ms 1372 KB Output is correct
54 Correct 52 ms 4964 KB Output is correct
55 Correct 3 ms 1128 KB Output is correct
56 Correct 3 ms 1116 KB Output is correct
57 Correct 3 ms 1116 KB Output is correct
58 Correct 3 ms 1116 KB Output is correct
59 Correct 2 ms 1372 KB Output is correct
60 Correct 44 ms 3800 KB Output is correct
61 Correct 3 ms 1112 KB Output is correct
62 Correct 54 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 376 KB Output is correct
34 Correct 2 ms 344 KB Output is correct
35 Correct 46 ms 3664 KB Output is correct
36 Correct 56 ms 5648 KB Output is correct
37 Correct 57 ms 5456 KB Output is correct
38 Correct 54 ms 5280 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 3 ms 1372 KB Output is correct
41 Correct 3 ms 1372 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 1 ms 860 KB Output is correct
47 Correct 3 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 6 ms 1592 KB Output is correct
50 Correct 59 ms 5712 KB Output is correct
51 Correct 4 ms 1116 KB Output is correct
52 Correct 50 ms 4412 KB Output is correct
53 Correct 5 ms 1372 KB Output is correct
54 Correct 52 ms 4964 KB Output is correct
55 Correct 3 ms 1128 KB Output is correct
56 Correct 3 ms 1116 KB Output is correct
57 Correct 3 ms 1116 KB Output is correct
58 Correct 3 ms 1116 KB Output is correct
59 Correct 2 ms 1372 KB Output is correct
60 Correct 44 ms 3800 KB Output is correct
61 Correct 3 ms 1112 KB Output is correct
62 Correct 54 ms 5324 KB Output is correct
63 Correct 192 ms 49588 KB Output is correct
64 Correct 195 ms 49704 KB Output is correct
65 Correct 186 ms 49748 KB Output is correct
66 Correct 92 ms 30988 KB Output is correct
67 Correct 179 ms 56660 KB Output is correct
68 Correct 94 ms 31060 KB Output is correct
69 Correct 201 ms 30888 KB Output is correct
70 Correct 93 ms 31060 KB Output is correct
71 Correct 97 ms 31056 KB Output is correct
72 Correct 177 ms 40532 KB Output is correct
73 Correct 173 ms 42324 KB Output is correct
74 Correct 347 ms 55860 KB Output is correct
75 Correct 264 ms 35900 KB Output is correct
76 Correct 278 ms 45136 KB Output is correct
77 Correct 285 ms 45140 KB Output is correct
78 Correct 102 ms 32856 KB Output is correct
79 Correct 196 ms 36944 KB Output is correct
80 Correct 107 ms 32852 KB Output is correct
81 Correct 179 ms 35408 KB Output is correct
82 Correct 299 ms 44680 KB Output is correct
83 Correct 281 ms 44724 KB Output is correct
84 Correct 268 ms 44240 KB Output is correct
85 Correct 249 ms 44264 KB Output is correct
86 Correct 163 ms 63828 KB Output is correct
87 Correct 193 ms 64848 KB Output is correct
88 Correct 176 ms 41580 KB Output is correct
89 Correct 288 ms 44116 KB Output is correct