답안 #698179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698179 2023-02-12T19:00:24 Z queuedq 족보 (KOI18_family) C++17
100 / 100
312 ms 48000 KB
#include <bits/stdc++.h>
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using lld = long long;
using pii = pair<int, int>;
using pll = pair<lld, lld>;

////////////////////////////////////////////////////////////////
const int MN = 303030;
int K, N1, N2;

struct Tree {
  int root, cnt[MN], rep[MN];
  vector<int> adj[MN];

  void set_parent(int u, int p) {
    if (p == 0) root = u;
    else adj[p].push_back(u);
  }

  void dfs(int u) {
    if (u <= K) cnt[u] = 1, rep[u] = u;
    for (auto v: adj[u]) {
      dfs(v);
      cnt[u] += cnt[v];
      rep[u] = rep[v];
    }
  }
};
Tree T1, T2;

// dsu
int par[MN], siz[MN];

int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}

void merge(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  par[y] = x;
  siz[x] += siz[y];
}

int size(int x) {
  return siz[find(x)];
}
// end dsu

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ////////////////////////////////

  cin >> N1 >> N2 >> K;
  for (int u=1, p; u<=N1; u++) { cin >> p; T1.set_parent(u, p); }
  for (int u=1, p; u<=N2; u++) { cin >> p; T2.set_parent(u, p); }

  T1.dfs(T1.root);
  T2.dfs(T2.root);

  vector<array<int, 3>> E;
  for (int u=K+1; u<=N1; u++) if (T1.adj[u].size() > 1) E.push_back({T1.cnt[u], 1, u});
  for (int u=K+1; u<=N2; u++) if (T2.adj[u].size() > 1) E.push_back({T2.cnt[u], 2, u});
  sort(all(E));

  for (int i=1; i<=K; i++) par[i] = i, siz[i] = 1;
  bool ok = 1;
  for (auto [c, t, u]: E) {
    Tree &T = t == 1 ? T1 : T2;
    for (auto v: T.adj[u]) merge(T.rep[u], T.rep[v]);
    if (size(T.rep[u]) != c) ok = 0;
  }
  cout << (ok ? "YES" : "NO") << endl;

  ////////////////////////////////
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 9 ms 14508 KB Output is correct
3 Correct 7 ms 14572 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 7 ms 14572 KB Output is correct
6 Correct 7 ms 14548 KB Output is correct
7 Correct 8 ms 14548 KB Output is correct
8 Correct 7 ms 14576 KB Output is correct
9 Correct 7 ms 14560 KB Output is correct
10 Correct 8 ms 14488 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14572 KB Output is correct
13 Correct 8 ms 14548 KB Output is correct
14 Correct 10 ms 14548 KB Output is correct
15 Correct 8 ms 14572 KB Output is correct
16 Correct 8 ms 14548 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 8 ms 14508 KB Output is correct
19 Correct 8 ms 14548 KB Output is correct
20 Correct 8 ms 14548 KB Output is correct
21 Correct 8 ms 14572 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14576 KB Output is correct
24 Correct 9 ms 14576 KB Output is correct
25 Correct 8 ms 14568 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 8 ms 14596 KB Output is correct
28 Correct 7 ms 14548 KB Output is correct
29 Correct 9 ms 14604 KB Output is correct
30 Correct 7 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 9 ms 14508 KB Output is correct
3 Correct 7 ms 14572 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 7 ms 14572 KB Output is correct
6 Correct 7 ms 14548 KB Output is correct
7 Correct 8 ms 14548 KB Output is correct
8 Correct 7 ms 14576 KB Output is correct
9 Correct 7 ms 14560 KB Output is correct
10 Correct 8 ms 14488 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14572 KB Output is correct
13 Correct 8 ms 14548 KB Output is correct
14 Correct 10 ms 14548 KB Output is correct
15 Correct 8 ms 14572 KB Output is correct
16 Correct 8 ms 14548 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 8 ms 14508 KB Output is correct
19 Correct 8 ms 14548 KB Output is correct
20 Correct 8 ms 14548 KB Output is correct
21 Correct 8 ms 14572 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14576 KB Output is correct
24 Correct 9 ms 14576 KB Output is correct
25 Correct 8 ms 14568 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 8 ms 14596 KB Output is correct
28 Correct 7 ms 14548 KB Output is correct
29 Correct 9 ms 14604 KB Output is correct
30 Correct 7 ms 14548 KB Output is correct
31 Correct 10 ms 14532 KB Output is correct
32 Correct 9 ms 14568 KB Output is correct
33 Correct 9 ms 14572 KB Output is correct
34 Correct 9 ms 14496 KB Output is correct
35 Correct 9 ms 14548 KB Output is correct
36 Correct 8 ms 14580 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 8 ms 14580 KB Output is correct
39 Correct 8 ms 14572 KB Output is correct
40 Correct 8 ms 14548 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 8 ms 14580 KB Output is correct
43 Correct 8 ms 14548 KB Output is correct
44 Correct 8 ms 14548 KB Output is correct
45 Correct 9 ms 14548 KB Output is correct
46 Correct 9 ms 14548 KB Output is correct
47 Correct 9 ms 14548 KB Output is correct
48 Correct 8 ms 14568 KB Output is correct
49 Correct 8 ms 14548 KB Output is correct
50 Correct 8 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 9 ms 14508 KB Output is correct
3 Correct 7 ms 14572 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 7 ms 14572 KB Output is correct
6 Correct 7 ms 14548 KB Output is correct
7 Correct 8 ms 14548 KB Output is correct
8 Correct 7 ms 14576 KB Output is correct
9 Correct 7 ms 14560 KB Output is correct
10 Correct 8 ms 14488 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14572 KB Output is correct
13 Correct 8 ms 14548 KB Output is correct
14 Correct 10 ms 14548 KB Output is correct
15 Correct 8 ms 14572 KB Output is correct
16 Correct 8 ms 14548 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 8 ms 14508 KB Output is correct
19 Correct 8 ms 14548 KB Output is correct
20 Correct 8 ms 14548 KB Output is correct
21 Correct 8 ms 14572 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14576 KB Output is correct
24 Correct 9 ms 14576 KB Output is correct
25 Correct 8 ms 14568 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 8 ms 14596 KB Output is correct
28 Correct 7 ms 14548 KB Output is correct
29 Correct 9 ms 14604 KB Output is correct
30 Correct 7 ms 14548 KB Output is correct
31 Correct 10 ms 14532 KB Output is correct
32 Correct 9 ms 14568 KB Output is correct
33 Correct 9 ms 14572 KB Output is correct
34 Correct 9 ms 14496 KB Output is correct
35 Correct 9 ms 14548 KB Output is correct
36 Correct 8 ms 14580 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 8 ms 14580 KB Output is correct
39 Correct 8 ms 14572 KB Output is correct
40 Correct 8 ms 14548 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 8 ms 14580 KB Output is correct
43 Correct 8 ms 14548 KB Output is correct
44 Correct 8 ms 14548 KB Output is correct
45 Correct 9 ms 14548 KB Output is correct
46 Correct 9 ms 14548 KB Output is correct
47 Correct 9 ms 14548 KB Output is correct
48 Correct 8 ms 14568 KB Output is correct
49 Correct 8 ms 14548 KB Output is correct
50 Correct 8 ms 14548 KB Output is correct
51 Correct 8 ms 14836 KB Output is correct
52 Correct 10 ms 14932 KB Output is correct
53 Correct 11 ms 14960 KB Output is correct
54 Correct 8 ms 14804 KB Output is correct
55 Correct 8 ms 14708 KB Output is correct
56 Correct 9 ms 14768 KB Output is correct
57 Correct 9 ms 14804 KB Output is correct
58 Correct 9 ms 14764 KB Output is correct
59 Correct 9 ms 14804 KB Output is correct
60 Correct 8 ms 14804 KB Output is correct
61 Correct 10 ms 15060 KB Output is correct
62 Correct 12 ms 15096 KB Output is correct
63 Correct 10 ms 14804 KB Output is correct
64 Correct 9 ms 14800 KB Output is correct
65 Correct 9 ms 14548 KB Output is correct
66 Correct 10 ms 14836 KB Output is correct
67 Correct 9 ms 14804 KB Output is correct
68 Correct 9 ms 14836 KB Output is correct
69 Correct 10 ms 14932 KB Output is correct
70 Correct 9 ms 14716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 9 ms 14508 KB Output is correct
3 Correct 7 ms 14572 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 7 ms 14572 KB Output is correct
6 Correct 7 ms 14548 KB Output is correct
7 Correct 8 ms 14548 KB Output is correct
8 Correct 7 ms 14576 KB Output is correct
9 Correct 7 ms 14560 KB Output is correct
10 Correct 8 ms 14488 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14572 KB Output is correct
13 Correct 8 ms 14548 KB Output is correct
14 Correct 10 ms 14548 KB Output is correct
15 Correct 8 ms 14572 KB Output is correct
16 Correct 8 ms 14548 KB Output is correct
17 Correct 8 ms 14548 KB Output is correct
18 Correct 8 ms 14508 KB Output is correct
19 Correct 8 ms 14548 KB Output is correct
20 Correct 8 ms 14548 KB Output is correct
21 Correct 8 ms 14572 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14576 KB Output is correct
24 Correct 9 ms 14576 KB Output is correct
25 Correct 8 ms 14568 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 8 ms 14596 KB Output is correct
28 Correct 7 ms 14548 KB Output is correct
29 Correct 9 ms 14604 KB Output is correct
30 Correct 7 ms 14548 KB Output is correct
31 Correct 10 ms 14532 KB Output is correct
32 Correct 9 ms 14568 KB Output is correct
33 Correct 9 ms 14572 KB Output is correct
34 Correct 9 ms 14496 KB Output is correct
35 Correct 9 ms 14548 KB Output is correct
36 Correct 8 ms 14580 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 8 ms 14580 KB Output is correct
39 Correct 8 ms 14572 KB Output is correct
40 Correct 8 ms 14548 KB Output is correct
41 Correct 8 ms 14588 KB Output is correct
42 Correct 8 ms 14580 KB Output is correct
43 Correct 8 ms 14548 KB Output is correct
44 Correct 8 ms 14548 KB Output is correct
45 Correct 9 ms 14548 KB Output is correct
46 Correct 9 ms 14548 KB Output is correct
47 Correct 9 ms 14548 KB Output is correct
48 Correct 8 ms 14568 KB Output is correct
49 Correct 8 ms 14548 KB Output is correct
50 Correct 8 ms 14548 KB Output is correct
51 Correct 8 ms 14836 KB Output is correct
52 Correct 10 ms 14932 KB Output is correct
53 Correct 11 ms 14960 KB Output is correct
54 Correct 8 ms 14804 KB Output is correct
55 Correct 8 ms 14708 KB Output is correct
56 Correct 9 ms 14768 KB Output is correct
57 Correct 9 ms 14804 KB Output is correct
58 Correct 9 ms 14764 KB Output is correct
59 Correct 9 ms 14804 KB Output is correct
60 Correct 8 ms 14804 KB Output is correct
61 Correct 10 ms 15060 KB Output is correct
62 Correct 12 ms 15096 KB Output is correct
63 Correct 10 ms 14804 KB Output is correct
64 Correct 9 ms 14800 KB Output is correct
65 Correct 9 ms 14548 KB Output is correct
66 Correct 10 ms 14836 KB Output is correct
67 Correct 9 ms 14804 KB Output is correct
68 Correct 9 ms 14836 KB Output is correct
69 Correct 10 ms 14932 KB Output is correct
70 Correct 9 ms 14716 KB Output is correct
71 Correct 29 ms 18512 KB Output is correct
72 Correct 31 ms 17904 KB Output is correct
73 Correct 22 ms 17108 KB Output is correct
74 Correct 32 ms 19788 KB Output is correct
75 Correct 40 ms 19380 KB Output is correct
76 Correct 256 ms 48000 KB Output is correct
77 Correct 301 ms 42468 KB Output is correct
78 Correct 179 ms 31360 KB Output is correct
79 Correct 203 ms 41236 KB Output is correct
80 Correct 250 ms 39804 KB Output is correct
81 Correct 28 ms 18436 KB Output is correct
82 Correct 31 ms 17888 KB Output is correct
83 Correct 19 ms 17136 KB Output is correct
84 Correct 32 ms 19712 KB Output is correct
85 Correct 30 ms 19352 KB Output is correct
86 Correct 312 ms 44232 KB Output is correct
87 Correct 283 ms 44256 KB Output is correct
88 Correct 188 ms 31292 KB Output is correct
89 Correct 224 ms 41060 KB Output is correct
90 Correct 230 ms 39788 KB Output is correct