Submission #963281

# Submission time Handle Problem Language Result Execution time Memory
963281 2024-04-14T20:07:05 Z Soumya1 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
969 ms 81832 KB
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5 + 5;
int sz[mxN], par[mxN];
queue<pair<int, int>> q;
set<int> outc[mxN], inc[mxN], inn[mxN];
set<pair<int, int>> outn[mxN];
long long ans;
int find(int u) {
  return (u == par[u] ? u : find(par[u]));
}
long long contrib(int a) {
  return 1LL * sz[a] * (sz[a] + inn[a].size() - 1);
}
void merge(int a, int b) {
  int aa = find(a), bb = find(b);
  if (aa == bb) return;
  if (inn[bb].find(a) != inn[bb].end()) return;
  if (outc[bb].find(aa) == outc[bb].end()) {
    ans -= contrib(bb);
    inc[bb].insert(aa);
    outc[aa].insert(bb);
    inn[bb].insert(a);
    outn[aa].insert({a, bb});
    ans += contrib(bb);
    return;
  }
  ans -= contrib(aa) + contrib(bb);
  if (sz[aa] > sz[bb]) swap(aa, bb), swap(a, b);
  outc[bb].erase(aa);
  inc[aa].erase(bb);
  for (int x : outc[aa]) {
    inc[x].erase(aa);
  }
  for (int x : inc[aa]) {
    outc[x].erase(aa);
  }
  outc[aa].clear(), inc[aa].clear();
  for (auto x : inn[aa]) {
    q.push({x, bb});
    outn[find(x)].erase({x, aa});
  }
  for (auto [x, y] : outn[aa]) {
    q.push({x, y});
    if (y != bb) ans -= contrib(y);
    inn[y].erase(x);
    if (y != bb) ans += contrib(y);
  }
  inn[aa].clear(), outn[aa].clear();
  par[aa] = bb;
  sz[bb] += sz[aa];
  ans += contrib(bb);
}
void testCase() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    sz[i] = 1, par[i] = i;
  }
  while (m--) {
    int a, b;
    cin >> a >> b;
    q.push({a, b});
    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop();
      merge(x, y);
    }
    cout << ans << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19032 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 7 ms 19036 KB Output is correct
4 Correct 8 ms 19288 KB Output is correct
5 Correct 8 ms 19036 KB Output is correct
6 Correct 8 ms 19260 KB Output is correct
7 Correct 9 ms 19288 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 7 ms 19296 KB Output is correct
10 Correct 7 ms 19100 KB Output is correct
11 Correct 7 ms 19036 KB Output is correct
12 Correct 7 ms 19036 KB Output is correct
13 Correct 7 ms 19036 KB Output is correct
14 Correct 8 ms 19036 KB Output is correct
15 Correct 8 ms 19040 KB Output is correct
16 Correct 7 ms 19036 KB Output is correct
17 Correct 8 ms 19040 KB Output is correct
18 Correct 7 ms 19036 KB Output is correct
19 Correct 7 ms 19048 KB Output is correct
20 Correct 6 ms 19048 KB Output is correct
21 Correct 7 ms 19300 KB Output is correct
22 Correct 6 ms 19040 KB Output is correct
23 Correct 9 ms 19288 KB Output is correct
24 Correct 7 ms 19288 KB Output is correct
25 Correct 6 ms 19292 KB Output is correct
26 Correct 6 ms 19252 KB Output is correct
27 Correct 6 ms 19036 KB Output is correct
28 Correct 6 ms 19036 KB Output is correct
29 Correct 6 ms 19032 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
31 Correct 7 ms 19032 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 8 ms 19292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19032 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 7 ms 19036 KB Output is correct
4 Correct 8 ms 19288 KB Output is correct
5 Correct 8 ms 19036 KB Output is correct
6 Correct 8 ms 19260 KB Output is correct
7 Correct 9 ms 19288 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 7 ms 19296 KB Output is correct
10 Correct 7 ms 19100 KB Output is correct
11 Correct 7 ms 19036 KB Output is correct
12 Correct 7 ms 19036 KB Output is correct
13 Correct 7 ms 19036 KB Output is correct
14 Correct 8 ms 19036 KB Output is correct
15 Correct 8 ms 19040 KB Output is correct
16 Correct 7 ms 19036 KB Output is correct
17 Correct 8 ms 19040 KB Output is correct
18 Correct 7 ms 19036 KB Output is correct
19 Correct 7 ms 19048 KB Output is correct
20 Correct 6 ms 19048 KB Output is correct
21 Correct 7 ms 19300 KB Output is correct
22 Correct 6 ms 19040 KB Output is correct
23 Correct 9 ms 19288 KB Output is correct
24 Correct 7 ms 19288 KB Output is correct
25 Correct 6 ms 19292 KB Output is correct
26 Correct 6 ms 19252 KB Output is correct
27 Correct 6 ms 19036 KB Output is correct
28 Correct 6 ms 19036 KB Output is correct
29 Correct 6 ms 19032 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
31 Correct 7 ms 19032 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 8 ms 19292 KB Output is correct
34 Correct 11 ms 19288 KB Output is correct
35 Correct 63 ms 25336 KB Output is correct
36 Correct 91 ms 28448 KB Output is correct
37 Correct 84 ms 28576 KB Output is correct
38 Correct 81 ms 28188 KB Output is correct
39 Correct 7 ms 19288 KB Output is correct
40 Correct 8 ms 19292 KB Output is correct
41 Correct 9 ms 19292 KB Output is correct
42 Correct 9 ms 19264 KB Output is correct
43 Correct 8 ms 19292 KB Output is correct
44 Correct 11 ms 19520 KB Output is correct
45 Correct 9 ms 19340 KB Output is correct
46 Correct 8 ms 19292 KB Output is correct
47 Correct 11 ms 19288 KB Output is correct
48 Correct 9 ms 19288 KB Output is correct
49 Correct 15 ms 20312 KB Output is correct
50 Correct 93 ms 28628 KB Output is correct
51 Correct 11 ms 19796 KB Output is correct
52 Correct 74 ms 26676 KB Output is correct
53 Correct 15 ms 20060 KB Output is correct
54 Correct 82 ms 27712 KB Output is correct
55 Correct 11 ms 20060 KB Output is correct
56 Correct 10 ms 20060 KB Output is correct
57 Correct 10 ms 20060 KB Output is correct
58 Correct 11 ms 20056 KB Output is correct
59 Correct 8 ms 19292 KB Output is correct
60 Correct 77 ms 24140 KB Output is correct
61 Correct 10 ms 19548 KB Output is correct
62 Correct 82 ms 28104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19032 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 7 ms 19036 KB Output is correct
4 Correct 8 ms 19288 KB Output is correct
5 Correct 8 ms 19036 KB Output is correct
6 Correct 8 ms 19260 KB Output is correct
7 Correct 9 ms 19288 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 7 ms 19296 KB Output is correct
10 Correct 7 ms 19100 KB Output is correct
11 Correct 7 ms 19036 KB Output is correct
12 Correct 7 ms 19036 KB Output is correct
13 Correct 7 ms 19036 KB Output is correct
14 Correct 8 ms 19036 KB Output is correct
15 Correct 8 ms 19040 KB Output is correct
16 Correct 7 ms 19036 KB Output is correct
17 Correct 8 ms 19040 KB Output is correct
18 Correct 7 ms 19036 KB Output is correct
19 Correct 7 ms 19048 KB Output is correct
20 Correct 6 ms 19048 KB Output is correct
21 Correct 7 ms 19300 KB Output is correct
22 Correct 6 ms 19040 KB Output is correct
23 Correct 9 ms 19288 KB Output is correct
24 Correct 7 ms 19288 KB Output is correct
25 Correct 6 ms 19292 KB Output is correct
26 Correct 6 ms 19252 KB Output is correct
27 Correct 6 ms 19036 KB Output is correct
28 Correct 6 ms 19036 KB Output is correct
29 Correct 6 ms 19032 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
31 Correct 7 ms 19032 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 8 ms 19292 KB Output is correct
34 Correct 11 ms 19288 KB Output is correct
35 Correct 63 ms 25336 KB Output is correct
36 Correct 91 ms 28448 KB Output is correct
37 Correct 84 ms 28576 KB Output is correct
38 Correct 81 ms 28188 KB Output is correct
39 Correct 7 ms 19288 KB Output is correct
40 Correct 8 ms 19292 KB Output is correct
41 Correct 9 ms 19292 KB Output is correct
42 Correct 9 ms 19264 KB Output is correct
43 Correct 8 ms 19292 KB Output is correct
44 Correct 11 ms 19520 KB Output is correct
45 Correct 9 ms 19340 KB Output is correct
46 Correct 8 ms 19292 KB Output is correct
47 Correct 11 ms 19288 KB Output is correct
48 Correct 9 ms 19288 KB Output is correct
49 Correct 15 ms 20312 KB Output is correct
50 Correct 93 ms 28628 KB Output is correct
51 Correct 11 ms 19796 KB Output is correct
52 Correct 74 ms 26676 KB Output is correct
53 Correct 15 ms 20060 KB Output is correct
54 Correct 82 ms 27712 KB Output is correct
55 Correct 11 ms 20060 KB Output is correct
56 Correct 10 ms 20060 KB Output is correct
57 Correct 10 ms 20060 KB Output is correct
58 Correct 11 ms 20056 KB Output is correct
59 Correct 8 ms 19292 KB Output is correct
60 Correct 77 ms 24140 KB Output is correct
61 Correct 10 ms 19548 KB Output is correct
62 Correct 82 ms 28104 KB Output is correct
63 Correct 497 ms 81832 KB Output is correct
64 Correct 475 ms 81788 KB Output is correct
65 Correct 513 ms 81716 KB Output is correct
66 Correct 99 ms 24480 KB Output is correct
67 Correct 203 ms 32924 KB Output is correct
68 Correct 86 ms 24444 KB Output is correct
69 Correct 267 ms 33632 KB Output is correct
70 Correct 89 ms 24340 KB Output is correct
71 Correct 93 ms 24204 KB Output is correct
72 Correct 275 ms 32984 KB Output is correct
73 Correct 238 ms 33104 KB Output is correct
74 Correct 969 ms 47160 KB Output is correct
75 Correct 406 ms 40816 KB Output is correct
76 Correct 632 ms 46776 KB Output is correct
77 Correct 668 ms 46816 KB Output is correct
78 Correct 186 ms 32924 KB Output is correct
79 Correct 309 ms 36248 KB Output is correct
80 Correct 189 ms 32816 KB Output is correct
81 Correct 264 ms 36176 KB Output is correct
82 Correct 632 ms 61008 KB Output is correct
83 Correct 724 ms 61180 KB Output is correct
84 Correct 509 ms 61112 KB Output is correct
85 Correct 545 ms 61152 KB Output is correct
86 Correct 105 ms 23392 KB Output is correct
87 Correct 117 ms 25836 KB Output is correct
88 Correct 305 ms 33216 KB Output is correct
89 Correct 571 ms 46500 KB Output is correct