Skip to content

Instantly share code, notes, and snippets.

@zaburo-ch
Created January 8, 2018 18:17
Show Gist options
  • Save zaburo-ch/d00d7084cbd38f956168bc3954b2d356 to your computer and use it in GitHub Desktop.
Save zaburo-ch/d00d7084cbd38f956168bc3954b2d356 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main(){
int n;
cin >> n;
vector<int> child_cnt(n);
vector<int> par(n);
for(int i=1;i<n;i++){
int p;
cin >> p;
par[i] = p - 1;
child_cnt[p - 1]++;
}
vector<int> non_leaf_cnt = child_cnt;
for(int i=1;i<n;i++){
if(child_cnt[i] > 0){
non_leaf_cnt[par[i]]--;
}
}
bool ok = true;
for(int i=0;i<n;i++){
if(non_leaf_cnt[i] == 2 || non_leaf_cnt[i] == 1){
ok = false;
break;
}
}
if(ok) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment