Submission #557500

#TimeUsernameProblemLanguageResultExecution timeMemory
557500myrcella족보 (KOI18_family)C++17
0 / 100
22 ms45004 KiB
//by szh #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define pll pair<long long,long long> #define pb push_back #define debug(x) cerr<<#x<<"="<<x<<endl #define pq priority_queue #define inf 0x3f #define rep(i,a,b) for (int i=a;i<(b);i++) #define MP make_pair #define SZ(x) (int(x.size())) #define ll long long #define mod 1000000007 #define ALL(x) x.begin(),x.end() void inc(int &a,int b) {a=(a+b)%mod;} void dec(int &a,int b) {a=(a-b+mod)%mod;} int lowbit(int x) {return x&(-x);} ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;} const int maxn = 3e5+10; int n[2],m; int fa[2][maxn]; map <int,int> mp[2][maxn]; int sz[2][maxn]; vector <int> edge[2][maxn]; int getf(int id,int node) { if (fa[id][node]==node) return fa[id][node]; else return fa[id][node] = getf(id,fa[id][node]); } int p[2][maxn]; pq <pair<int,pii>, vector<pair<int,pii>>, greater <pair<int,pii>>> q; int dfs(int id,int u) { if (u<m) return 1; sz[id][u]=1; for (int v:edge[id][u]) { sz[id][u]+=dfs(id,v); } return sz[id][u]; } int main() { // freopen("input.txt","r",stdin); std::ios::sync_with_stdio(false);cin.tie(); cin>>n[0]>>n[1]>>m; memset(sz,0,sizeof(sz)); int rt[2]; rep(k,0,2) rep(i,0,n[k]) { cin>>p[k][i]; p[k][i]--; if (p[k][i]!=-1) edge[k][p[k][i]].pb(i); else rt[k] = i; if (i<m) fa[k][i] = p[k][i]; else fa[k][i] = i; } dfs(0,rt[0]); dfs(1,rt[1]); rep(k,0,2) rep(i,0,m) { int ff1 = getf(k,i); int ff2 = getf(1-k,i); mp[k][ff1][ff2] = 1; } rep(i,0,2) rep(j,m,n[i]) { q.push({sz[i][j],{i,j}}); } while (!q.empty()) { auto cur = q.top(); // debug(cur.se.fi),debug(cur.se.se); q.pop(); int tmp = getf(1-cur.se.fi,(mp[cur.se.fi][cur.se.se].begin())->fi); if (mp[cur.se.fi][cur.se.se].size()>1) { for (auto it:mp[cur.se.fi][cur.se.se]) { if (getf(1-cur.se.fi,it.fi)!=tmp) { cout<<"NO"; return 1; } } } // debug(tmp); if (p[cur.se.fi][cur.se.se]!=-1) mp[cur.se.fi][p[cur.se.fi][cur.se.se]][tmp] = 1, fa[cur.se.fi][cur.se.se] = p[cur.se.fi][cur.se.se]; } cout<<"YES"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...