Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n = int(input())
- adj = []
- while n:
- adj.append(list(map(int, input().split())))
- n -= 1
- visited = [0] * len(adj)
- res = 'YES'
- count = 0
- for i in range(len(adj)):
- if not visited[i]:
- count += 1
- if count > 1:
- res = 'NO'
- break
- st = [(i, None)]
- visited[i] = 1
- while st:
- cur, parent = st.pop()
- for j in range(len(adj[cur])):
- if adj[cur][j]:
- if not visited[j]:
- st.append((j, cur))
- visited[j] = 1
- elif j != parent:
- res = 'NO'
- break
- if res == 'NO':
- break
- if res == 'NO':
- break
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment