PT07Y - Is it a tree

You are given an unweighted, undirected graph. Write a program to check if it's a tree topology.


The first line of the input file contains two integers N and M --- number of nodes and number of edges in the graph (0 < N <= 10000, 0 <= M <= 20000). Next M lines contain M edges of that graph --- Each line contains a pair (u, v) means there is an edge between node u and node v (1 <= u,v <= N).


Print YES if the given graph is a tree, otherwise print NO.


3 2
1 2
2 3


hide comments
ram: 2015-09-02 08:35:16

Easy ques :)
My first graph problem.
AC in one go. :) :)

tonystark1996: 2015-08-31 15:32:46

Weak Test Cases
4 2
1 2
3 4
Answer is NO
But my code gives YES,still code accepted

coder_shishir: 2015-08-22 23:09:42

awesome problem ..learned a lot !!!

Vivek: 2015-08-18 10:56:19

if Using DFS, Don't use ADJ Matrix.It would result in O(v^2) time.
Go for DFS+ ADJList.
With Matrix,got TLE, With List Accepted.

SangKuan: 2015-08-12 05:24:18

first i used visited array and got many errors,then is use two hash, colored and merge the edge got need
dfs or bfs, but i think union find is the best way

Last edit: 2015-08-12 05:24:51
nadavishe: 2015-08-10 15:48:19

Very week test cases

Saksham : 2015-08-04 01:50:59

no need of graph

harkirat: 2015-07-28 13:52:53

how weak are the test cases?
My solution is completely wrong but got ac!

Anurag Pasi: 2015-07-05 18:13:25

@Tanmay Kulshrestha I partially agree with u, when m==n-1 and still not a tree i.e it is cyclic , but i dont think there will b a totally disconnected node mayb there or mayb not u cant say..
i hope this helps

Tanmay Kulshrestha: 2015-07-03 14:37:39

A confusion:
If m==n-1 and still it's not a tree, it means there is a cycle in the graph and for that to happen, there should be a totally disconnected node, right, because otherwise total edges will increase.
Please help !!

Last edit: 2015-07-03 14:38:18

Added by:Thanh-Vy Hua
Time limit:0.166s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS
Resource:Co-author Amber