Problem:
Problem F: Ghost Legs
Problem
縦線が３本のあみだくじが$N$個ある。
どの線からスタートしてもスタートした線で終わるあみだくじを良いあみだくじとする。
あみだくじを１つ以上選んで縦に自由な順序でつなぐことができる。
良いあみだくじを作ることができるのであれば"yes"、そうでなければ"no"を出力せよ。
$i$番目のあみだくじには横線が$w_i$本ある。
$a_{i,j}$はあみだくじ$i$の上から$j$番目の横棒が中央の縦線から左右どちらに伸びているかを表し、
$a_{i,j}$が0ならば左に、1ならば右に伸びていることを表す。
Input
入力は以下の形式で与えられる。
$N$
$w_1$ $a_{1,1}$ $a_{1,2}$ ... $a_{1,w_1}$
$w_2$ $a_{2,1}$ $a_{2,2}$ ... $a_{2,w_2}$
...
$w_N$ $a_{N,1}$ $a_{N,2}$ ... $a_{N,w_N}$
入力はすべて整数で与えられる。
1行目に$N$が与えられる。
続く$N$行に$w_i$と$w_i$個の$a_{i,j}$が空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
$1 \le N \le 50$
$0 \le w_i \le 100$
$a_{i,j}$は0または1
Output
良いあみだくじを作ることができるのであれば"yes"、そうでなければ"no"を出力する。
Sample Input 1
3
2 0 0
1 1
5 1 0 0 1 0
Sample Output 1
yes
Sample Input 2
2
1 1
1 0
Sample Output 2
no

Submitted solutions:

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 operator<(const vector<int>&l, const vector<int>&r) {
	for (int i = 0; i < l.size(); ++i) {
		if(l[i]<r[i])return true;
		else if(l[i]==r[i])continue;
		else return false;
	}
	return false;
}

int main() {
	int N;cin>>N;

	map<vector<int>,int>mp;
	map<int,vector<int>>rev_mp;
	vector<int>v(3);
	iota(v.begin(),v.end(),0);
	{
		for (int i = 0; i < 3; ++i) {
			rotate(v.begin(),v.begin()+1,v.end());
			for (int j = 0; j < 2; ++j) {
				mp[v]=i*2+j;
				rev_mp[i*2+j]=v;
				swap(v[0],v[1]);
			}
		}
	}

	vector<int>nums(6);
	for (int i = 0; i < N; ++i) {
		vector<int>perms(3);
		iota(perms.begin(),perms.end(),0);

		int w;cin>>w;
		for (int j = 0; j < w; ++j) {
			int k;cin>>k;
			swap(perms[k],perms[k+1]);
		}
		nums[mp[perms]]++;
	}

	for (int i = 0; i < 6; ++i) {
		nums[i]=min(3,nums[i]);
	}

	set<pair<int,vector<int>>>memo;
	memo.emplace(mp[vector<int>{0, 1, 2}],nums);

	queue<pair<int,vector<int>>>que;
	que.emplace(mp[vector<int>{0, 1, 2}],nums);

	bool aok=false;
	while (!que.empty()) {
		auto p(que.front());
		que.pop();
		int status_id=p.first;
		vector<int> rest=p.second;

		for (int use_id = 0; use_id < 6; ++use_id) {
			if (rest[use_id]) {
				rest[use_id]--;

				vector<int>now_status(rev_mp[status_id]);
				vector<int>next_status(now_status);
				for (int i = 0; i < 3; ++i) {
					auto next_change(rev_mp[use_id]);

					next_status[next_change[i]]= now_status[i];

				}
				int next_status_id = mp[next_status];
				if(next_status_id==4)aok=true;
				if (memo.find(make_pair(next_status_id, rest)) == memo.end()) {
					memo.emplace(next_status_id, rest);
					que.emplace(next_status_id, rest);
				}

				rest[use_id]++;
			}
		}
	}

	bool ok=memo.find(make_pair(4,vector<int>(6,0)))!=memo.end();
	if(aok)cout<<"yes"<<endl;
	else cout<<"no"<<endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4480
final_rank: 1

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 operator<(const vector<int>&l, const vector<int>&r) {
	for (int i = 0; i < l.size(); ++i) {
		if(l[i]<r[i])return true;
		else if(l[i]==r[i])continue;
		else return false;
	}
	return false;
}

int main() {
	int N;cin>>N;

	map<vector<int>,int>mp;
	map<int,vector<int>>rev_mp;
	vector<int>v(3);
	iota(v.begin(),v.end(),0);
	{
		for (int i = 0; i < 3; ++i) {
			rotate(v.begin(),v.begin()+1,v.end());
			for (int j = 0; j < 2; ++j) {
				mp[v]=i*2+j;
				rev_mp[i*2+j]=v;
				swap(v[0],v[1]);
			}
		}
	}

	vector<int>nums(6);
	for (int i = 0; i < N; ++i) {
		vector<int>perms(3);
		iota(perms.begin(),perms.end(),0);

		int w;cin>>w;
		for (int j = 0; j < w; ++j) {
			int k;cin>>k;
			swap(perms[k],perms[k+1]);
		}
		nums[mp[perms]]++;
	}

	for (int i = 0; i < 6; ++i) {
		nums[i]=min(3,nums[i]);
	}

	set<pair<int,vector<int>>>memo;
	memo.emplace(mp[vector<int>{0, 1, 2}],nums);

	queue<pair<int,vector<int>>>que;
	que.emplace(mp[vector<int>{0, 1, 2}],nums);

	bool aok=false;
	while (!que.empty()) {
		auto p(que.front());
		que.pop();
		int status_id=p.first;
		vector<int> rest=p.second;

		for (int use_id = 0; use_id < 6; ++use_id) {
			if (rest[use_id]) {
				rest[use_id]--;

				vector<int>now_status(rev_mp[status_id]);
				vector<int>next_status(now_status);
				for (int i = 0; i < 3; ++i) {
					auto next_change(rev_mp[use_id]);

					next_status[next_change[i]]= now_status[i];

				}
				int next_status_id = mp[next_status];
				if(next_status_id==0)aok=true;
				if (memo.find(make_pair(next_status_id, rest)) == memo.end()) {
					memo.emplace(next_status_id, rest);
					que.emplace(next_status_id, rest);
				}

				rest[use_id]++;
			}
		}
	}

	bool ok=memo.find(make_pair(4,vector<int>(6,0)))!=memo.end();
	if(aok)cout<<"yes"<<endl;
	else cout<<"no"<<endl;
	return 0;
}
accuracy: 0.3333333333333333
time_ms: 20
memory_kb: 4476
final_rank: 6

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<ll,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------

struct S {
	int a,b,c;
	S() {}
	S(int a,int b,int c):a(a),b(b),c(c) {}
	bool operator<(const S s) const {
		return a<s.a;
	}
};

int n;
int to[50][3];
set<S> st[2];

void dfs(int idx,int a,int b,int c,int limit,bool used) {
	if(idx==limit) {
		//cout<<"idx="<<idx<<"("<<a<<','<<b<<','<<c<<')'<<endl;
		if(used) {
			if(limit==n) st[1].insert(S(a,b,c));
			else st[0].insert(S(a,b,c));
		}
		return;
	}
	dfs(idx+1,a,b,c,limit,used);
	dfs(idx+1,to[idx][a],to[idx][b],to[idx][c],limit,true);
}

int f(S s,int i) {
	if(i==0) return s.a;
	else if(i==1) return s.b;
	else return s.c;
}

S conv(S s,S t) {
	S res;
	res.a=f(s,t.a),res.b=f(s,t.b),res.c=f(s,t.c);
	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	REP(i,50) REP(j,3) to[i][j]=j;

	cin>>n;
	REP(i,n) {
		int w,a;cin>>w;
		REP(j,w) {
			cin>>a;
			if(a==0) swap(to[i][0],to[i][1]);
			else swap(to[i][1],to[i][2]);
		}
	}
	dfs(0,0,1,2,n/2,false);dfs(n/2,0,1,2,n,false);
	for(auto s:st[0]) {
		//cout<<s.a<<' '<<s.b<<' '<<s.c<<endl;
		if(s.a==0&&s.b==1&&s.c==2) {
			cout<<"yes"<<endl;
			return 0;
		}
	}
	//cout<<"after st1"<<endl;
	for(auto t:st[1]) {
		//cout<<t.a<<' '<<t.b<<' '<<t.c<<endl;
		if(t.a==0&&t.b==1&&t.c==2) {
			cout<<"yes"<<endl;
			return 0;
		}
	}
	//cout<<"after st2"<<endl;
	for(auto s:st[0]) {
		for(auto t:st[1]) {
			S tmp=conv(s,t);
			if(tmp.a==0&&tmp.b==1&&tmp.c==2) {
				cout<<"yes"<<endl;
				return 0;
			}
		}
	}
	cout<<"no"<<endl;

	return 0;
}
accuracy: 0.45454545454545453
time_ms: 50
memory_kb: 3156
final_rank: 3

code_snippet: #include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
using ll = long long;
int dd[] = { 0, 1, 0, -1, 0 }; //→↓←↑

void solve()
{
	int N;
	cin >> N;
	vector<int>W(N);
	//using T = tuple<int, int, int>;
	vector<vector<int>> V(N);

	for (int i = 0; i < N; i++)
	{
		cin >> W[i];
		vector<int> A(W[i]);
		for (int j = 0; j < W[i]; j++)
		{
			cin >> A[j];
		}
		V[i].push_back(0);
		V[i].push_back(1);
		V[i].push_back(2);
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < W[i]; k++)
			{
				if (A[k] == 0)
				{
					if (V[i][j] == 1)V[i][j] = 0;
					else if (V[i][j] == 0)V[i][j] = 1;
				}
				else
				{
					if (V[i][j] == 1)V[i][j] = 2;
					else if (V[i][j] == 2) V[i][j] = 1;
				}
				//cout << A[k] << " " << V[i][j]  << endl;
			}
		}
		//cout << V[i][0] << " " << V[i][1] << " " << V[i][2] << endl;
	}

	int n2 = N / 2;
	vector<int> tmp(3);
	vector<vector<int>> P(1 << n2);
	bool B[3][3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				B[i][j][k] = false;
			}
		}
	}
	for (int i = 1; i < 1 << n2; i++)
	{
		P[i].push_back(0);
		P[i].push_back(1);
		P[i].push_back(2);
		for (int j = 0; j < n2; j++)
		{
			if (i >> j & 1) //jを使う
			{
				tmp[0] = P[i][0];
				tmp[1] = P[i][1];
				tmp[2] = P[i][2];
				P[i][0] = tmp[V[j][0]];
				P[i][1] = tmp[V[j][1]];
				P[i][2] = tmp[V[j][2]];
			}
		}
		if (P[i][0] == 0 && P[i][1] == 1 && P[i][2] == 2)
		{
			cout << "yes" << endl;
			return;
		}
		B[P[i][0]][P[i][1]][P[i][2]] = true;
	}


	for (int i = 1; i < 1 << (N - n2); i++)
	{
		vector<int> Q;
		Q.push_back(0);
		Q.push_back(1);
		Q.push_back(2);

		for (int j = 0; j < N - n2; j++)
		{
			if (i >> j & 1)
			{
				tmp[0] = Q[0];
				tmp[1] = Q[1];
				tmp[2] = Q[2];
				//cout << Q[0] << " " << Q[1] << " " << Q[2] << endl;
				Q[0] = tmp[V[j + n2][0]];
				Q[1] = tmp[V[j + n2][1]];
				Q[2] = tmp[V[j + n2][2]];
				//cout << Q[0] << " " << Q[1] << " " << Q[2] << endl;
			}
		}

		if (Q[0] == 0 && Q[1] == 1 && Q[2] == 2)
		{
			cout << "yes" << endl;
			return;
		}
		//cout << Q[0] << " " << Q[1] << " " << Q[2] << endl;
		int zero, one, two;
		for (int k = 0; k < 3; k++)
		{
			if (Q[k] == 0)
			{
				zero = k;
			}
			if (Q[k] == 1)
			{
				one = k;
			}
			if (Q[k] == 2)
			{
				two = k;
			}
		}
		if (B[zero][one][two])
		{
			cout << "yes" << endl;
			return;
		}
	}

	cout << "no" << endl;



}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 395992
final_rank: 2

code_snippet: #include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
using ll = long long;
int dd[] = { 0, 1, 0, -1, 0 }; //→↓←↑

void solve()
{
	int N;
	cin >> N;
	vector<int>W(N);
	//using T = tuple<int, int, int>;
	vector<vector<int>> V(N);

	for (int i = 0; i < N; i++)
	{
		cin >> W[i];
		vector<int> A(W[i]);
		for (int j = 0; j < W[i]; j++)
		{
			cin >> A[j];
		}
		V[i].push_back(0);
		V[i].push_back(1);
		V[i].push_back(2);
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < W[i]; k++)
			{
				if (A[k] == 0)
				{
					if (V[i][j] == 1)V[i][j] = 0;
					else if (V[i][j] == 0)V[i][j] = 1;
				}
				else
				{
					if (V[i][j] == 1)V[i][j] = 2;
					else if (V[i][j] == 2) V[i][j] = 1;
				}
				//cout << A[k] << " " << V[i][j]  << endl;
			}
		}
		//cout << V[i][0] << " " << V[i][1] << " " << V[i][2] << endl;
	}

	int n2 = N / 2;
	vector<int> tmp(3);
	vector<vector<int>> P(1 << n2);
	bool B[3][3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 3; k++)
			{
				B[i][j][k] = false;
			}
		}
	}
	for (int i = 1; i < 1 << n2; i++)
	{
		P[i].push_back(0);
		P[i].push_back(1);
		P[i].push_back(2);
		for (int j = 0; j < n2; j++)
		{
			if (i >> j & 1) //jを使う
			{
				tmp[0] = P[i][0];
				tmp[1] = P[i][1];
				tmp[2] = P[i][2];
				P[i][0] = tmp[V[j][0]];
				P[i][1] = tmp[V[j][1]];
				P[i][2] = tmp[V[j][2]];
			}
		}
		if (P[i][0] == 0 && P[i][1] == 1 && P[i][2] == 2)
		{
			cout << "yes" << endl;
			return;
		}
		B[P[i][0]][P[i][1]][P[i][2]] = true;
	}


	for (int i = 0; i < 1 << (N - n2); i++)
	{
		vector<int> Q(3);
		Q.push_back(0);
		Q.push_back(1);
		Q.push_back(2);
		for (int j = 1; j < N - n2; j++)
		{
			if (i >> j & 1)
			{
				if (i >> j & 1) //j+n2を使う
				{
					tmp[0] = Q[0];
					tmp[1] = Q[1];
					tmp[2] = Q[2];
					Q[0] = tmp[V[j + n2][0]];
					Q[1] = tmp[V[j + n2][1]];
					Q[2] = tmp[V[j + n2][2]];
				}

			}
			if (Q[0] == 0 && Q[1] == 1 && Q[2] == 2)
			{
				cout << "yes" << endl;
				return;
			}
			//cout << Q[0] << " "  << Q[1] << " " << Q[2] << endl;
			int zero, one, two;
			for (int k = 0; k < 3; k++)
			{
				if (Q[k] == 0)
				{
					zero = k;
				}
				if (Q[k] == 1)
				{
					one = k;
				}
				if (Q[k] == 2)
				{
					two = k;
				}
			}
			if (B[zero][one][two])
			{
				cout << "yes" << endl;
				return;
			}
		}
	}

	cout << "no" << endl;



}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	solve();
	return 0;
}
accuracy: 0.42424242424242425
time_ms: 10
memory_kb: 51748
final_rank: 4

code_snippet: #include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;

int numbers[3] = {0b001, 0b010, 0b100};

vector<vector<int>> s;
vector<vector<int>> t;

set<int> st;

int main() {
  cin >> n;
  vector<int> base = {0, 1, 2};
  int sz = n / 2;
  s.assign(sz, base);
  t.assign(n - (sz) + 1, base);
  rep(i, 0, sz - 1) {
    int w;
    cin >> w;
    rep(j, 0, w - 1) {
      int a;
      cin >> a;
      if (a == 0) {
        swap(s[i][0], s[i][1]);
      } else {
        swap(s[i][1], s[i][2]);
      }
    }
  }
  rep(i, 0, n - (sz)-1) {
    int w;
    cin >> w;
    rep(j, 0, w - 1) {
      int a;
      cin >> a;
      if (a == 0) {
        swap(t[i][0], t[i][1]);
      } else {
        swap(t[i][1], t[i][2]);
      }
    }
  }
  {
    sz = s.size();
    vector<int> dp((1 << sz), 0);
    for (int i = 0; i < 3; i++) {
      dp[0] |= (numbers[i] << (i * 3));
    }
    for (int i = 1; i < (1 << sz); i++) {
      int j;
      for (j = 0; j < sz; j++) {
        if (i & (1 << j)) break;
      }
      int next = 0;
      int before = i & ~(1 << j);
      for (int from = 0; from < 3; from++) {
        int to = s[j][from];
        for (int k = 0; k < 3; k++) {
          int bit = 0;
          if (dp[before] & (1 << (from * 3 + k))) {
            bit = 1;
          }
          next |= (bit << (to * 3 + k));
        }
      }
      dp[i] = next;
      st.insert(next);
    }
  }
  {
    sz = t.size();
    vector<int> dp((1 << sz), 0);
    for (int i = 0; i < 3; i++) {
      dp[0] |= (numbers[i] << (i * 3));
    }
    for (int i = 1; i < (1 << sz); i++) {
      int j;
      for (j = 0; j < sz; j++) {
        if (i & (1 << j)) break;
      }
      int next = 0;
      int before = i & ~(1 << j);
      for (int from = 0; from < 3; from++) {
        int to = t[j][from];
        for (int k = 0; k < 3; k++) {
          int bit = 0;
          if (dp[before] & (1 << (from * 3 + k))) {
            bit = 1;
          }
          next |= (bit << (to * 3 + k));
        }
      }
      dp[i] = next;
      if ((i != (1 << (sz - 1)) && dp[0] == next) || st.count(next)) {
        cout << "yes" << endl;
        return 0;
      }
    }
  }
  cout << "no" << endl;
}
accuracy: 0.42424242424242425
time_ms: 30
memory_kb: 35652
final_rank: 5