Root of the problem

QUESTION

Chef has a binary tree. The binary tree consists of 1 or more nodes. Each node has a unique integer id. Each node has up to 2 children, which are identified by their ids, and each node is the child of at most 1 other node. A node X is considered to be an ancestor of node Y if node Y is a child of node X or if there is some node Z for which X is an ancestor of Z and Y is a child of Z. No node is an ancestor of itself. A special node called the root node is an ancestor of all other nodes.\n\nChef has forgotten which node of his tree is the root, and wants you to help him to figure it out. Unfortunately, Chef’s knowledge of the tree is incomplete. He does not remember the ids of the children of each node, but only remembers the sum of the ids of the children of each node.\n\nInput\nInput begins with an integer T, the number of test cases. Each test case begins with an integer N, the number of nodes in the tree. N lines follow with 2 integers each: the id of a node, and the sum of the ids of its children. The second number will be 0 if the node has no children.\n\nOutput\nFor each test case, output on a line a space separated list of all possible values for the id of the root node in increasing order. It is guaranteed that at least one such id exists for each test case.\n\nConstraints :\n\n1 <=T <=50\n1 <= N<= 30\nAll node ids are between 1 and 1000, inclusive.

“TESTCASE_1”: “1\n1\n4 0\n###—###SEPERATOR—###—\n4”, “TESTCASE_2”: “1\n4\n1 5\n2 0\n3 0\n4 0\n###—###SEPERATOR—###—\n5”, “TESTCASE_3”: “1\n6\n1 5\n2 0\n3 0\n4 0\n5 5\n6 5\n###—###SEPERATOR—###—\n6”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
 
int main()
{
int fall, a, b, i, n;
int A[100], B[100];
for(scanf("%d",&fall); fall--;)
	{
	scanf("%d",&n);
	for(i=0; i<n; i++)
		scanf("%d %d",&A[i],&B[i]);
	a=b=0;
	for(i=0; i<n; i++)
		{
		a+=A[i];
		b+=B[i];
		}
	printf("%d\n",a-b);
	}
return 0;
}

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock