Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71f7668

Browse files
Added HackerEarth Graph problems
1 parent bc7d334 commit 71f7668

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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')
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /