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

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)
#define RREP(i,n) RFOR(i,0,n)
#define sz(A) (ll)(A.size())
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())
#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())
#define COU(A,x) (UB(A,x)-LB(A,x))
#define F first
#define S second
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
const ll mod=998244353;
template<const long long int mod=998244353>
struct modint{
  using mint=modint<mod>;
  long long int x;
  modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}
  long long int val(){return x;}
  mint&operator=(const mint&a){x=a.x;return *this;}
  mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}
  mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}
  mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}
  friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}
  friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}
  friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}
  mint operator-()const{return mint(0)-*this;}
  mint pow(long long int n){
    if(!n)return 1;
    mint a=1;
    mint _x=x;
    while(n){
      if(n&1)a*=_x;
      _x=_x*_x;n>>=1;
    }
    return a;
  }
  mint inv(){return pow(mod-2);}
  mint&operator/=(mint&a){return *this*=a.inv();}
  friend mint operator/(const mint&a,mint b){return mint(a)/=b;}
};
using mint=modint<998244353>;
int main(){
    vvb DP(1<<18,vb(36));
    while(1){
        ll N=0;cin>>N;
        if(!N)return 0;
        vi A(N);cin>>A;
        REP(i,1<<N)REP(j,2*N)DP[i][j]=0;
        REP(i,N)DP[1<<i][2*i]=DP[1<<i][2*i+1]=1;
        FOR(i,1,1<<N)REP(j,2*N)if(DP[i][j]){
            if(j%2==0){
                REP(k,N)if((i>>k)%2==0){
                    ll b=A[j/2]%10,c=A[k]/10,d=A[k]%10;
                    if(b==c)DP[i+(1<<k)][2*k]=1;
                    if(b==d)DP[i+(1<<k)][2*k+1]=1;
                }
            }
            else{
                REP(k,N)if((i>>k)%2==0){
                    ll b=A[j/2]/10,c=A[k]/10,d=A[k]%10;
                    if(b==c)DP[i+(1<<k)][2*k]=1;
                    if(b==d)DP[i+(1<<k)][2*k+1]=1;
                }
            }
        }
        bool ans=0;
        REP(i,2*N)if(DP[(1<<N)-1][i])ans=1;
        cout<<(ans?"Yes":"No")<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 21340
final_rank: 17

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

bool solve(int n){
    vector<array<char,2>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i][0] >> v[i][1];
    }
    vector<vector<bool>> dp(2*n,vector<bool>(1<<n));
    queue<pair<int,int>> q;
    for (int i = 0; i < n; ++i) {
        dp[2*i][1<<i] = true;
        dp[2*i+1][1<<i] = true;
        q.push({2*i,1<<i});
        q.push({2*i+1,1<<i});
    }
    while (q.size()){
        auto p = q.front(); q.pop();
        int b = p.first/2, c = p.first%2;
        auto last = v[b][1-c];
        for (int i = 0; i < n; ++i) {
            if((1<<i)&p.second) continue;
            int e = p.second+(1<<i);
            if(last == v[i][0]){
                int d = 2*i;
                if(!dp[d][e]){
                    dp[d][e] = true; q.push({d,e});
                }
            }
            if(last == v[i][1]){
                int d = 2*i+1;
                if(!dp[d][e]){
                    dp[d][e] = true; q.push({d,e});
                }
            }
        }
    }
    for (int i = 0; i < 2*n; ++i) {
        if(dp[i].back()) return true;
    }
    return false;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n){
        if(solve(n)) cout << "Yes" << "\n";
        else cout << "No" << "\n";
    }
}
accuracy: 1
time_ms: 170
memory_kb: 5036
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PI = vector<pair<int, int>>;

bool dfs(VVI& grid, PI& dominos, int n, int r) {
    if (dominos.size() == n) {
        return true;
    } else {
        for (int i = 0; i < 7; ++i) {
            if (grid[r][i] == 1) {
                grid[r][i] = grid[i][r] = 0;
                dominos.emplace_back(r, i);
                if (dfs(grid, dominos, n, i)) {
                    return true;
                }
                grid[r][i] = grid[i][r] = 1;
                dominos.pop_back();
            }
        }
        return false;
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    while (cin >> n) {
        vector<string> v(n);
        for (int i = 0; i < n; ++i) cin >> v[i];
        VVI grid(7, VI(7, 0));
        PI dominos;
        for (int i = 0; i < n; ++i) {
            int x = v[i][0] - '0';
            int y = v[i][1] - '0';
            grid[x][y] = grid[y][x] = 1;
        }
        bool flg = false;
        for (int i = 0; i < 7; ++i) {
            if (dfs(grid, dominos, n, i)) {
                cout << "Yes" << endl;
                flg = true;
                break;
            }
        }
        if (!flg) cout << "No" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 3452
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
struct Card {int l, r;};
const int MAX_N = 30;
int V; bool used[MAX_N]; vector<Card> cards;

int dfs(int v, int num, bool l) {
    used[v] = true;
    auto &card = cards[v];
    int x = l ? card.l : card.r;
    if (++num==V) return true;
    rep(u, cards.size()) {
        auto &card_next = cards[u];
        if (!used[u] && (card_next.l == x || card_next.r == x)) {
            if (dfs(u, num, card_next.l != x)) return true;
        }
    }
    used[v] = false;
    return false;
}

int main()
{
    while (cin >> V) {
        cards.clear();
        rep(i, V) {
            string c; cin >> c;
            cards.push_back({c[0]-'0', c[1]-'0'});
        }
        bool ans = false;
        rep(u, V) {
            fill(used, used+MAX_N, false);
            ans |= dfs(u, 0, true);
        }
        printf("%s\n", ans ? "Yes" : "No");
    }
}
accuracy: 1
time_ms: 900
memory_kb: 3208
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int N;
    while(cin >> N){
        vector<char> l(N), r(N);
        for(int i = 0; i < N; i++) {
            cin >> l[i] >> r[i];
        }
        vector<vector<bool>> dp(10, vector<bool>(1 << N, false));
        for(int i = 0; i < N; i++){
            dp[r[i] - '0'][1 << i] = true;
            dp[l[i] - '0'][1 << i] = true;
        }
        for(int bit = 0; bit < (1 << N); bit++){
            for(int i = 0; i < 10; i++){
                if(!dp[i][bit]) continue;
                for(int j = 0; j < N; j++){
                    if(bit & (1 << j)) continue;
                    if(i == l[j] - '0'){
                        dp[r[j] - '0'][bit ^ (1 << j)] = true;
                    }
                    if(i == r[j] - '0'){
                        dp[l[j] - '0'][bit ^ (1 << j)] = true;
                    }
                }
            }
        }
        bool f = false;
        for(int i = 0; i < 10; i++) {
            if(dp[i][(1 << N) - 1]) f = true;
        }
        if(f) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 80
memory_kb: 3240
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <set>
#include <algorithm>

bool bruteForceSearch(std::vector< std::pair<int, int> > &dominos, std::vector< std::pair<int, int> > &ans);

int main() {
	int n;

	while (std::cin >> n) {
		// receive inputs
		std::vector< std::pair<int, int> > dominos(n);
		for (int i = 0; i < n; i++) {
			int temp;
			std::cin >> temp;
			dominos[i].first = temp / 10;
			dominos[i].second = temp % 10;
		}

		// brute force search
		std::vector< std::pair<int, int> > ans;
		if (bruteForceSearch(dominos, ans)) {
			std::cout << "Yes";
		} else {
			std::cout << "No";
		}
		std::cout << std::endl;
	}

	return 0;
}

std::set< std::vector< std::pair<int, int> > > set;

bool bruteForceSearch(std::vector< std::pair<int, int> > &dominos, std::vector< std::pair<int, int> > &ans) {
	// when you set entire dominos
	if (dominos.size() == 0) {
		for (int i = 0; i < ans.size() - 1; i++) {
			if (ans[i].second != ans[i+1].first) return false;
		}
		return true;
	}

	// search
	// first time
	if (ans.size() == 0) {
		for (int i = 0; i < dominos.size(); i++) {
			// get target domino and remove
			std::pair<int, int> target = dominos[i];
			dominos.erase(dominos.begin() + i);

			// normal order
			std::vector< std::pair<int, int> > ans1(ans);
			ans1.push_back(target);
			if (bruteForceSearch(dominos, ans1)) {
				return true;
			}
			
			// reverse order
			std::vector< std::pair<int, int> > ans2(ans);
			std::swap(target.first, target.second);
			ans2.push_back(target);
			if (bruteForceSearch(dominos, ans2)) {
				return true;
			}

			// revert removed domino
			dominos.insert(dominos.begin() + i, target);
		}

		return false;
	// not first time
	} else {
		int last = (*(ans.end() - 1)).second;

		for (int i = 0; i < dominos.size(); i++) {
			if (dominos[i].first == last) {
				// get target domino and remove
				std::pair<int, int> target = dominos[i];
				dominos.erase(dominos.begin() + i);

				// normal order
				std::vector< std::pair<int, int> > ans1(ans);
				ans1.push_back(target);
				if (bruteForceSearch(dominos, ans1)) {
					return true;
				}

				// revert removed domino
				dominos.insert(dominos.begin() + i, target);
			} else if (dominos[i].second == last) {
				// get target domino and remove
				std::pair<int, int> target = dominos[i];
				dominos.erase(dominos.begin() + i);

				// reverse order
				std::vector< std::pair<int, int> > ans2(ans);
				std::swap(target.first, target.second);
				ans2.push_back(target);
				if (bruteForceSearch(dominos, ans2)) {
					return true;
				}

				// revert removed domino
				dominos.insert(dominos.begin() + i, target);
			}
		}
	}

	return false;
}
accuracy: 1
time_ms: 650
memory_kb: 3040
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

bool search(const int state, const int last, const std::vector<std::string> &dominos, std::vector<std::vector<char>>& memo) {
	if (state == memo[last].size() - 1) return true;
	if (memo[last][state] >= 0) return memo[last][state] == 1;
	memo[last][state] = 0;
	for (auto i = 0; i < dominos.size(); ++i) if ((state & (1 << i)) == 0) {
		if (dominos[i].front() - '0' == last) {
			auto res = search(state | (1 << i), dominos[i].back() - '0', dominos, memo);
			if (res) {
				memo[last][state] = 1;
				return true;
			}
		}
		if (dominos[i].back() - '0' == last) {
			auto res = search(state | (1 << i), dominos[i].front() - '0', dominos, memo);
			if (res) {
				memo[last][state] = 1;
				return true;
			}
		}
	}
	return false;
}

int main() {
	int n;
	while (std::cin >> n) {
		std::vector<std::string> dominos(n); for (auto& domino : dominos) std::cin >> domino;
		std::vector<std::vector<char>> can_make(10, std::vector<char>(1 << n, -1));
		auto can = false;
		for (auto i = 0; i < n; ++i) {
			if (search(1 << i, dominos[i].back() - '0', dominos, can_make)) {
				std::cout << "Yes\n";
				can = true;
				break;
			}
			if (search(1 << i, dominos[i].front() - '0', dominos, can_make)) {
				std::cout << "Yes\n";
				can = true;
				break;
			}
		}
		if (!can) std::cout << "No\n";
	}
}
accuracy: 1
time_ms: 30
memory_kb: 5692
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        vector<string> a(n);
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        bool dp[1<<18][7];
        memset(dp, 0, sizeof(dp));
        for(int i=0; i<7; i++){
            dp[0][i] = true;
        }

        for(int i=0; i<(1<<n); i++){
            for(int j=0; j<7; j++){
                if(!dp[i][j]) continue;
                for(int k=0; k<n; k++){
                    if((i & 1<<k) != 0) continue;
                    if(j == a[k][0]-'0'){
                        dp[i | 1<<k][a[k][1]-'0'] = true;
                    }
                    if(j == a[k][1]-'0'){
                        dp[i | 1<<k][a[k][0]-'0'] = true;
                    }
                }
            }
        }
        bool ok = false;
        for(int i=0; i<7; i++){
            if(dp[(1<<n)-1][i]){
                ok = true;
            }
        }

        if(ok){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 4764
final_rank: 6

code_snippet: #include <vector>
#include <iostream>
#include <utility>
#include <bitset>
using namespace std;

int dp[1 << 18][10];
int main() {
  int N;
  while(cin >> N) {
    vector< pair<int, int> > dominoes(N);
    for(int i=0; i<N; i++) {
      int val; cin >> val;
      int A = val / 10, B = val % 10;
      dominoes[i] = make_pair(A, B);
    }

    fill(dp[0], dp[1 << N], false);
    dp[0][0] = true;
    for(int bit=0; bit<(1<<N); bit++) {
      for(int pre=0; pre<10; pre++) {
        if(dp[bit][pre] == false) continue;
        for(int i=0; i<N; i++) {
          if(bit >> i & 1) continue;

          for(int k=0; k<2; k++) {
            if(bit != 0 and pre != dominoes[i].first) {
              swap(dominoes[i].first, dominoes[i].second);
              continue;
            }

            int nbit = bit | (1 << i);
            int npre = dominoes[i].second;
            dp[nbit][npre] |= dp[bit][pre];
            swap(dominoes[i].first, dominoes[i].second);
          }
        }
      }
    }

    /*
    for(int bit=0; bit<(1<<N); bit++) {
      bool ok = false;
      for(int pre=0; pre<10; pre++) {
        ok |= dp[bit][pre];
      }
      if(ok) {
        cout << bitset<10>(bit).to_string();
        cout << ":";
        for(int pre=0; pre<10; pre++) {
          if(dp[bit][pre]) cout << " " << pre;
        }
        cout << endl;
      }
    }
    */

    bool ans = false;
    for(int i=0; i<10; i++) ans |= dp[(1<<N)-1][i];
    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 13256
final_rank: 14

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld =long double;
const ld eps = 1e-9;

bool solve(bitset<18>bs, vector<vector<int>>&dp,const vector<pair<int,int>>&vs,const int now) {
	if(dp[now][bs.to_ulong()]!=-1)return dp[now][bs.to_ulong()];

	bool ok = false;
	if(bs.count()==vs.size())ok=true;
	else {
		for (int i = 0; i < vs.size(); ++i) {
			if (bs[i]) {

			}
			else {
				if (vs[i].first == now || now == 7) {
					bs[i] = true;
					bool aok = solve(bs, dp, vs, vs[i].second);
					if (aok)ok = true;
					bs[i] = false;
				}
				if (vs[i].second == now || now == 7) {
					bs[i] = true;
					bool aok = solve(bs, dp, vs, vs[i].first);
					if(aok)ok=true;
					bs[i] = false;
				}
			}
		}
	
	}
	return dp[now][bs.to_ulong()]=ok;
}
int main()
{
	int N;
	while (cin >> N) {
		vector<pair<int,int>>vs;
		for (int i = 0; i < N; ++i) {
			string st;cin>>st;
			vs.push_back(make_pair(st[0]-'0',st[1]-'0'));
		}

		vector<vector<int>>dp(8,vector<int>(1<<N,-1));

		bitset<18>bs;
		int ok=solve(bs,dp,vs,7);
		if(ok)cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 12364
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define N 18
using namespace std;

bool dp[(1<<N)][N][2];

int main(){
  
  int n;
  
  while(cin>>n){
    
    string num[N];

    for(int i=0;i<n;i++) cin>>num[i];
    
    memset(dp,0,sizeof(dp));

    for(int i=0;i<n;i++)
      for(int j=0;j<2;j++) dp[(1<<i)][i][j]=true;
    
    for(int i=0;i<(1<<n);i++)
      
      for(int j=0;j<n;j++)
	
	for(int k=0;k<2;k++){
	  
	  if(!dp[i][j][k]) continue;

	  for(int l=0;l<n;l++){

	    if(i&(1<<l)) continue;
	    
	    for(int m=0;m<2;m++)
	      
	      if(num[j][k]==num[l][m]) dp[i|(1<<l)][l][!m]=true;
	    
	  }
	  
	}

    bool ans=false;

    for(int i=0;i<n;i++)
      for(int j=0;j<2;j++) ans|=dp[(1<<n)-1][i][j];

    cout<<(ans?"Yes":"No")<<endl;
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 12340
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
string A[20];

int n;
bool used[1<<18][10][10],mem[1<<18][10][10];
bool dfs(int bit,int l,int r){
  if(bit == (1<<n)-1) return 1;
  if(used[bit][l][r])return mem[bit][l][r];
  used[bit][l][r] = 1;
  
  bool res = 0;
  for(int i=0;i<n;i++){
    if(bit>>i&1) continue;
    int nbit = bit | (1<<i);
    
    if(l == A[i][0]-'0') res |= dfs(nbit,A[i][1]-'0',r);
    if(l == A[i][1]-'0') res |= dfs(nbit,A[i][0]-'0',r);
    if(r == A[i][0]-'0') res |= dfs(nbit,l,A[i][1]-'0');
    if(r == A[i][1]-'0') res |= dfs(nbit,l,A[i][0]-'0');
    if(res) break;
  }
  return mem[bit][l][r] = res; 
}

signed main(){

  while(cin>>n){
    for(int i=0;i<n;i++) cin>>A[i];
    memset(used,0,sizeof(used));
    memset(mem,0,sizeof(mem));
    bool ans = dfs(1,A[0][0]-'0',A[0][1]-'0');
    cout<<(ans? "Yes":"No") <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 54336
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

int n;
bool g[7][7];
int t;

bool dfs(int i){
	rep(j, 7){
		if(!g[i][j]){
			g[i][j] = g[j][i] = true;
			++t;
			bool r = dfs(j);
			g[i][j] = g[j][i] = false;
			--t;
			if(r){
				return true;
			}
		}
	}
	return t == n;
}

int main(){
	while(cin >> n){
		rep(i, 7){
			fill(g[i], g[i] + 7, true);
		}

		rep(i, n){
			int p;
			cin >> p;
			int a = p / 10;
			int b = p % 10;
			g[a][b] = g[b][a] = false;
		}

		bool a = false;
		rep(i, 7){
			if(dfs(i)){
				a = true;
				break;
			}
		}
		cout << (a ? "Yes" : "No") << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 1152
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

struct Domino
{
  int id;
  int op;
  Domino(int _id, int _op) { id = _id; op = _op; }
};

vector<pair<int,int> > Dominos;
vector<Domino> graph[7];
bool visited[28];
int n;

bool dfs(int num, int d)
{
  if (d == n) { return true; }
  if (d > n) return false;
  if (graph[num].empty()) { return false; }
  for (int i=0;i<graph[num].size();i++)
  {
    int nextid = graph[num][i].id;
    int nextop = graph[num][i].op;
    if (!visited[nextid])
    {
      visited[nextid] = true;
      bool res = dfs(nextop, d+1);
      if (res) { return true; }
      else { visited[nextid] = false; }
    }
  }
  return false;
}


int main()
{
  while (cin >> n)
  {
    for (int i=0;i<7;i++) graph[i].clear();
    Dominos.clear();
    bool found = false;

    for (int i=0;i<n;i++)
    {
      int m, x, y;
      cin >> m;
      x = m / 10; y = m % 10;
      Dominos.push_back(make_pair(x, y));

      Domino dx(i, y), dy(i, x);
      if (x == y) graph[x].push_back(dx);
      else
      {
        graph[x].push_back(dx);
        graph[y].push_back(dy);
      }
    }

    for (int id=0;id<n;id++)
    {
      for (int i=0;i<n;i++) visited[i] = false;
      int x = Dominos[id].first;
      int y = Dominos[id].second;
      visited[id] = true;
      if (dfs(x, 1)) { found = true; break; }
      if (x != y) if (dfs(y, 1)) { found = true; break; }
    }
    if (found) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 1192
final_rank: 4

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
  
static const double EPS = 1e-8;
 
static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};
 
class Domino {
private:
  int _head;
  int _tail;
public:
  Domino(int head, int tail) {
    _head = head;
    _tail = tail;
  }
 
  int detect_connect_pattern(Domino d) {
    int res = 0;
    if(d.get_head() == _head){
      return (res | (1<<0));
    }
    if(d.get_head() == _tail){
      return (res | (1<<1));
    }
    if(d.get_tail() == _head){
      return (res | (1<<2));
    }
    if(d.get_tail() == _tail){
      return (res | (1<<3));
    }
    return res;
  }
 
  Domino connect(Domino d,int pattern){
    if(pattern == (1<<0)){
      return Domino(d.get_tail(),_tail);
    }
    if(pattern == (1<<1)){
      return Domino(d.get_tail(),_head);
    }
    if(pattern == (1<<2)){
      return Domino(d.get_head(),_tail);
    }
    if(pattern == (1<<3)){
      return Domino(d.get_head(),_head);
    }
    return *this;
  }
 
  int get_head() const{
    return _head;
  }
 
  int get_tail() const{
    return _tail;
  }
};
 
bool dp[1<<22];
 
bool dfs(Domino& current_state,int visited, const vector<Domino>& dominos){
  if(dp[visited]) return false;
  if(visited == (1<<dominos.size()) - 1){
    return true;
  }
 
  int res = false;
  for(int i = 0; i < dominos.size(); i++){
 
    if(visited & (1<<i)) continue;
    int pattern;
    if((pattern = current_state.detect_connect_pattern(dominos[i])) == 0) continue;
    Domino next = current_state.connect(dominos[i],pattern);
    res |= dfs(next,visited | (1<<i),dominos);
    dp[visited | (1<<i)] = true;
  }
  return res;
}
 
int main(){
  int n;
  while(~scanf("%d",&n)){
    vector<Domino> dominos;
    memset(dp,false,sizeof(dp));
 
    int same_head_tail = 0;
    for(int i = 0; i < n; i++){
      char domino[8];
      scanf("%s",domino);
      if(domino[0] - '0' == domino[1] - '0'){
        same_head_tail |= (1 << (domino[1] - '0'));
        continue;
      }
      dominos.push_back(Domino(domino[0] - '0',domino[1] - '0'));
    }
 
    bool same_check = true;
    for(int num = 0; num <= 6; num++){
      if(same_head_tail & (1 << num)){
        bool isok = false;
        for(int i = 0; i < dominos.size(); i++){
          if(dominos[i].get_tail() == num
             || dominos[i].get_head() == num){
            isok = true;
          }
        }
        if(!isok){
          same_check = false;
          break;
        }
      }
    }
 
    bool travel_check = false;
    travel_check = dfs(dominos[0],(1<<0),dominos);
    printf("%s\n",((same_check && travel_check) || (n == 1)) ? "Yes" : "No");
  }
}
accuracy: 1
time_ms: 10
memory_kb: 5272
final_rank: 1

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
 
static const double EPS = 1e-8;

static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

class Domino {
private:
  int _head;
  int _tail;
public:
  Domino(int head, int tail) {
    _head = head;
    _tail = tail;
  }

  int detect_connect_pattern(Domino d) {
    int res = 0;
    if(d.get_head() == _head){
      return (res | (1<<0));
    }
    if(d.get_head() == _tail){
      return (res | (1<<1));
    }
    if(d.get_tail() == _head){
      return (res | (1<<2));
    }
    if(d.get_tail() == _tail){
      return (res | (1<<3));
    }
    return res;
  }

  Domino connect(Domino d,int pattern){
    if(pattern == (1<<0)){
      return Domino(d.get_tail(),_tail);
    }
    if(pattern == (1<<1)){
      return Domino(d.get_tail(),_head);
    }
    if(pattern == (1<<2)){
      return Domino(d.get_head(),_tail);
    }
    if(pattern == (1<<3)){
      return Domino(d.get_head(),_head);
    }
    return *this;
  }

  int get_head() const{
    return _head;
  }

  int get_tail() const{
    return _tail;
  }
};

bool dp[1<<22];

bool dfs(Domino& current_state,int visited, const vector<Domino>& dominos){
  if(dp[visited]) return false;
  if(visited == (1<<dominos.size()) - 1){
    return true;
  }

  int res = false;
  for(int i = 0; i < dominos.size(); i++){

    if(visited & (1<<i)) continue;
    int pattern;
    if((pattern = current_state.detect_connect_pattern(dominos[i])) == 0) continue;
    Domino next = current_state.connect(dominos[i],pattern);
    res |= dfs(next,visited | (1<<i),dominos);
    dp[visited | (1<<i)] = true;
  }
  return res;
}

int main(){
  int n;
  while(~scanf("%d",&n)){
    vector<Domino> dominos;
    memset(dp,false,sizeof(dp));

    int same_head_tail = 0;
    for(int i = 0; i < n; i++){
      char domino[8];
      scanf("%s",domino);
      if(domino[0] - '0' == domino[1] - '0'){
        same_head_tail |= (1 << (domino[1] - '0'));
        continue;
      }
      dominos.push_back(Domino(domino[0] - '0',domino[1] - '0'));
    }

    bool same_check = true;
    for(int num = 0; num <= 6; num++){
      if(same_head_tail & (1 << num)){
        bool isok = false;
        for(int i = 0; i < dominos.size(); i++){
          if(dominos[i].get_tail() == num
             || dominos[i].get_head() == num){
            isok = true;
          }
        }
        if(!isok){
          same_check = false;
          break;
        }
      }
    }

    bool travel_check = false;
    travel_check = dfs(dominos[0],(1<<0),dominos);
    printf("%s\n",((same_check && travel_check) || (n == 1)) ? "Yes" : "No");
  }
}
accuracy: 1
time_ms: 10
memory_kb: 5272
final_rank: 1

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<bitset>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;
typedef pair<int,int> P;
int n;
int memo[(1<<25)][10];

bool rec(P *G,int used,int prev)
{
  if(__builtin_popcount(used) == n)return true;
  if(__builtin_popcount(used) >= n)return false;
  if(memo[used][prev] != -1)return memo[used][prev];

  rep(i,n)
    {
      if((used>>i)&1)continue;
      if(prev == G[i].first)if(rec(G,used|(1<<i),G[i].second))
			      {
				memo[used|(1<<i)][G[i].second] = true;
				return true;
			      }
      if(prev == G[i].second)if(rec(G,used|(1<<i),G[i].first))
			       {
				 memo[used|(1<<i)][G[i].first] = true;
				 return true;
			       }
    }
  memo[used][prev] = false;
  return false;
}

int main()
{
  while(cin >> n)
    {
      cin.ignore();
      rep(i,(1<<n))rep(j,10)memo[i][j] = -1;
      P G[n];
      rep(i,n)
	{
	  char a,b,c;
	  scanf("%c%c%c",&a,&b,&c);
	  G[i].first = a-'0';
	  G[i].second = b-'0';
	}

      rep(i,n)
	{
	  if(rec(G,(1<<i),G[i].second) || rec(G,(1<<i),G[i].first))
	    {
	      cout << "Yes" << endl;
	      goto F;
	    }
	}
      cout << "No" << endl;
    F:;
    }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 13468
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
typedef pair<int, int> pii;

int main(){
        ios_base::sync_with_stdio(false);
        int N;
        while(cin >> N){
                vector<pii> p(N);
                bool found[10] = { false };
                for(int i = 0; i < N; ++i){
                        int x;
                        cin >> x;
                        p[i] = pii(x / 10, x % 10);
                        found[p[i].first] = found[p[i].second] = true;
                }
                bool answer = false;
                vector<int> dp(1 << N);
                for(int begin = 0; !answer && begin < 10; ++begin){
                        if(!found[begin]){ continue; }
                        fill(dp.begin(), dp.end(), 0);
                        dp[0] |= (1 << begin);
                        for(int i = 0; i < (1 << N); ++i){
                                for(int j = 0; j < 10; ++j){
                                        if(!(dp[i] & (1 << j))){ continue; }
                                        for(int k = 0; k < N; ++k){
                                                if(i & (1 << k)){ continue; }
                                                int m = i | (1 << k);
                                                if(p[k].first == j){ dp[m] |= (1 << p[k].second); }
                                                if(p[k].second == j){ dp[m] |= (1 << p[k].first); }
                                        }
                                }
                        }
                        if(dp[(1 << N) - 1]){ answer = true; }
                }
                cout << (answer ? "Yes" : "No") << endl;
        }
        return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 2300
final_rank: 7

code_snippet: //51
#include<iostream>
#include<set>

#define T(a,b,c) (a*1000+b*10+c)

using namespace std;

int p[28];
int n;
set<int> s;

bool dfs(int pr,int u,int r){
  if(r==0){
    return true;
  }else if(s.count(T(u,r,pr))){
    return false;
  }else{
    for(int i=0;i<n;i++){
      if(!(u&1<<i)){
	for(int j=0;j<2;j++){
	  p[i]=p[i]/10+p[i]%10*10;
	  if(pr==p[i]/10){
	    if(dfs(p[i]%10,u|1<<i,r-1))return true;
	  }
	}
      }
    }
    s.insert(T(u,r,pr));
    return false;
  }
}

int main(){
  while(cin>>n){
    s.clear();
    for(int i=0;i<n;i++){
      cin>>p[i];
    }
    for(int i=0;i<n;i++){
      if(dfs(p[i]%10,1<<i,n-1)||dfs(p[i]/10,1<<i,n-1)){
	cout<<"Yes"<<endl;
	goto next;
      }
    }
    cout<<"No"<<endl;
  next:
    ;
  }
  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 6452
final_rank: 13

code_snippet: #include<stdio.h>
#include<string.h>

const int max=7;
int con[max][max];
int n;
bool allMoveOK;

void saiki(int now,int deep){
if(deep==n){
	allMoveOK=true;
	return;
}
for(int i=0;i<max;i++){
	if(con[now][i]>0){
		con[now][i]--;
		con[i][now]--;
		saiki(i,deep+1);
		if(allMoveOK==true) return;
		con[now][i]++;
		con[i][now]++;
	}
}
}
void setData(){
char in[3];
memset(con,0,sizeof(con));
allMoveOK=false;	
for(int i=0;i<n;i++){
	scanf("%s",in);
	in[0]-='0';
	in[1]-='0';
	con[in[0]][in[1]]++;
	con[in[1]][in[0]]++;
}
for(int i=0;i<max;i++){
	saiki(i,0);
	if(allMoveOK==true)break;
}
printf("%s\n",allMoveOK?"Yes":"No");
}
int main(){
while(scanf("%d",&n)!=EOF){
	setData();
}
}
accuracy: 1
time_ms: 510
memory_kb: 732
final_rank: 15