File tree 2 files changed +69
-0
lines changed
CompetitiveProgramming/HackerEarth/Algorithms/Graphs
2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Our Code Monk recently learnt about Graphs and is very excited!
2
+ #
3
+ # He went over to the Graph-making factory to watch some freshly prepared graphs. Incidentally,
4
+ # one of the workers at the factory was ill today, so Monk decided to step in and do her job.
5
+ #
6
+ # The Monk's Job is to Identify whether the incoming graph is a tree or not. He is given N, the number
7
+ # of vertices in the graph and the degree of each vertex.
8
+ #
9
+ # Find if the graph is a tree or not.
10
+ #
11
+ # Input:
12
+ # First line contains an integer N, the number of vertices.
13
+ # Second line contains N space-separated integers, the degrees of the N vertices.
14
+ #
15
+ # Output:
16
+ # Print "Yes" (without the quotes) if the graph is a tree or "No" (without the quotes) otherwise.
17
+ #
18
+ # Constraints:
19
+ # 1 ≤ N ≤ 100
20
+ # 1 ≤ Degreei ≤ 1000
21
+ #
22
+ # SAMPLE INPUT
23
+ # 3
24
+ # 1 2 1
25
+ #
26
+ # SAMPLE OUTPUT
27
+ # Yes
28
+
29
+ n = int (input ())
30
+ degrees = [int (i ) for i in input ().split ()]
31
+
32
+ # Number of nodes are given thus in a tree number of edges are (n-1) and each edge has two degree
33
+ # thus in tree data structure total degree should be 2*(n-1) and this should be equal to sum of given degree
34
+
35
+ if (2 * (n - 1 ) == sum (degrees )):
36
+ print ('Yes' )
37
+ else :
38
+ print ('No' )
Original file line number Diff line number Diff line change
1
+ # The Monk wants to buy some cities. To buy two cities, he needs to buy the road connecting those two cities.
2
+ # Now, you are given a list of roads, bought by the Monk. You need to tell how many cities did the Monk buy.
3
+ #
4
+ # Input:
5
+ # First line contains an integer T, denoting the number of test cases. The first line of each test case
6
+ # contains an integer E, denoting the number of roads. The next E lines contain two space separated
7
+ # integers X and Y, denoting that there is an road between city X and city Y.
8
+ #
9
+ # Output:
10
+ # For each test case, you need to print the number of cities the Monk bought.
11
+ #
12
+ # Constraint:
13
+ # 1 <= T <= 100
14
+ # 1 <= E <= 1000
15
+ # 1 <= X, Y <= 10000
16
+ #
17
+ # SAMPLE INPUT
18
+ # 1
19
+ # 3
20
+ # 1 2
21
+ # 2 3
22
+ # 1 3
23
+
24
+ for _ in range (int (input ())):
25
+ roads = int (input ())
26
+ lst = set ([])
27
+ for i in range (roads ):
28
+ node1 ,node2 = map (int ,input ().split ())
29
+ lst .add (node1 )
30
+ lst .add (node2 )
31
+ print (len (lst ))
You can’t perform that action at this time.
0 commit comments