solved nwck using graph structure