Submission #403474

#TimeUsernameProblemLanguageResultExecution timeMemory
403474shenxy족보 (KOI18_family)C++11
59 / 100
47 ms34884 KiB
#include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef pair<int, int> ii; struct UFDS { vector<int> parent, size; UFDS(int N) { for (int i = 0; i < N; ++i) parent.push_back(i), size.push_back(1); } int find_parent(int i) { if (parent[i] == i) return i; return parent[i] = find_parent(parent[i]); } int get_size(int i) { return size[find_parent(i)]; } void union_set(int u, int v) { u = find_parent(u), v = find_parent(v); if (u == v) return; if (size[u] < size[v]) parent[u] = v, size[v] += size[u]; else parent[v] = u, size[u] += size[v]; } }; vector<int> children[10000], leaf[10000]; void dfs(int v) { for (int i: children[v]) { dfs(i); for (int j: leaf[i]) leaf[v].push_back(j); } } int main() { int N1, N2, K, p, r1, r2; scanf("%d %d %d", &N1, &N2, &K); for (int i = 0; i < K; ++i) leaf[i].push_back(i); for (int i = 0; i < N1; ++i) { scanf("%d", &p); if (p != 0) children[p - 1].push_back(i); else r1 = i; } for (int i = 0; i < K; ++i) { scanf("%d", &p); children[N1 + p - K - 1].push_back(i); } for (int i = 0; i < N2 - K; ++i) { scanf("%d", &p); if (p != 0) children[N1 + p - K - 1].push_back(N1 + i); else r2 = N1 + i; } dfs(r1); dfs(r2); ii srt[N1 + N2 - K]; for (int i = 0; i < N1 + N2 - K; ++i) srt[i] = ii(leaf[i].size(), i); sort(srt, srt + N1 + N2 - K); UFDS sets(K); for (int i = 0; i < N1 + N2 - K; ++i) { int x = srt[i].second; for (int i = 1; i < leaf[x].size(); ++i) sets.union_set(leaf[x][i - 1], leaf[x][i]); if (sets.get_size(leaf[x][0]) != leaf[x].size()) { printf("NO"); return 0; } } printf("YES"); return 0; }

Compilation message (stderr)

family.cpp: In function 'int main()':
family.cpp:58:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for (int i = 1; i < leaf[x].size(); ++i) sets.union_set(leaf[x][i - 1], leaf[x][i]);
      |                   ~~^~~~~~~~~~~~~~~~
family.cpp:59:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   if (sets.get_size(leaf[x][0]) != leaf[x].size()) {
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
family.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |  scanf("%d %d %d", &N1, &N2, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
family.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |   scanf("%d", &p);
      |   ~~~~~^~~~~~~~~~
family.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |   scanf("%d", &p);
      |   ~~~~~^~~~~~~~~~
family.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   scanf("%d", &p);
      |   ~~~~~^~~~~~~~~~
family.cpp:51:5: warning: 'r2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   51 |  dfs(r2);
      |  ~~~^~~~
family.cpp:50:5: warning: 'r1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |  dfs(r1);
      |  ~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...