Tree and Level | Practice | GeeksforGeeks

Given a tree with N nodes rooted at 1. Each node labeled with a value arr[i]. The task is to find the absolute difference between the sum of values of nodes at even level and odd level

Note: All the nodes are numbered from 1 to N. 
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer N.
3. The next line contains N space-separated positive integers represents the node value.
4. Next N-1 lines contain two space-separated integers u and v, represents an edge in between them

Output: For each test case, print the answer

Constraints:
1. 1 <= T <= 5
2. 1 <= N <= 105
3. 1 <= arr[i] <= 105


Example:
Input:
2
4
1 2 3 4
1 2
3 4
4 1
5
1 2 1 2 1
1 2
2 3
3 4
4 5
Output:
2
1
0
anutiger6 months ago
Do not  create adjacency list, create ab unordered map in form of adjacency list.
 
    int t;
	cin>> t;
	while(t--){
	    int n;
	    cin >> n;
	    int arr[n];
	    for(int i = 0 ; i < n ; i ++){
	        int a; cin >> a;
	        arr[i] = a;
	    }
	    unordered_map<int,vector<int>> adj;
	    for(int i = 1 ; i <= n - 1; i ++){
	        int a,b;
	        cin >> a >> b;
	        adj[a].push_back(b);
	        adj[b].push_back(a);
	    }
	    int even = 0;
	    int odd = 0;
	    queue<pair<int,int>>q;
	    q.push({1,1});
	    vector<int> vis(n,0);
	    while(!q.empty()){
	        auto it = q.front();q.pop();
	        if(it.second == 1){
	            odd+= arr[it.first - 1];
	        }
	        else even += arr[it.first - 1];
	        vis[it.first - 1] = 1;
	        int flag = 1;
	        if(it.second == 1) flag = 2;
	        for(int j : adj[it.first]){
	                if(vis[j - 1] == 0) q.push({j,flag});
	        }
	    }
	    cout<<abs(even - odd)<<endl;
	}
 
0
imranwahid6 months ago
Easy C++ solution with BFS
https://ide.geeksforgeeks.org/a6moHaGY36
#include<bits/stdc++.h>
using namespace std;
// helper function to find the abs value of diff of odd and even level sum
int helper(unordered_map<int,vector<int>>&m,vector<int>&val)
{
    if(val.size()==0)
    {
        return 0;
    }
    // visited array will tell whether a particular node has been visited or not
    vector<int>visited(val.size()+1,false);
    
    // queue for running a BFS call
    queue<int>q;
    
    // as 1 is the root
    q.push(1);
    
    // level is the current level , odd is the odd level sum and same goes for even
    int level=0,even=0,odd=0;
    
    // mark the root as visited
    visited[1]=true;
    
    // run the BFS
    while(!q.empty())
    {
        int n=q.size();
        while(n)
        {
            int node=q.front();
            q.pop();
            if(level%2==0)
            {
                even+=val[node-1];
            }
            else
            {
                odd+=val[node-1];
            }
            for(auto it:m[node])
            {
                if(!visited[it])
                {
                    visited[it]=true;
                    q.push(it);
                }
            }
            n--;
        }
        level++;
    }
    return abs(odd-even);
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    vector<int>val(n);
	    for(int i=0;i<n;i++)
	    {
	        cin>>val[i];
	    }
	    // the map will store the graph where each node is the label not the actual value
	    unordered_map<int,vector<int>>m;
	    for(int i=0;i<n-1;i++)
	    {
	        int u,v;
	        cin>>u>>v;
	        m[u].push_back(v);
	        m[v].push_back(u);
	    }
	    cout<<helper(m,val)<<endl;
	}
	return 0;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.