Root of the Problem

Problem Statement :

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.

Chef 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.


Input 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.


For 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.


1 ≤ T ≤ 50
1 ≤ N ≤ 30
All node ids are between 1 and 1000, inclusive

Sample Input

4 0
1 5
2 0
3 0
4 0
5 5
6 5

Sample Output


Solution :

                            Solution in C :

#include <stdio.h>

int main(void) {
	int t, n, id_sum, sid_sum, x, y;
	scanf("%d", &t);
	while(t--) {
	    scanf("%d", &n);
	    id_sum = 0, sid_sum = 0;
	    while(n--) {
    	    scanf("%d %d", &x, &y);
    	    id_sum += x;
    	    sid_sum += y;
	    printf("%d\n", id_sum - sid_sum);
	return 0;

                        Solution in C++ :

#include <iostream>
using namespace std;

#define ll long long int

int main() {
	ll t;
	for(ll z=1;z<=t;z++){
		ll n;
		int ans=0;
		for(int j=1;j<=n;j++){
			int x,y;
	return 0;

                        Solution in Java :

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
	public static void main (String[] args) throws java.lang.Exception
		// your code goes here
		Scanner sc = new Scanner(;
		int t = sc.nextInt();
		while(t-- > 0)
		    int n = sc.nextInt();
		    int node = 0;
		    int idsum= 0;
		    for(int i=0;i<n;i++)
		        node = node + sc.nextInt();
		        idsum = idsum + sc.nextInt();
		    int r = node - idsum;

                        Solution in Python : 
# cook your dish here
for _ in range(t):
    n = int(input())
    root = 0
    for j in range(n):
        ide,c = map(int,input().split(' '))
        root +=ide - c

View More Similar Problems

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →

Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →

Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →