Submission #64796

#TimeUsernameProblemLanguageResultExecution timeMemory
64796kjp4155족보 (KOI18_family)C++17
100 / 100
753 ms98900 KiB
#include <stdio.h> #include <algorithm> #include <utility> #include <vector> using namespace std; typedef pair<int,int> Pi; /* Union Find data structure */ int pa[300500], cnt[300500]; int find(int x){ if( pa[x] == x ) return x; return pa[x] = find(pa[x]); } void uni(int a, int b){ a = find(a); b = find(b); if( a == b ) return; pa[a] = b; cnt[b] += cnt[a]; } struct event{ int t, len, level, node; // Smaller event first. Deeper event first. bool operator < (event e){ if( len != e.len ) return len < e.len; return level > e.level; } }; int N[2],K; vector<int> E[2][300500]; int root[2]; int sz[2][300500], leaf[2][300500], level[2][300500]; int timer = 0; Pi dfs(int t, int x, int l){ level[t][x] = l; if( 1 <= x && x <= K ){ timer++; leaf[t][x] = x; sz[t][x] = 1; return {x,1}; } for(auto e : E[t][x]){ Pi res = dfs(t, e, l+1); leaf[t][x] = res.first; sz[t][x] += res.second; } return {leaf[t][x], sz[t][x]}; } int main(){ scanf("%d%d%d",&N[0],&N[1],&K); for(int i=1;i<=K;i++){ pa[i] = i; cnt[i] = 1; } for(int t=0;t<2;t++){ for(int i=1;i<=N[t];i++){ int x; scanf("%d",&x); if( x == 0 ) root[t] = i; else E[t][x].push_back(i); } } dfs( 0, root[0] , 0); timer = 0; dfs( 1, root[1] , 0); vector<event> events; for(int t=0;t<2;t++){ for(int i=K+1;i<=N[t];i++){ events.push_back({t, sz[t][i], level[t][i], i}); } } sort(events.begin(), events.end()); for(auto e : events){ //printf("[%d,%d] %d %d \n",e.t, e.node, e.len, e.level); for(auto x : E[e.t][e.node]){ uni(leaf[e.t][e.node], leaf[e.t][x]); } //printf("%d %d\n",cnt[find(leaf[e.t][e.node])], e.len); if( cnt[find(leaf[e.t][e.node])] != e.len ){ printf("NO\n"); return 0; } } printf("YES\n"); }

Compilation message (stderr)

family.cpp: In function 'int main()':
family.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d",&N[0],&N[1],&K);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
family.cpp:64:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int x; scanf("%d",&x);
           ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...