930A - Peculiar apple-tree - CodeForces Solution


dfs and similar graphs trees *1500

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<functional>
#include<numeric>
#include<cmath>

using namespace std;

#define endl "\n"
#define ain(a) for(int _i=0;_i<a.size();_i++) cin >> a[_i]
#define aout(a) for(int _i=0;_i<a.size();_i++) { if(_i > 0) cout << " "; cout << a[_i]; }; cout << endl
#define couty cout << "YES\n"
#define coutn cout << "NO\n"

#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()

#ifdef LOCAL_JUDGE
#include "local.hpp"
#else
template <typename Head, typename... Tail> void dout(Head&& h, Tail&&... t) {}
#endif

typedef long long ll;
typedef pair<int,int> pii; typedef pair<ll,int> pli; typedef tuple<ll,int,int> tllii;
typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ll> vll; typedef vector<vector<int> > vvi; typedef vector<string> vs;

const ll MOD = 1e9+7;

void solve() {
	int n; cin >> n;
	vi a(n);
	for(int i=1;i<n;i++) {
		cin >> a[i];
		a[i]--;
	}
	vi h(n);
	map<int,int> hf;
	hf[0] = 1;
	for(int i=1;i<n;i++) {
		h[i] = h[a[i]]+1;
		hf[h[i]]++;
	}
	int count = 0;
	for(auto [_,c]:hf) if(c%2) count++;
	cout << count << endl;
}

int main(int argc,char *argv[]) {
#ifdef LOCAL_JUDGE
	freopen(argc > 1 ? argv[1] : "in.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int t = 1;
	// cin >> t;
	for(int i=1;i<=t;i++) {
		dout("***",i);
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds