Problem # 16 : A tree or not a tree

Write a function TREE(M N A()) to find out whether a given graph is a tree or not.

M stands for the number of nodes (points) and N stands for the number of connections in the graph. If there is a connection between node P & Q then cells A(<connection> 1) and A(<connection> 2) are set to P & Q.

Sample Output
M=5 N=5 (5 stations and 5 connections)
[Graphical Representation]
and array A() contains.
 1   2 
 1   3 
 5  2
 3  4
 3  5
TREE(5 5 A()) should return 0 as it is not a tree.


If M=7 N=6 and array A() has values
[Graphical Representation]
 1   2 
 1  3
 3   5 
 3  4 
 6  2 
 7  2
TREE(M N A()) should return -1 since the graph is a tree.

[Graphical Representation]
if M=8 N=5 and A() contains
 1   2 
 1  3
 5  2
 3  4
 6  7
TREE(8 5 A()) should return 0 since it is not a single tree.


Previous Problem
Return to problems at The Vault
(Back to problems at The Vault )
Next Problem

LinkExchange
LinkExchange Member
1