Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Mancunian And Colored Tree problem solution

YASH PAL, 31 July 2024
In this HackerEarth Mancunian And Colored Tree problem solution After a hectic week at work, Mancunian and Liverbird decide to go on a fun weekend camping trip. As they were passing through a forest, they stumbled upon a unique tree of N nodes. Vertices are numbered from 1 to N.
Each node of the tree is assigned a color (out of C possible colors). Being bored, they decide to work together (for a change) and test their reasoning skills. The tree is rooted at vertex 1. For each node, they want to find its closest ancestor having the same color.
HackerEarth Mancunian And Colored Tree problem solution

HackerEarth Mancunian And Colored Tree problem solution.

#include <iostream>
#include <stack>
#include <vector>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2*(n))
#define RIGHT(n) (2*(n)+1)

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;


#define SIZE 100005
int n, C, col[SIZE], ans[SIZE];
vector<int> adj[SIZE];
stack<int> st[SIZE];


void dfs(int v){

if(st[col[v]].empty()){
ans[v] = -1;
}
else{
ans[v] = st[col[v]].top();
}

st[col[v]].push(v);

for(int i=0;i<(int)adj[v].size();i++){
int vv = adj[v][i];
dfs(vv);
}

st[col[v]].pop();
}


int main(){

cin>>n>>C;
for(int i=2;i<=n;i++){
int p;
cin>>p;
adj[p].pb(i);
}
for(int i=1;i<=n;i++)
cin>>col[i];

dfs(1);
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector <int> G[MAXN], col_list[MAXN];
int col[MAXN], ans[MAXN];
void dfs(int pos)
{
if(col_list[col[pos]].empty())
ans[pos] = -1;
else
ans[pos] = col_list[col[pos]].back();
col_list[col[pos]].push_back(pos);
for (int i = 0; i < G[pos].size(); ++i)
{
dfs(G[pos][i]);
}
col_list[col[pos]].pop_back();
}
int main()
{
int n,c;
cin>>n>>c;
for (int i = 2; i <= n; ++i)
{
int par;
cin>>par;
G[par].push_back(i);
}
for (int i = 1; i <= n; ++i)
{
cin>>col[i];
}
dfs(1);
for (int i = 1; i <= n; ++i)
{
cout<<ans[i]<<" ";
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes