Submission #1084031

# Submission time Handle Problem Language Result Execution time Memory
1084031 2024-09-04T21:43:39 Z stefanneagu Making Friends on Joitter is Fun (JOI20_joitter2) C++17
17 / 100
729 ms 67668 KB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e5 + 1;

int root[nmax], sz[nmax], ans;

set<int> adj[nmax], inv[nmax], child[nmax];

int find(int a) {
  if(root[a] == a) {
    return a;
  }
  return root[a] = find(root[a]);
}

queue<pair<int, int>> q;

void weak(int a, int b) {
  adj[a].insert(b);
  inv[b].insert(a);
  if(adj[b].count(a)) {
    q.push({a, b});
  }
}

int ds_sz(int x) {
  return child[x].size() + adj[x].size() + inv[x].size();
}

void unite(int a, int b) {
  a = find(a), b = find(b);
  if(a == b) {
    return;
  }
  if(ds_sz(a) < ds_sz(b)) {
    swap(a, b);
  }
  ans += sz[b] * child[a].size() + sz[a] * child[b].size();
  root[b] = a;
  sz[a] += sz[b];
  for(auto it : child[b]) {
    if(child[a].count(it)) {
      ans -= sz[a];
    } else {
      child[a].insert(it);
    }
  }
  adj[a].erase(b), adj[b].erase(a);
  inv[a].erase(b), inv[b].erase(a);
  for(auto it : adj[b]) {
    inv[it].erase(b);
    weak(a, it);
  }
  for(auto it : inv[b]) {
    adj[it].erase(b);
    weak(it, a);
  }
}

int main() {
  cin.tie()->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for(int i = 1; i <= n; i++) {
    sz[i] = 1, root[i] = i;
    child[i].insert(i);
  }
  for(int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    b = find(b);
    if(find(a) != b && child[b].count(a) == 0) {
      ans += sz[b];
      child[b].insert(a);
      a = find(a);
      weak(a, b);
      while(!q.empty()) {
        pair<int, int> p = q.front();
        q.pop();
        unite(p.first, p.second);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 4 ms 14828 KB Output is correct
5 Correct 5 ms 14428 KB Output is correct
6 Correct 5 ms 14428 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14680 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14428 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 7 ms 14428 KB Output is correct
13 Correct 6 ms 14428 KB Output is correct
14 Correct 6 ms 14376 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 7 ms 14680 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 5 ms 14428 KB Output is correct
19 Correct 5 ms 14368 KB Output is correct
20 Correct 5 ms 14428 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14428 KB Output is correct
23 Correct 6 ms 14428 KB Output is correct
24 Correct 6 ms 14428 KB Output is correct
25 Correct 7 ms 14428 KB Output is correct
26 Correct 5 ms 14428 KB Output is correct
27 Correct 6 ms 14428 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 6 ms 14428 KB Output is correct
30 Correct 7 ms 14428 KB Output is correct
31 Correct 12 ms 14428 KB Output is correct
32 Correct 6 ms 14428 KB Output is correct
33 Correct 7 ms 14604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 4 ms 14828 KB Output is correct
5 Correct 5 ms 14428 KB Output is correct
6 Correct 5 ms 14428 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14680 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14428 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 7 ms 14428 KB Output is correct
13 Correct 6 ms 14428 KB Output is correct
14 Correct 6 ms 14376 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 7 ms 14680 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 5 ms 14428 KB Output is correct
19 Correct 5 ms 14368 KB Output is correct
20 Correct 5 ms 14428 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14428 KB Output is correct
23 Correct 6 ms 14428 KB Output is correct
24 Correct 6 ms 14428 KB Output is correct
25 Correct 7 ms 14428 KB Output is correct
26 Correct 5 ms 14428 KB Output is correct
27 Correct 6 ms 14428 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 6 ms 14428 KB Output is correct
30 Correct 7 ms 14428 KB Output is correct
31 Correct 12 ms 14428 KB Output is correct
32 Correct 6 ms 14428 KB Output is correct
33 Correct 7 ms 14604 KB Output is correct
34 Correct 16 ms 14936 KB Output is correct
35 Correct 380 ms 20304 KB Output is correct
36 Correct 439 ms 23024 KB Output is correct
37 Correct 415 ms 23124 KB Output is correct
38 Correct 406 ms 22864 KB Output is correct
39 Correct 12 ms 15196 KB Output is correct
40 Correct 13 ms 15284 KB Output is correct
41 Correct 12 ms 15196 KB Output is correct
42 Correct 12 ms 14680 KB Output is correct
43 Correct 11 ms 15196 KB Output is correct
44 Correct 12 ms 14940 KB Output is correct
45 Correct 11 ms 14684 KB Output is correct
46 Correct 12 ms 14684 KB Output is correct
47 Correct 13 ms 14940 KB Output is correct
48 Correct 12 ms 14940 KB Output is correct
49 Correct 24 ms 16216 KB Output is correct
50 Correct 398 ms 23248 KB Output is correct
51 Correct 20 ms 15196 KB Output is correct
52 Correct 370 ms 21588 KB Output is correct
53 Correct 23 ms 15960 KB Output is correct
54 Correct 434 ms 22256 KB Output is correct
55 Correct 15 ms 15704 KB Output is correct
56 Correct 14 ms 15196 KB Output is correct
57 Correct 13 ms 15196 KB Output is correct
58 Correct 13 ms 15240 KB Output is correct
59 Correct 13 ms 15196 KB Output is correct
60 Correct 373 ms 20252 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 409 ms 22532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 6 ms 14428 KB Output is correct
4 Correct 4 ms 14828 KB Output is correct
5 Correct 5 ms 14428 KB Output is correct
6 Correct 5 ms 14428 KB Output is correct
7 Correct 8 ms 14428 KB Output is correct
8 Correct 8 ms 14680 KB Output is correct
9 Correct 7 ms 14424 KB Output is correct
10 Correct 7 ms 14428 KB Output is correct
11 Correct 5 ms 14428 KB Output is correct
12 Correct 7 ms 14428 KB Output is correct
13 Correct 6 ms 14428 KB Output is correct
14 Correct 6 ms 14376 KB Output is correct
15 Correct 6 ms 14428 KB Output is correct
16 Correct 7 ms 14680 KB Output is correct
17 Correct 4 ms 14940 KB Output is correct
18 Correct 5 ms 14428 KB Output is correct
19 Correct 5 ms 14368 KB Output is correct
20 Correct 5 ms 14428 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14428 KB Output is correct
23 Correct 6 ms 14428 KB Output is correct
24 Correct 6 ms 14428 KB Output is correct
25 Correct 7 ms 14428 KB Output is correct
26 Correct 5 ms 14428 KB Output is correct
27 Correct 6 ms 14428 KB Output is correct
28 Correct 6 ms 14428 KB Output is correct
29 Correct 6 ms 14428 KB Output is correct
30 Correct 7 ms 14428 KB Output is correct
31 Correct 12 ms 14428 KB Output is correct
32 Correct 6 ms 14428 KB Output is correct
33 Correct 7 ms 14604 KB Output is correct
34 Correct 16 ms 14936 KB Output is correct
35 Correct 380 ms 20304 KB Output is correct
36 Correct 439 ms 23024 KB Output is correct
37 Correct 415 ms 23124 KB Output is correct
38 Correct 406 ms 22864 KB Output is correct
39 Correct 12 ms 15196 KB Output is correct
40 Correct 13 ms 15284 KB Output is correct
41 Correct 12 ms 15196 KB Output is correct
42 Correct 12 ms 14680 KB Output is correct
43 Correct 11 ms 15196 KB Output is correct
44 Correct 12 ms 14940 KB Output is correct
45 Correct 11 ms 14684 KB Output is correct
46 Correct 12 ms 14684 KB Output is correct
47 Correct 13 ms 14940 KB Output is correct
48 Correct 12 ms 14940 KB Output is correct
49 Correct 24 ms 16216 KB Output is correct
50 Correct 398 ms 23248 KB Output is correct
51 Correct 20 ms 15196 KB Output is correct
52 Correct 370 ms 21588 KB Output is correct
53 Correct 23 ms 15960 KB Output is correct
54 Correct 434 ms 22256 KB Output is correct
55 Correct 15 ms 15704 KB Output is correct
56 Correct 14 ms 15196 KB Output is correct
57 Correct 13 ms 15196 KB Output is correct
58 Correct 13 ms 15240 KB Output is correct
59 Correct 13 ms 15196 KB Output is correct
60 Correct 373 ms 20252 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 409 ms 22532 KB Output is correct
63 Correct 703 ms 67668 KB Output is correct
64 Correct 723 ms 67528 KB Output is correct
65 Correct 729 ms 67516 KB Output is correct
66 Incorrect 405 ms 33624 KB Output isn't correct
67 Halted 0 ms 0 KB -