# Coloring Tree

### Problem Statement :

```You are given a tree with N nodes with every node being colored. A color is represented by an integer ranging from 1 to 109. Can you find the number of distinct colors available in a subtree rooted at the node s?

Input Format

The first line contains three space separated integers representing the number of nodes in the tree (N), number of queries to answer (M) and the root of the tree.

In each of the next N-1 lines, there are two space separated integers(a b) representing an edge from node a to Node b and vice-versa.

N lines follow: N+ith line contains the color of the ith node.

M lines follow: Each line containg a single integer s.

Output Format

Output exactly M lines, each line containing the output of the ith query.

Constraints

0 <= M <= 105
1 <= N <= 105
1 <= root <= N
1 <= color of the Node <= 109```

### Solution :

```                            ```Solution in C :

In   C++  :

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

#define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++ e)
#define next nxt

const int N = 100000 + 10;
const int MOD = 1000000000 + 7;

int n, m, r;
int tot;
int color[N];
int father[N];
int sqn[N];
int ret[N];
int ord[N][2];
int next[N];
int c[N];
pair< pair<int, int>, int> query[N];

void dfs(int u)
{
sqn[tot] = color[u];
ord[u][0] = tot ++;
int v = *it;
if (v == father[u]) continue;
father[v] = u;
dfs(v);
}
ord[u][1] = tot;
}

{
for( ; u <= n; u += u & -u)
c[u] += x;
}

{
int ret = 0;
for( ; u; u -= u & -u)
ret += c[u];
return ret;
}

void solve()
{
cin >> n >> m >> r;
int u, v;
for(int i = 1; i < n; ++ i) {
scanf("%d%d", &u, &v);
-- u, -- v;
}
-- r;
father[r] = -1;
for(int i = 0; i < n; ++ i) {
scanf("%d", &color[i]);
}
tot = 0;
dfs(r);
for(int i = 0; i < n; ++ i) {
query[i] = make_pair(make_pair(ord[i][0], ord[i][1]), i);
}
sort(query, query + n);
map<int, int> s;
for(int i = n - 1; i >= 0; -- i) {
if (s.count(sqn[i]) == 0) {
next[i] = -1;
} else {
next[i] = s[sqn[i]];
}
s[sqn[i]] = i;
}
foreach(it, s) {
}
int l, r;
int ptr = 0;
for(int i = 0; i < n; ++ i) {
l = query[i].first.first;
r = query[i].first.second;
for ( ; ptr < l; ) {
if (next[ptr] != -1)
++ ptr;
}
}
for(int i = 0; i < n; ++ i) {
}
for(int i = 0; i < m; ++ i) {
scanf("%d", &u);
-- u;
printf("%d\n", ret[u]);
}
}

int main()
{
solve();
return 0;
}

In   Java  :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class ColoringTree {
public static class TreeNode {
ArrayList<TreeNode> ch;
int color, numberOfColors;
boolean visited;

TreeNode() {
ch = new ArrayList<TreeNode>();
visited = false;
}
}

public static TreeSet<Integer> colors(TreeNode node) {
TreeSet<Integer> combined;
node.visited = true;
combined = new TreeSet<Integer>();
for (TreeNode child : node.ch) {
if (child.visited)
continue;
TreeSet<Integer> col = colors(child);
if (col.size() > combined.size())
combined = col;
}
for (TreeSet<Integer> col : childColors) {
if (col != combined)
}
node.numberOfColors = combined.size();
return combined;
}

public static void main(String[] args) {
int n, m, root;
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
root = in.nextInt();
TreeNode[] nodes = new TreeNode[n + 1];
for (int i = 1; i <= n; i++)
nodes[i] = new TreeNode();
for (int i = 0; i < n - 1; i++) {
int a, b;
a = in.nextInt();
b = in.nextInt();
}
for (int i = 1; i <= n; i++) {
nodes[i].color = in.nextInt();
}
colors(nodes[root]);
for (int i = 0; i < m; i++) {
System.out.println(nodes[in.nextInt()].numberOfColors);
}

}
}

In   Python3 :

from collections import Counter
n, m, root = map(int, input().split())
uniquenum = dict()
multipleset = dict()
for _ in range(n-1):
n1, n2 = map(int, input().split())
else:
else:

colors = [int(input()) for _ in range(n)]
multiples = set(Counter(colors)-Counter(set(colors)))
colors.insert(0, 0)
totalcolors = len(set(colors[1:]))

stack = [root]
visited = set()
while len(stack)>0:
node = stack[len(stack)-1]
if node not in visited:
stack.append(child)
else:
if colors[node] in multiples:
uniquenum[node] = 0
multipleset[node] = set([colors[node]])
else:
uniquenum[node] = 1
multipleset[node] = set()
uniquenum[node] += uniquenum[child]
multipleset[node] |= multipleset[child]
stack.pop()

for _ in range(m):
node = int(input())
print(uniquenum[node]+len(multipleset[node]))```
```

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <