Sunday, March 9, 2014

finding the longest path in a tree

There is a very good task in spoj which requires finding the longest path in a tree.
Go here to find the link to the task, the explanation and teh full source code for this task.

SPOJ 1437. Longest path in a tree

http://www.spoj.com/problems/PT07Z/

This task is solved with 2 BFSs. (If you need more info about BFS go here). Here is how its done. First of all we need to start a BFS from any node we want and find the furthest node from the starting node. But wait, the path we just found isn't the longest, the node we just found is the starting point for our real longest path. Now we need to find the furthest node from the node we just found, that will be our answer.
So , for summarizing, we need 2 BFS, the first one starts with any node and ends on some node X.Xis the furthest form the starting node. Then the second BFS finds the furthest node from X. and the path is the path between those 2 nodes. Check my source code for a better understanding..


       DOWNLOAD THE FULL SOURCE CODE







Monday, February 24, 2014

SPOJ 11931. AMZ Word

The problem statement

We can solve this task using dynamic programming. Suppose we have the number of words which end with zero, one and two. Lets make arrays zero[N], one[N], two[N] where zero[I], one[I], two[I] each will show the number of words with length I ending with 0, 1 and 2 respectively. The task says to find the number of words which have a neighboring digit difference at most 1 which means that numbers 0 and 2 can't be neighbors. zero[I] shows the number of words which have length I and end with 0. So I can say that when it comes to calculating the words with length I+1 there will be at least zero[I] words which will end in 0 and zero[I] words which will end 1 because after 0 only the digits 0 and 1 can come. So here is a general formula
suppose we have the answer for the length I ,
the formula for I+1 would be
zero[i]=zero[i-1]+one[i-1]; (Because we can add 0 and 1 to 0)
one[i]=zero[i-1]+one[i-1]+two[i-1]; (because we can add 0 1 and 2 to 1)
two[i]=one[i-1]+two[i-1]; (because we can add 1 and 2 to 2)


       DOWNLOAD THE FULL SOURCE CODE

Sunday, February 23, 2014

SPOJ 13046. The Glittering Caves of Aglarond


here is one statement which will make this task a lot easier. Every time we are required to click on switch, I say we should click a row which has the minimum number of on turned lights. Why?
because if there number of ON switches is less then the half of the row then we will "win" a few more lights (by saying win I mean get a better number) , if the number of ON turned switches is more then the half we will have the minimum "lose" note that every time we click a switch the OFF turned lights become ON and the rest ON turned lights become of, in other words if we have on ON lights and off OFF lights then after switching them on=N-on, off=N-off. So every time we can just sort them sort them by the number of OFF turned switches and take the minimum one and switch it , this way we will minimize the "loss" and maximize the "profit".

       DOWNLOAD THE FULL SOURCE CODE

Wednesday, February 19, 2014

SPOJ 10471. Aliens at the train, again!

http://www.spoj.com/problems/ALIEN2/

  We will name the given arrays A and B. We will solve this with a dynamic programming (building every step based on the previous one). Let us keep 2 additional arrays , we will call them AA and BB. AA[I] shows the minimum number of people which must be seen in order to reach the station I on train A, similarly the element BB[I] shows the minimum number to be seen to reach to station I on train B. Suppose we have the answers AA[1],BB[1],AA[2],BB[2]....,AA[i-1],BB[i-1]. We need to calculate the AA[i] and BB[i]. There are 2 ways we need to compare to get to AA[i] , we can get there directly from station I-1 in sector A or we can be in the station I-1 in B sector then go to the station I in B sector and then go to the station I in A sector. The same refers to BB[i] but in reversed order. The basic formula is this
BB[i]=min ( AA[i-1]+AA[i]+BB[i] ,  BB[i-1]+BB[i]);
AA[i]=min (BB[i-1]+BB[i]+AA[i] , AA[i-1]+AA[i] );


       DOWNLOAD THE FULL SOURCE CODE

Monday, February 17, 2014

SPOJ 10401. Aliens at the train

http://www.spoj.com/problems/ALIEN/

By looking at the constraints we can assume that our solution must be O(NlogN) a worst. but here is a solution with O(N) complexity. First of all we will keep 3 variable which will show us the current answer.  we will cal them P and Q and S Let us call them ans1 and ans2 (ans1 stands for the number of people seen and ans2 for the number of stations passed). Our variables P,Q and S show the left and right ends of the interval we are now in and the sum of those numbers. So , every time we need to check if S is smaller than Bt (the maximum number) then we increment Q by 1 and S by A[Q]( we want to check that is it possible to take the next element or not). If at some point we came across a situation where our S (the number of seen people) >Bt (meaning that we took an invalid interval) we are increasing P by 1 and decreasing S by A[P] .
Also at every step we update the values of ans1 and ans2. Also, check the source code for a better understanding.  


       DOWNLOAD THE FULL SOURCE CODE

Sunday, February 16, 2014

SPOJ 15454. How to Handle the Fans

The problem statement

This task is a classic task of segment trees . Learn about segment trees and implementation of segment trees here. . Also check the source code for a good understanding.


       DOWNLOAD THE FULL SOURCE CODE

Segment Tree. Finding the sum of a given range in O (logN). Implementation of segment tree

 We have the following task. We are given an array of integers. There are 2 types of operations we need to perform. 1st update x,y. Change the xth element to y. 2. query x-y  find the sum of elements in range [x;y].
Of course the first way which pops up in mind is the simple brute force solution. That can work only if the constraints are low but here we are going to discuss a solution which will work for big constraints (in other words every query will work in O(logN) time not O (N)).
    We will do that with the help of segment trees. Segment tree ( sometimes called interval tree) is a very special way of data representation. Segment tree is a tree which is fully binary.I think It would be better If we discuss it on an example.
Suppose we have this simple example
N=5
1 2 3 4 5
We have 5 elements. Ok . Now have a look at the segment tree of our data set and we will continue discussing.






















I am sorry for the bad picture, I am not good at this sort of stuff but anyway I hope you can understand it.
Here are a few things to notice for the beginning.
1. Every node in this tree shows the required information (in our case it is the sum) of a specific range. The numbers of nodes are written in green, the interval of every node is written in dark brown. For example the node 4 represents the sum of elements between 1 and 2.
2. The leaves are initial elements of the array.
3.  For having a fully binary tree we need to add up some elements which won't affect our answer to the closest power of 2. In our case we have 5 elements and we need to add 3 more elements so that we can have a fully binary tree. I marked the extra added elements with red X. So in our case we need to add 3 0s because when we sum 0 with other numbers the numbers are not changing.
4. If the current node which has the index of I shows the sum of elements in interval X and Y then
    his 2 children will have the index 2*I and 2*I+1 and , the 2*I the node will show the sum of elements between X and (X+Y)/2 and the 2*I+1 the node will show the sum of element between (X+Y)/2+1 and Y. So, basically we are cutting
  Implementation
We will need to make 3 functions.
1. Building the tree.
2. Updating the tree
3. Querying the tree

1. Building tree
let M be the closest element to N which is not smaller than N and it is a power of 2.
Our tree will have 2*M-1 elements. So our initial elements will be from M to 2*M-1 which will be our given elements from 1 to N and also the extra elements we will add to form a number which is a power of 2. For that we will keep a linear array name Tree. Tree I will be one node of a tree which will have 2 children with indexes 2*I and 2*I+1. So tree[I]=tree[2*I]+tree[2*I+1];
  for(i=m;i<=2*m-1;i++)
tree[i]=a[i-m+1]; (Note that the indexing starts with 1 in array A and in array Tree).
For the rest we need to loop back and every element Tree[i]=Tree[2*i]+Tree[2*i+1];
               for(i=m-1;i>=1;i--)
tree[i]=tree[2*i]+tree[2*i+1];
2. Update tree
   Updating is easy. We are getting an index of an array as an updating parameter. We need to find which node represents the given element of an array and update the tree by cutting the index of that node to half until we reach 2. (Note that the children of node I are 2*I and 2*I+1 , both of this number will give the result I when divided on 2).  This can be done recursively.
void update_tree(int x,int y)
{
tree[x]=tree[x]+y;
if(x==1)
return;
update_tree(x/2,y);
}
Also NOTE that when we receive a parameter I which tells us to update the value in A[I] our function must receive a index of a node not an index of array in our case we will give our function the index m+I-1.
3. Querying
  So, this one is a little bit complicated so I will try to be as clear as possible. Our function is going to have 5 parameters.
int query_tree(int node, int x, int y, int qx, int qy)
1. node-  node shows the current node we are observing
2. x,y - those 2 show the current interval which the node is representing
3. qx, qy,  - those 2 represent the interval which sis being queried.
SO, the node which has an interval of x to y has 2 children . The first child has an index of 2*node and the second child has an index of 2*node+1. The first child shows the answer for an interval X and (X+Y)/2 and the second child shows the answer for interval (X+Y)/2+1 and Y. our function is recursive. Now we need to check for 3 things.
If the queried interval and the current interval of current node are the same we need to return the answer on the current node.
If the previous condition is not true we need to check for 2 more things. If the queried interval belongs to the interval of node*2 (in other words qx and qy are between X and (X+Y)/2) then we need to call this function again but with new parameters ( query_tree( node*2 , X, (X+Y)/2, qx , qy)) .
If the queried interval belongs to the second child of the current node (in otehr words if the qx and qy are between (X+Y)/2+1 and Y then we need to call this function again similar to the previous call. ( query_tree( node*2+1, (X+Y)/2+1, Y, qx, qy) .  
If the 2 conditions below are not met that means that the queried interval has 2 parts where the first part belongs to the first child and the second part belongs to the second child. This calls for 2 recursive functions
query_tree(2*node,x,mid,qx,mid)+query_tree(node*2+1,mid+1,y,mid+1,qy); (mid is (X+Y)/2) , as we can see we divided our qx to qy into 2 different queries , teh first one is from qx to mid and the second one is from mid+1 to qy.
 For having a better understanding check this task from spoj.com and the source code for this task can be found in this site in the section "SPOJ tutorials". 

Saturday, February 15, 2014

SPOJ 9921. ABC Path

The problem statement

 This task can be solved easily with BFS. (Here is some more information about BFS). Just add the current position to the queue and every time check for it's neighbors, if there are any which are the next letter on our current position then add that cell to the queue as well. We have a problem with marking here. We can'r mark a cell as visited and forget about it because there might be another path which is being made with teh help of that cell . If we leave everything unmarked and don't use marking at all we will sure get a time limit exceeded error even with optimized input / output. We can just keep the number of times we visited one cell. And every cell has a total of 8 neighbors then we need to keep the number of times we visit the same cell and make sure that it won't exceed 8. See the source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE

Wednesday, February 12, 2014

SPOJ 181. Scuba diver

http://www.spoj.com/problems/SCUBADIV/

We can call this task a "Knapsack probelm with 3 parameters". For solving this we will need a knowledge of "knapsack problem with 2 parameters".
 We use a linear array in "knapsack with 2 parameters" where A[I] shows the maxmum value we can get which has a mass of I. In our case we will keep a matrix where A[I][J] shows the minimum mass of cylinders which have I oxygen and J nytrogen. Have a look at the constraints the limits of oxygen and nytrogen are not high which means that keeping a matrix and looping over it won't be much of a problem. Check the source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE