Problem:


Problem B: Dominoes Arrangement

[0, 0]
[0, 1]  [1, 1]
[0, 2]  [1, 2]  [2, 2]
[0, 3]  [1, 3]  [2, 3]  [3, 3]
[0, 4]  [1, 4]  [2, 4]  [3, 4]  [4, 4]
[0, 5]  [1, 5]  [2, 5]  [3, 5]  [4, 5]  [5, 5]
[0, 6]  [1, 6]  [2, 6]  [3, 6]  [4, 6]  [5, 6]  [6, 6]

Consider the standard set of 28 western dominoes as shown in the above figure. Given a subset of the standard set dominoes, decide whether this subset can be arranged in a straight row in accordance with the familiar playing rule that touching ends must match. For example, the subset [1, 1], [2, 2], [1, 2] can be arranged in a row (as [1, 1] followed by [1, 2] followed by [2, 2]), while the subset [1, 1], [0, 3], [1, 4] can not be arranged in one row. Note that as in usual dominoes playing any pair [i, j] can also be treated as [j, i].

Your task is to write a program that takes as input any subset of the dominoes and output either yes (if the input subset can be arranged in one row) or no (if the input set can not be arranged in one row).

Input

Input file consists of pairs of lines. The first line of each pair is the number of elements N (1 ≤ N ≤ 18) in the subset, and the second line is the elements of the subset separated by blanks, see the input sample below.

The number of pairs (datasets) is less than 30.

Output

For each pair of lines of the input file, the corresponding output is either Yes or No, based on whether the input subset can be arranged in one line or not.

Sample Input

6
13 23 14 24 15 25
10
00 01 11 02 12 22 03 13 23 33

Output for the Sample Input

Yes
No



Sample input:
6
13 23 14 24 15 25
10
00 01 11 02 12 22 03 13 23 33


Sample output:
Yes
No

Submission status: Wrong Answer

Solution code:
import sys

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]
    digits = []
    for i in xy:
        digits.extend(i)
    digits = list(set(digits))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] * b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = [sum(dominos[i]) for i in range(0, 7)]
            odd = 0
            for i in digits:
                if order[i] % 2 != 0:
                    odd += 1
            if odd <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Wrong Answer

Solution code:
import sys

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]
    digits = []
    for i in xy:
        digits.extend(i)
    digits = list(set(digits))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] * b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = [sum(dominos[i]) for i in range(0, 7)]
            odd = 0
            for i in digits:
                if order[i] % 2 != 0:
                    odd += 1
            if odd <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Wrong Answer

Solution code:

# find_pair
def find_pair(target_val, nums):
    if len(nums) == 0:
        print("YES")
        return

    cand_tup = [n for n in nums if n[0] == target_val or n[1] == target_val]

    if len(cand_tup) == 0:
        print("NO")
        return

    next_tup = cand_tup[0]

    next_val = next_tup[0] if next_tup[1] == target_val else next_tup[1]
    nums.remove(next_tup)
    find_pair(next_val, nums)

while True:
    n = int(input())
    nums = [(int(a), int(b)) for a,b in input().split() ]

    dd = {}

    for t in nums:
        dd[t[0]] = dd[t[0]] + 1 if t[0] in dd else 1
        dd[t[1]] = dd[t[1]] + 1 if t[1] in dd else 1

    odd_num = [k for k, v in dd.items() if v % 2 != 0]


    if not(len(odd_num) == 0 or len(odd_num) == 2):
        print("NO")
        quit()

    start = odd_num[0] if len(odd_num) == 2 else nums[0][0]

    find_pair(start, nums)

Submission status: Wrong Answer

Solution code:
import sys

def checkSorted(List):
    dicts = {}
    for data in List:
        dicts[data % 10] = 0
    for data in List:
        dicts[data % 10] += 1
    if max(dicts,key = dicts.get) != min(dicts,key = dicts.get):
        return "NO"
    return "YES"
        

List = []
for i in sys.stdin:
    List.append(i)

for i in range(1,len(List),2):
    List[i] = List[i].split()
    for j in range(len(List[i])):
        List[i][j] = int(List[i][j])
    print(checkSorted(List[i]))

Submission status: Accepted

Solution code:
import sys

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[2 if [x, y] in xy and x == y else \
                1 if [x, y] in xy else \
                1 if [y, x] in xy else 0 \
                for x in range(0, 7)] for y in range(0, 7)] 
    digits = []
    for i in xy:
        digits.extend(i)
    digits = list(set(digits))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] * b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = [sum(dominos[i]) for i in range(0, 7)]
            odd = 0
            for i in digits:
                if order[i] % 2 != 0:
                    odd += 1
            if odd <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Accepted

Solution code:
class UnionFind():
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)

        if x == y:
            return

        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)


while True:
    try:
        n = int(input())
    except EOFError:
        break
    tree = UnionFind(7)
    nums = [0] * 7
    for dominoes in input().split():
        x, y = divmod(int(dominoes), 10)
        tree.unite(x, y)
        nums[x] += 1
        nums[y] += 1
    if all(tree.same(x, i) for i in range(1, 7) if nums[i]) and \
       sum(1 for x in nums if x % 2) <= 2:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

while True:
    try:
        n = input()
    except:
        break
    A = raw_input().split()
    E = [[] for i in range(7)]
    for s in A:
        u = int(s[0])
        v = int(s[1])
        E[u].append(v)
        E[v].append(u)
    cnt = 0
    for i in range(7):
        if len(E[i]) % 2 == 1:
            cnt += 1
    if cnt > 2:
        print "No"
        continue
    used = [len(E[i]) == 0 for i in range(7)]
    def func(num):
        if used[num]:
            return
        used[num] = True
        for u in E[num]:
            func(u)
    for i in range(7):
        if len(E[i]) > 0:
            func(i)
            break
    if False in used:
        print "No"
    else:
        print "Yes"

Submission status: Accepted

Solution code:
# AOJ 1010: Dominoes Arrangement
# Python3 2018.7.5 bal4u

while True:
	try: n = int(input())
	except: break
	f, g = [0]*7, [0]*7
	p = list(input().split())
	for x in p:
		if x[0] == x[1]: g[int(x[0])] = 1
		else:
			f[int(x[0])] += 1
			f[int(x[1])] += 1
	if n == 1:
		print("Yes")
		continue
	ans = 1
	for i in range(7):
		if g[i] and f[i] == 0:
			ans = 0
			break
	if ans: 
		k = 0
		for i in range(7): 
			if f[i] & 1: k += 1
		ans = (k == 0 or k == 2)
	print("Yes" if ans else "No")

Submission status: Runtime Error

Solution code:
import sys
import numpy as np


def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    #print(xy)
    dominos = [[1 if [x, y] in xy or [y, x] in xy else 0 for x in range(0, 7)] for y in range(0, 7)]  
    #print(dominos)
    return (dominos)

lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
        dominos = np.zeros((7, 7))
    else:
        dominos = subset(line)
        order = np.sum(dominos, axis = 0)
        #print(order)
        order = order % 2
        if np.all(order == 0):
            print("Yes")
        elif len(order[order == 1]) <= 2:
            print("Yes")
        else:
            print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = np.unique(np.sort(reduce(lambda x, y: x + y, xy)))
    return digits, dominos

def is_connected(d, g):
    a = np.array(g)
    z = np.zeros_like(a)
    b = a
    for i in range(1, z.shape[0]):
        z += b
        b = b.dot(a)
    zz = z[d[0]][d]
    return len(zz[zz == 0]) == 0
     
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    #print(xy)
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = np.unique(np.sort(reduce(lambda x, y: x + y, xy)))
    print("digits=", digits)
    return digits, dominos

def is_connected(d, g):
    a = np.array(g)
    z = np.zeros_like(a)
    b = a
    for i in range(1, z.shape[0]):
        z += b
        b = b.dot(a)
    zz = z[d[0]][d]
    print("zz=", zz)
    return len(zz[zz == 0]) == 0
    #return True #(len(z[0][z == 0]) == 0)
     
        
f = open("python\input_1010.txt")
lno = 0
for line in f:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    #print(xy)
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = np.unique(np.sort(reduce(lambda x, y: x + y, xy)))
    print("digits=", digits)
    return digits, dominos

def is_connected(d, g):
    a = np.array(g)
    z = np.zeros_like(a)
    b = a
    for i in range(1, z.shape[0]):
        z += b
        b = b.dot(a)
    zz = z[d[0]][d]
    print("zz=", zz)
    return len(zz[zz == 0]) == 0

lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = np.unique(np.sort(reduce(lambda x, y: x + y, xy)))
    return digits, dominos

def is_connected(d, g):
    a = np.array(g)
    z = np.zeros_like(a)
    b = a
    for i in range(1, z.shape[0]):
        z += b
        b = b.dot(a)
    zz = z[d[0]][d]
    return len(zz[zz == 0]) == 0

lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    #print(xy)
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = list(set(reduce(lambda x, y: x + y, xy)))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] + b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    #print("zz=", zz)
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        #print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            #print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = list(set(reduce(lambda x, y: x + y, xy)))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] + b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        #print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            #print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys
from functools import reduce

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]  
    digits = list(set(reduce(lambda x, y: x + y, xy)))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] + b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(1, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        #print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            #print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]
    digits = []
    for i in xy:
        digits.extend(i)
    digits = list(set(digits))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] * b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        #print("line=", line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            #print("order=", order)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0

Submission status: Runtime Error

Solution code:
import sys

def subset(line):
    l = line.strip().split(" ")
    a = list(map(int, l))
    xy = list(map(lambda x: [x // 10, x % 10], a))
    dominos = [[1 if x != y and ([x, y] in xy or [y, x] in xy) else 0 \
                for x in range(0, 7)] for y in range(0, 7)]
    digits = []
    for i in xy:
        digits.extend(i)
    digits = list(set(digits))
    digits.sort()
    return digits, dominos

def add_matrix(a, b):
    c = [[a[j][i] + b[j][i] for i in range(0, 7)] for j in range(0, 7)]
    return c

def mul_matrix(a, b):
    c = [[0 for i in range(0, 7)] for j in range(0, 7)]
    for j in range(0, 7):
        for i in range(0, 7):
            for k in range(0, 7):
                c[j][i] += a[j][k] * b[k][i]
    return c
                
def is_connected(d, g):
    a = g[:]
    z = [[0 for i in range(0, 7)] for j in range(0, 7)]
    b = a
    for i in range(0, 7):
        z = add_matrix(z, b)
        b = mul_matrix(b, a)
    zeros = 0
    for i in d:
        if z[d[0]][i] == 0:
            zeros += 1
    return zeros == 0
        
lno = 0
for line in sys.stdin:
    lno += 1
    if lno == 1:
        n = int(line)
    else:
        digits, dominos = subset(line)
        if not is_connected(digits, dominos):
            print("No")
        else:
            order = np.sum(dominos, axis = 0)
            order = order % 2
            if np.all(order == 0):
                print("Yes")
            elif len(order[order == 1]) <= 2:
                print("Yes")
            else:
                print("No")
        lno = 0