Coloring Tree
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
Example
Sample Input
4 2 1
1 2
2 4
2 3
10
20
20
30
1
2
Sample Output
3
2
Explanation
Query 1-Subtree rooted at 1 is the entire tree and colors used are 10 20 20 30 , so the answer is 3(10,20 and 30)
Query 2-Subtree rooted at 2 contains color 20 20 30, so the answer is 2(20 and 30)
xxxxxxxxxx
char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);
int parse_int(char*);
/*
* Complete the 'solve' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. 2D_INTEGER_ARRAY tree
* 2. INTEGER_ARRAY color
* 3. INTEGER_ARRAY s
*/
/*
* To return the integer array from the function, you should:
* - Store the size of the array to be returned in the result_count variable
* - Allocate the array statically or dynamically
*
* For example,
* int* return_integer_array_using_static_allocation(int* result_count) {
* *result_count = 5;
*
* static int a[5] = {1, 2, 3, 4, 5};
*
* return a;
* }
*
* int* return_integer_array_using_dynamic_allocation(int* result_count) {
* *result_count = 5;
*
* int *a = malloc(5 * sizeof(int));
*
* for (int i = 0; i < 5; i++) {
* *(a + i) = i + 1;
* }
*
* return a;
* }
*
*/
int* solve(int tree_rows, int tree_columns, int** tree, int color_count, int* color, int s_count, int* s, int* result_count) {
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char** first_multiple_input = split_string(rtrim(readline()));
int n = parse_int(*(first_multiple_input + 0));
int m = parse_int(*(first_multiple_input + 1));
int r = parse_int(*(first_multiple_input + 2));
int** tree = malloc((n - 1) * sizeof(int*));
for (int i = 0; i < n - 1; i++) {
*(tree + i) = malloc(2 * (sizeof(int)));
char** tree_item_temp = split_string(rtrim(readline()));
for (int j = 0; j < 2; j++) {
int tree_item = parse_int(*(tree_item_temp + j));
*(*(tree + i) + j) = tree_item;
}
}
int* color = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
int color_item = parse_int(ltrim(rtrim(readline())));
*(color + i) = color_item;
}
int* s = malloc(m * sizeof(int));
for (int i = 0; i < m; i++) {
int s_item = parse_int(ltrim(rtrim(readline())));
*(s + i) = s_item;
}
int result_count;
int* result = solve(n - 1, 2, tree, n, color, m, s, &result_count);
for (int i = 0; i < result_count; i++) {
fprintf(fptr, "%d", *(result + i));
if (i != result_count - 1) {
fprintf(fptr, "\n");
}
}
fprintf(fptr, "\n");
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}