Thursday, April 1, 2021

The Joy of Computing using Python Week 10 Programming Assignment 2 Solution

 

Week 10 Programming Assignment 2

Due on 2021-04-01, 23:59 IST

Consider a directed graph. It can be represented by an adjacency matrix. The nodes are numbered 1 to n. If there is an edge from node i to node j, there will be a 1 in the (i-1,j-1) position in the adjacency matrix. There are no self loops in the graph. For a node, the number of head ends adjacent to a node is called the indegree of the node and the number of tail ends adjacent to a node is its outdegree. Print 'yes'  and the node number if  in the given graph there is a node with outdegree 2, else print 'no'. Adjacency matrix is given as a line of zeros and ones in row major order i.e, row 1 will be given as input first then row 2 so on. There will be only one edge with outdegree 2.


Input format :

Line 1 - Number of nodes

Line 2 - Adjacency matrix in row major order


Output format : if the condition is satisfied then yes node_number else no


Example 

input:

4 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0

output

yes 2


Your last recorded submission was on 2021-04-01, 08:11 IST
Select the Language for this assignment. 
1
N,n,f,Ans=int(input()),input().split(),0,[]
2
ans=[n[i-N:i]for i in range(1,len(n)+1)if i%N==0]
3
for k in range(N):
4
  p=[]
5
  for j in range(N):
6
    p.append(ans[j][k])
7
  Ans.append(p)
8
for a in range(N):
9
  if Ans[a].count('1')==2:
10
    f=a+1   
11
if f!=0:
12
  print("yes",f,end="")
13
else:
14
  print("no",end="")

No comments:

Post a Comment