답안 #1084032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084032 2024-09-04T21:44:13 Z stefanneagu 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1041 ms 68780 KB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e5 + 1;

int root[nmax], sz[nmax];
long long 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14428 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 5 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 6 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 14428 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 5 ms 14428 KB Output is correct
13 Correct 5 ms 14424 KB Output is correct
14 Correct 5 ms 14364 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 7 ms 14428 KB Output is correct
20 Correct 8 ms 14424 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14540 KB Output is correct
23 Correct 7 ms 14428 KB Output is correct
24 Correct 5 ms 14552 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 5 ms 14428 KB Output is correct
28 Correct 4 ms 14428 KB Output is correct
29 Correct 5 ms 14428 KB Output is correct
30 Correct 5 ms 14428 KB Output is correct
31 Correct 7 ms 14764 KB Output is correct
32 Correct 5 ms 14428 KB Output is correct
33 Correct 7 ms 14460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14428 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 5 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 6 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 14428 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 5 ms 14428 KB Output is correct
13 Correct 5 ms 14424 KB Output is correct
14 Correct 5 ms 14364 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 7 ms 14428 KB Output is correct
20 Correct 8 ms 14424 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14540 KB Output is correct
23 Correct 7 ms 14428 KB Output is correct
24 Correct 5 ms 14552 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 5 ms 14428 KB Output is correct
28 Correct 4 ms 14428 KB Output is correct
29 Correct 5 ms 14428 KB Output is correct
30 Correct 5 ms 14428 KB Output is correct
31 Correct 7 ms 14764 KB Output is correct
32 Correct 5 ms 14428 KB Output is correct
33 Correct 7 ms 14460 KB Output is correct
34 Correct 20 ms 14936 KB Output is correct
35 Correct 382 ms 17892 KB Output is correct
36 Correct 388 ms 20568 KB Output is correct
37 Correct 393 ms 20560 KB Output is correct
38 Correct 389 ms 20564 KB Output is correct
39 Correct 11 ms 14680 KB Output is correct
40 Correct 12 ms 15452 KB Output is correct
41 Correct 13 ms 14996 KB Output is correct
42 Correct 10 ms 15192 KB Output is correct
43 Correct 12 ms 14684 KB Output is correct
44 Correct 13 ms 14684 KB Output is correct
45 Correct 12 ms 14684 KB Output is correct
46 Correct 12 ms 14876 KB Output is correct
47 Correct 12 ms 15452 KB Output is correct
48 Correct 12 ms 14936 KB Output is correct
49 Correct 25 ms 15748 KB Output is correct
50 Correct 384 ms 21196 KB Output is correct
51 Correct 21 ms 14936 KB Output is correct
52 Correct 384 ms 18924 KB Output is correct
53 Correct 23 ms 15452 KB Output is correct
54 Correct 383 ms 19780 KB Output is correct
55 Correct 14 ms 15196 KB Output is correct
56 Correct 14 ms 15196 KB Output is correct
57 Correct 13 ms 15196 KB Output is correct
58 Correct 15 ms 15196 KB Output is correct
59 Correct 14 ms 15708 KB Output is correct
60 Correct 373 ms 17640 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 389 ms 20096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14428 KB Output is correct
2 Correct 6 ms 14428 KB Output is correct
3 Correct 5 ms 14428 KB Output is correct
4 Correct 4 ms 14428 KB Output is correct
5 Correct 6 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 14428 KB Output is correct
9 Correct 7 ms 14428 KB Output is correct
10 Correct 6 ms 14424 KB Output is correct
11 Correct 6 ms 14428 KB Output is correct
12 Correct 5 ms 14428 KB Output is correct
13 Correct 5 ms 14424 KB Output is correct
14 Correct 5 ms 14364 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 4 ms 14428 KB Output is correct
17 Correct 5 ms 14940 KB Output is correct
18 Correct 6 ms 14428 KB Output is correct
19 Correct 7 ms 14428 KB Output is correct
20 Correct 8 ms 14424 KB Output is correct
21 Correct 8 ms 14428 KB Output is correct
22 Correct 5 ms 14540 KB Output is correct
23 Correct 7 ms 14428 KB Output is correct
24 Correct 5 ms 14552 KB Output is correct
25 Correct 8 ms 14428 KB Output is correct
26 Correct 6 ms 14428 KB Output is correct
27 Correct 5 ms 14428 KB Output is correct
28 Correct 4 ms 14428 KB Output is correct
29 Correct 5 ms 14428 KB Output is correct
30 Correct 5 ms 14428 KB Output is correct
31 Correct 7 ms 14764 KB Output is correct
32 Correct 5 ms 14428 KB Output is correct
33 Correct 7 ms 14460 KB Output is correct
34 Correct 20 ms 14936 KB Output is correct
35 Correct 382 ms 17892 KB Output is correct
36 Correct 388 ms 20568 KB Output is correct
37 Correct 393 ms 20560 KB Output is correct
38 Correct 389 ms 20564 KB Output is correct
39 Correct 11 ms 14680 KB Output is correct
40 Correct 12 ms 15452 KB Output is correct
41 Correct 13 ms 14996 KB Output is correct
42 Correct 10 ms 15192 KB Output is correct
43 Correct 12 ms 14684 KB Output is correct
44 Correct 13 ms 14684 KB Output is correct
45 Correct 12 ms 14684 KB Output is correct
46 Correct 12 ms 14876 KB Output is correct
47 Correct 12 ms 15452 KB Output is correct
48 Correct 12 ms 14936 KB Output is correct
49 Correct 25 ms 15748 KB Output is correct
50 Correct 384 ms 21196 KB Output is correct
51 Correct 21 ms 14936 KB Output is correct
52 Correct 384 ms 18924 KB Output is correct
53 Correct 23 ms 15452 KB Output is correct
54 Correct 383 ms 19780 KB Output is correct
55 Correct 14 ms 15196 KB Output is correct
56 Correct 14 ms 15196 KB Output is correct
57 Correct 13 ms 15196 KB Output is correct
58 Correct 15 ms 15196 KB Output is correct
59 Correct 14 ms 15708 KB Output is correct
60 Correct 373 ms 17640 KB Output is correct
61 Correct 13 ms 14936 KB Output is correct
62 Correct 389 ms 20096 KB Output is correct
63 Correct 744 ms 64416 KB Output is correct
64 Correct 738 ms 64080 KB Output is correct
65 Correct 714 ms 64516 KB Output is correct
66 Correct 394 ms 31496 KB Output is correct
67 Correct 597 ms 60496 KB Output is correct
68 Correct 411 ms 33716 KB Output is correct
69 Correct 567 ms 34896 KB Output is correct
70 Correct 396 ms 33616 KB Output is correct
71 Correct 417 ms 33748 KB Output is correct
72 Correct 512 ms 44884 KB Output is correct
73 Correct 533 ms 46436 KB Output is correct
74 Correct 1041 ms 68180 KB Output is correct
75 Correct 756 ms 41552 KB Output is correct
76 Correct 874 ms 53508 KB Output is correct
77 Correct 845 ms 53844 KB Output is correct
78 Correct 360 ms 37424 KB Output is correct
79 Correct 647 ms 41812 KB Output is correct
80 Correct 346 ms 36176 KB Output is correct
81 Correct 588 ms 39760 KB Output is correct
82 Correct 826 ms 54288 KB Output is correct
83 Correct 766 ms 54356 KB Output is correct
84 Correct 750 ms 53072 KB Output is correct
85 Correct 671 ms 53024 KB Output is correct
86 Correct 477 ms 66416 KB Output is correct
87 Correct 646 ms 68780 KB Output is correct
88 Correct 527 ms 45648 KB Output is correct
89 Correct 834 ms 51592 KB Output is correct