Problem:
博士の研究室
会津大学の鶴賀博士はとても研究熱心なことで有名です。彼の研究室には複数の学生がいますが、彼は毎日夜遅くまで研究をするので必ず最後に帰宅します。彼の研究室にはドアでつながった複数の部屋があり、最後に研究室を出る人がすべての部屋の明かりを消して帰宅することになっています。
最近、大学では省エネに力を入れているため、すべての明かりが消えているか厳しくチェックされます。困ったことに彼は誰もが認める極度の臆病者で、明かりの消えた部屋には決して入ることができません。そこで、彼はある部屋の照明のON/OFFを他の部屋から操作できるように研究室の照明設備を改造しました。
ところが、予算の都合で１つの部屋からコントロールできる部屋が限られています。その上、帰宅時の各部屋の明かりの状態も日によって異なるため、全ての明かりを消して出口までたどりつくのに毎日かなりの時間が掛かっています。そこで、研究員のあなたが博士を助けるためにプログラムを作成することになりました。
研究室の部屋情報、各部屋の明かりの点灯情報、各部屋の照明スイッチの情報を入力とし、博士がすべての明かりを消して帰宅できるかどうかを出力するプログラムを作成してください。ただし、部屋の数
n
は 1 以上 15 以下の整数、ドアの数
m
は 1 以上 30 以下の整数、照明の点灯情報
l
は 0 または 1 の整数でそれぞれ消灯と点灯を表し、各部屋には 1 以上
n
以下の整数で番号が付与されているものとします。出口は番号
n
の部屋に存在し、博士は常に部屋 1 からスタートするものとします。
なお、出力は、博士の取るべき行動に応じて以下の 3 通りに分けて出力します。
ケース１
.
出口以外の全ての明かりを消して出口にたどりつける場合（経路の過程で出口を通ってもよい）。
You can go home in X steps.
と出力します。ここで X は部屋の移動、スイッチのON/OFF をそれぞれ１ステップとした場合の、博士の部屋から出口にたどり着くまでの最短のステップ数です。さらに、以下の文字列に従い博士の部屋から出口までの経路（博士のとるべき行動）を X 行で出力します。
部屋 R へ移動する場合
Move to room R.
部屋 R の照明を消す場合
Switch off room R.
部屋 R の照明を点灯する場合
Switch on room R.
ここで、R は部屋の番号を表します。博士がある部屋に移動した直後、複数のスイッチを操作して次の部屋に移動する場合は操作する部屋番号が小さいほうから出力してください。この条件を満たす限り、複数の解がある場合は、どれを出力してもかまいません。この情報をもとに、博士は無事帰宅することができます。
ケース2
. 出口にたどり着くことはできるが、出口のある部屋以外の全ての明かりを消すことが
できない場合。
You can not switch off all lights.
と出力する。この場合、博士は省エネを守らなかったとして大学に罰せられます。
ケース3
. どうあがいても出口にたどり着けない場合。
Help me!
と出力する。この場合、博士は警備員に緊急救助を求めます。
簡単な例を示します。この例では、研究室は 4 つの部屋で構成され、部屋 1 と 2、2 と 3、2 と 4 がそれぞれ繋がっています。また、部屋 1 及び 4 から部屋 2、3 の照明を操作することができ、部屋 3 から部屋 1、2、4 の照明を操作することができます。最初、部屋 2、3、4 の照明が消えた状態で、博士が部屋 1 にいます。
この状況では、博士が取るべき行動は次のようになります。
部屋2と3の照明をonにする。
部屋2に移動する。
部屋3に移動する。
部屋1の照明をoffにし、部屋4の照明をonにする。
部屋2に移動する。
部屋4に移動する。
部屋2と3の照明をoffにする。
これで博士は部屋 4 以外の照明を消すことができ、帰宅することができます。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロ2つの行で示されます。
各データセットは以下の形式で与えられます。
n
m
s
1
t
1
s
2
t
2
:
s
m
t
m
l
1
l
2
...
l
n
k
1
r
1
r
2
...
r
k
1
k
2
r
1
r
2
...
r
k
2
:
k
n
r
1
r
2
...
r
k
n
１行目に部屋の数
n
(1 ≤
n
≤ 15) とドアの数
m
(1 ≤
m
≤ 30) が空白区切りで与えられます。続く
m
行に
i
個目のドアの情報が与えられます。
s
i
t
i
は部屋
s
i
と部屋
t
i
がドアで繋がっていることを表します。
続く１行に、部屋
i
の照明の点灯情報
l
i
(0 または 1) が空白区切りで与えられます。
続く
n
行に部屋
i
のスイッチ情報が与えられます。
k
i
(0 ≤
k
i
≤ 12) は部屋
i
のスイッチの数、
r
1
...
r
k
i
は照明を操作できる部屋の番号を表します。
データセットの数は100 を超えません。
Output
データセットごとに、上記の 3 通りの結果に応じて以下の形式で出力します。
ケース 1 の場合
1行目: You can go home in X steps.
2行目: 1つ目の博士が取るべき行動
3行目: 2つ目の博士が取るべき行動
:
X+1 行目: X 個目の博士が取るべき行動
ケース 2 の場合
1 行目: You can not switch off all lights.
ケース 3 の場合
1 行目: Help me!
Sample Input
4 3
1 2
2 3
2 4
1 0 0 0
2 2 3
0
3 1 2 4
2 2 3
4 3
1 2
2 3
2 4
1 0 0 0
2 2 3
0
3 1 2 4
1 3
4 3
1 2
2 3
2 4
1 0 0 0
2 2 3
0
2 1 2
2 2 3
0 0
Output for the Sample Input
You can go home in 10 steps.
Switch on room 2.
Switch on room 3.
Move to room 2.
Move to room 3.
Switch off room 1.
Switch on room 4.
Move to room 2.
Move to room 4.
Switch off room 2.
Switch off room 3.
You can not switch off all lights.
Help me!

Submitted solutions:

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int N, M;
	while (cin >> N >> M, N) {
		vector<vector<int> > G(N);
		for (int i = 0; i < M; ++i) {
			int a, b;
			cin >> a >> b; --a, --b;
			G[a].push_back(b);
			G[b].push_back(a);
		}
		int inibit = 0;
		for (int i = 0; i < N; ++i) {
			int x;
			cin >> x;
			inibit |= x << i;
		}
		vector<int> op(N);
		for (int i = 0; i < N; ++i) {
			int c;
			cin >> c;
			for (int j = 0; j < c; ++j) {
				int x;
				cin >> x; --x;
				if (x != i) op[i] |= 1 << x;
			}
		}
		vector<vector<int> > dist(1 << N, vector<int>(N, -1));
		vector<vector<pair<int, int> > > pre(1 << N, vector<pair<int, int> >(N));
		queue<pair<int, int> > que;
		if (inibit & 1) {
			dist[inibit][0] = 0;
			que.push(make_pair(inibit, 0));
		}
		while (!que.empty()) {
			pair<int, int> u = que.front(); que.pop();
			int bit = u.first, pos = u.second;
			for (int i : G[pos]) {
				if (((bit >> i) & 1) && dist[bit][i] == -1) {
					dist[bit][i] = dist[bit][pos] + 1;
					pre[bit][i] = make_pair(bit, pos);
					que.push(make_pair(bit, i));
				}
			}
			for (int i = 0; i < N; ++i) {
				if (((op[pos] >> i) & 1) && dist[bit ^ (1 << i)][pos] == -1) {
					dist[bit ^ (1 << i)][pos] = dist[bit][pos] + 1;
					pre[bit ^ (1 << i)][pos] = make_pair(bit, pos);
					que.push(make_pair(bit ^ (1 << i), pos));
				}
			}
		}
		if (dist[1 << (N - 1)][N - 1] == -1) {
			bool escape = false;
			for (int i = 0; i < 1 << N; ++i) {
				if (dist[i][N - 1] != -1) {
					escape = true;
					break;
				}
			}
			cout << (escape ? "You can not switch off all lights." : "Help me!") << endl;
		}
		else {
			cout << "You can go home in " << dist[1 << (N - 1)][N - 1] << " steps." << endl;
			pair<int, int> cur = make_pair(1 << (N - 1), N - 1);
			vector<pair<int, int> > path = { cur };
			while (cur != make_pair(inibit, 0)) {
				cur = pre[cur.first][cur.second];
				path.push_back(cur);
			}
			reverse(path.begin(), path.end());
			vector<pair<string, int> > ans;
			for (int i = 1; i < path.size(); ++i) {
				if (path[i - 1].second != path[i].second) {
					ans.push_back(make_pair("move", path[i].second));
				}
				else {
					int diff = -1;
					for (int j = 0; j < N; ++j) {
						if (((path[i - 1].first >> j) & 1) != ((path[i].first >> j) & 1)) {
							diff = j;
							break;
						}
					}
					if ((path[i - 1].first >> diff) & 1) {
						ans.push_back(make_pair("off", diff));
					}
					else {
						ans.push_back(make_pair("on", diff));
					}
				}
			}
			for (int i = 0; i < ans.size(); ++i) {
				for (int j = 1; j < ans.size(); ++j) {
					if (ans[j - 1].first != "move" && ans[j].first != "move" && ans[j - 1].second > ans[j].second) {
						swap(ans[j - 1], ans[j]);
					}
				}
			}
			for (pair<string, int> i : ans) {
				if (i.first == "move") {
					cout << "Move to room " << i.second + 1 << "." << endl;
				}
				else if (i.first == "off") {
					cout << "Switch off room " << i.second + 1 << "." << endl;
				}
				else {
					cout << "Switch on room " << i.second + 1 << "." << endl;
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 10980
final_rank: 6

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<int>G[15];
		vector<int>L[15];
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=start.L=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].rbegin(),L[i].rend());
		}
		P.push(start);
		A[start]=0;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
					int tmp=now.now;
					now.now=v;
					if(A.find(now)==A.end())
					{
						P.push(now);
						A[now]=turn+1;
					}
					now.now=tmp;
				}
			}
		}
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 40
memory_kb: 5908
final_rank: 7

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<int>G[15];
		vector<int>L[15];
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=start.L=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].rbegin(),L[i].rend());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
					int tmp=now.now;
					now.now=v;
					if(A.find(now)==A.end())
					{
						P.push(now);
						A[now]=turn+1;
					}
					now.now=tmp;
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 40
memory_kb: 5980
final_rank: 8

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<int>G[15];
		vector<int>L[15];
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=start.L=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].begin(),L[i].end());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
					int tmp=now.now;
					now.now=v;
					if(A.find(now)==A.end())
					{
						P.push(now);
						A[now]=turn+1;
					}
					now.now=tmp;
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			int tmpX=X;
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			X=tmpX;
			for(int i=1;i<X;i++)
			{
				for(int j=i-1;j>=0;j--)
				{
					if(ans[j][0]=='S'&&ans[j+1][0]=='S')
					{
						bool flag=false;
						int id=0,scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j][id]==' ';
							id++;
						}
						int A,B;
						A=ans[j][id]-'0';
						if(ans[j][id+1]!='.')A=A*10+ans[j][id+1]-'0';
						id=scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j+1][id]==' ';
							id++;
						}
						B=ans[j+1][id]-'0';
						if(ans[j+1][id+1]!='.')B=B*10+ans[j+1][id+1]-'0';
						if(A>B)swap(ans[j],ans[j+1]);
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 5868
final_rank: 13

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	state(){now=L=0;}
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
	bool operator==(const state&a)const{
		return !(*this<a||a<*this);
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<int>G[15];
		vector<int>L[15];
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].begin(),L[i].end());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
					int tmp=now.now;
					now.now=v;
					if(A.find(now)==A.end())
					{
						P.push(now);
						A[now]=turn+1;
					}
					now.now=tmp;
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			int tmpX=X;
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			if(!(goal==start))return 1;
			X=tmpX;
			for(int i=1;i<X;i++)
			{
				for(int j=i-1;j>=0;j--)
				{
					if(ans[j][0]=='S'&&ans[j+1][0]=='S')
					{
						bool flag=false;
						int id=0,scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j][id]==' ';
							id++;
						}
						int A,B;
						A=ans[j][id]-'0';
						if(ans[j][id+1]!='.')A=A*10+ans[j][id+1]-'0';
						id=scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j+1][id]==' ';
							id++;
						}
						B=ans[j+1][id]-'0';
						if(ans[j+1][id+1]!='.')B=B*10+ans[j+1][id+1]-'0';
						if(A>B)swap(ans[j],ans[j+1]);
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 5932
final_rank: 15

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	state(){now=L=0;}
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
	bool operator==(const state&a)const{
		return !(*this<a||a<*this);
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<int>G[15];
		vector<int>L[15];
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].begin(),L[i].end());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
				    state X=now;
				    X.now=v;
					if(X.L>>X.now&1&&A.find(X)==A.end())
					{
						P.push(X);
						A[X]=turn+1;
					}
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			int tmpX=X;
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			if(!(goal==start))return 1;
			X=tmpX;
			for(int i=1;i<X;i++)
			{
				for(int j=i-1;j>=0;j--)
				{
					if(ans[j][0]=='S'&&ans[j+1][0]=='S')
					{
						bool flag=false;
						int id=0,scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j][id]==' ';
							id++;
						}
						int A,B;
						A=ans[j][id]-'0';
						if(ans[j][id+1]!='.')A=A*10+ans[j][id+1]-'0';
						id=scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j+1][id]==' ';
							id++;
						}
						B=ans[j+1][id]-'0';
						if(ans[j+1][id+1]!='.')B=B*10+ans[j+1][id+1]-'0';
						if(A>B)swap(ans[j],ans[j+1]);
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 5904
final_rank: 14

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	state(){now=L=0;}
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
	bool operator==(const state&a)const{
		return !(*this<a||a<*this);
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<vector<int> >G(N),L(N);
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].begin(),L[i].end());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			if(!(now.L>>now.now&1))return turn;
			if(now==goal)break;
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
				    state X=now;
				    X.now=v;
					if(X.L>>X.now&1&&A.find(X)==A.end())
					{
						P.push(X);
						A[X]=turn+1;
					}
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			int tmpX=X;
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			if(!(goal==start))return 1;
			X=tmpX;
			for(int i=1;i<X;i++)
			{
				for(int j=i-1;j>=0;j--)
				{
					if(ans[j][0]=='S'&&ans[j+1][0]=='S')
					{
						bool flag=false;
						int id=0,scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j][id]==' ';
							id++;
						}
						int A,B;
						A=ans[j][id]-'0';
						if(ans[j][id+1]!='.')A=A*10+ans[j][id+1]-'0';
						id=scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j+1][id]==' ';
							id++;
						}
						B=ans[j+1][id]-'0';
						if(ans[j+1][id+1]!='.')B=B*10+ans[j+1][id+1]-'0';
						if(A>B)swap(ans[j],ans[j+1]);
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 5796
final_rank: 12

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int N,M;
struct state{
	int now,L;
	state(){now=L=0;}
	bool operator<(const state&a)const{
		return L*15+now<a.L*15+a.now;
	}
	bool operator==(const state&a)const{
		return !(*this<a||a<*this);
	}
};
main()
{
	while(cin>>N>>M,N)
	{
		map<state,int>A;
		vector<vector<int> >G(N),L(N);
		for(int i=0;i<M;i++)
		{
			int u,v;cin>>u>>v;
			u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		queue<state>P;
		state start;
		start.now=0;
		for(int i=0;i<N;i++)
		{
			int l;cin>>l;
			start.L|=l<<i;
		}
		for(int i=0;i<N;i++)
		{
			int k;cin>>k;
			for(;k--;)
			{
				int id;cin>>id;id--;
				L[i].push_back(id);
			}
			sort(L[i].begin(),L[i].end());
		}
		P.push(start);
		A[start]=0;
		state goal;
		goal.now=N-1;
		goal.L=1<<N-1;
		while(!P.empty())
		{
			state now=P.front();P.pop();
			int turn=A[now];
			if(!(now.L>>now.now&1))return turn;
			if(now==goal)break;
			for(int id:L[now.now])
			{
			    if(id==now.now)continue;
				now.L^=1<<id;
				if(A.find(now)==A.end())
				{
					P.push(now);
					A[now]=turn+1;
				}
				now.L^=1<<id;
			}
			for(int v:G[now.now])
			{
				if(now.L>>v&1)
				{
				    state X=now;
				    X.now=v;
					if(X.L>>X.now&1&&A.find(X)==A.end())
					{
						P.push(X);
						A[X]=turn+1;
					}
				}
			}
		}
		if(A.find(goal)!=A.end())
		{
			int X=A[goal];
			cout<<"You can go home in "<<X<<" steps."<<endl;
			vector<string>ans(X);
			int tmpX=X;
			for(;X--;)
			{
				for(int id:L[goal.now])
				{
					goal.L^=1<<id;
					if(A.find(goal)!=A.end()&&A[goal]==X)
					{
						if(goal.L>>id&1)ans[X]="Switch off room "+to_string(id+1)+".";
						else ans[X]="Switch on room "+to_string(id+1)+".";
						break;
					}
					goal.L^=1<<id;
				}
				if(ans[X]=="")
				{
					for(int v:G[goal.now])
					{
						int tmp=goal.now;
						goal.now=v;
						if(A.find(goal)!=A.end()&&A[goal]==X)
						{
							ans[X]="Move to room "+to_string(tmp+1)+".";
							break;
						}
						goal.now=tmp;
					}
				}
			}
			if(!(goal==start))return 1;
			X=tmpX;
			for(int i=1;i<X;i++)
			{
				for(int j=i-1;j>=0;j--)
				{
					if(ans[j][0]=='S'&&ans[j+1][0]=='S')
					{
						bool flag=false;
						int id=0,scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j][id]==' ';
							id++;
						}
						int A,B;
						A=ans[j][id]-'0';
						if(ans[j][id+1]!='.')A=A*10+ans[j][id+1]-'0';
						id=scnt=0;
						while(scnt<3)
						{
							scnt+=ans[j+1][id]==' ';
							id++;
						}
						B=ans[j+1][id]-'0';
						if(ans[j+1][id+1]!='.')B=B*10+ans[j+1][id+1]-'0';
						if(A>B)swap(ans[j],ans[j+1]);
					}
				}
			}
			for(const string&s:ans)cout<<s<<endl;
		}
		else
		{
			bool flag=false;
			for(map<state,int>::iterator it=A.begin();it!=A.end();it++)
			{
				flag|=it->first.now==N-1;
				if(flag)break;
			}
			cout<<(flag?"You can not switch off all lights.":"Help me!")<<endl;
		}
	}
}
accuracy: 1
time_ms: 50
memory_kb: 5784
final_rank: 11

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	Move,
	On,
	Off,
};


struct Data{
	Data(Type arg_type,int arg_room_id){
		type = arg_type;
		room_id = arg_room_id;
	}
	Type type;
	int room_id;
};

struct Info{
	void set(int arg_current_room,int arg_num_step,int arg_state){
		current_room = arg_current_room;
		num_step = arg_num_step;
		state  = arg_state;
	}
	int current_room,num_step,state;
	vector<Data> history;
};


int N,M,start = 0,goal;
int POW[16];
int min_cost[32768][15];
vector<int> G[15],button[15];


void func(){

	for(int i = 0; i < N; i++){
		G[i].clear();
		button[i].clear();
	}

	goal = N-1;

	int from,to;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	int first_state = 0,tmp;
	for(int i = 0; i < N; i++){
		scanf("%d",&tmp);
		if(tmp == 1){
			first_state += POW[i];
		}
	}

	int num;
	for(int i = 0; i < N; i++){
		scanf("%d",&num);
		for(int k = 0; k < num; k++){
			scanf("%d",&tmp);
			tmp--;
			button[i].push_back(tmp);
		}
	}

	for(int i = 0; i < N; i++){
		sort(button[i].begin(),button[i].end());
	}

	queue<Info> Q;
	Info first;
	first.set(start,0,first_state);
	Q.push(first);

	bool finished = false;
	int target_room,next_room,next_state;

	for(int state = 0; state < POW[N]; state++){
		for(int room = 0; room < N; room++){
			min_cost[state][room] = BIG_NUM;
		}
	}

	min_cost[first_state][start] = 0;

	Type next_type;

	while(!Q.empty()){

		if(Q.front().current_room == goal){
			finished = true;
			if(Q.front().state == POW[goal]){

				printf("You can go home in %d steps.\n",Q.front().num_step);
				for(int i = 0; i < Q.front().num_step; i++){
					switch(Q.front().history[i].type){
					case Move:
						printf("Move to room ");
						break;
					case On:
						printf("Switch on room ");
						break;
					case Off:
						printf("Switch off room ");
						break;
					}
					printf("%d.\n",Q.front().history[i].room_id+1);
				}

				return;
			}
		}

		if(Q.front().num_step > min_cost[Q.front().state][Q.front().current_room]){
			Q.pop();
		}else{

			for(int i = 0; i < button[Q.front().current_room].size(); i++){
				target_room = button[Q.front().current_room][i];

				if(target_room == Q.front().current_room)continue;

				if(Q.front().state & (1 << target_room)){
					next_state = Q.front().state-POW[target_room];
					next_type = Off;
				}else{
					next_state = Q.front().state+POW[target_room];
					next_type = On;
				}

				if(min_cost[next_state][Q.front().current_room] > Q.front().num_step+1){
					min_cost[next_state][Q.front().current_room] = Q.front().num_step+1;
					Info next_info;
					next_info.set(Q.front().current_room,Q.front().num_step+1,next_state);
					for(int k = 0; k < Q.front().num_step; k++){
						next_info.history.push_back(Q.front().history[k]);
					}
					next_info.history.push_back(Data(next_type,target_room));
					Q.push(next_info);
				}
			}

			//移動する場合
			for(int i = 0; i < G[Q.front().current_room].size(); i++){
				next_room = G[Q.front().current_room][i];

				if(Q.front().state & (1 << next_room)){
					if(min_cost[Q.front().state][next_room] > Q.front().num_step+1){
						min_cost[Q.front().state][next_room] = Q.front().num_step+1;
						Info next_info;
						next_info.set(next_room,Q.front().num_step+1,Q.front().state);
						for(int k = 0; k < Q.front().num_step; k++){
							next_info.history.push_back(Q.front().history[k]);
						}
						next_info.history.push_back(Data(Move,next_room));
						Q.push(next_info);
					}
				}
			}
			Q.pop();
		}
	}

	if(finished){
		printf("You can not switch off all lights.\n");
	}else{
		printf("Help me!\n");
	}
}


int main(){

	for(int i = 0; i < 16; i++)POW[i] = pow(2,i);

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 7328
final_rank: 5

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

#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define f first
#define s second
#define mp make_pair
typedef pair<int, int> P;
static const int INF = (1 <<29);

struct State{
   int pos;
   int light;
   vector<P> step;

   State(){}
   State(int p, int l, vector<P> s):pos(p),light(l),step(s){}
};

//light : 1 = ON, 0 = OFF
int n,m;
int initLight;
vector<int> edge[16], room[16];
int minStep[15][(1 << 15)];
bool exitFlag;
string table[3][16];//0 = ON, 1 = OFF, 2 = MOVE

void makeTable(){
   char tmp[50];
   for(int j = 1 ; j <= 15 ; j++){
     sprintf(tmp, "Switch on room %d.",j);
     table[0][j] = string(tmp);
     sprintf(tmp, "Switch off room %d.",j);
     table[1][j] = string(tmp);
     sprintf(tmp, "Move to room %d.",j);
     table[2][j] = string(tmp);
   }
}

void bfs(){
   rep(i,15)rep(j,(1 << 15))minStep[i][j] = INF;
   exitFlag = false;
   int goal = (1 << (n-1)) ;
   queue<State> q;
   q.push(State(0, initLight, vector<P>()));

   while(!q.empty()){
      State p = q.front();
      q.pop();

      if(p.pos==n){
	 // Case 1
	 if(p.light == goal){
	    cout << "You can go home in " << p.step.size() << " steps." << endl;
	    for(int i = 0 ; i < p.step.size() ; i++){
	       cout << table[p.step[i].f][p.step[i].s] << endl;
       	    }
	    return;
	 }
	 // Case 2
	 else{
	    exitFlag = true;
	    continue;
	 }
      }

      if(minStep[p.pos][p.light] <= p.step.size())continue;
      minStep[p.pos][p.light] = p.step.size();

      for(int j = 0 ; j < room[p.pos].size() ; j++){
	 int tmpLight = p.light;
	 vector<P> tmpStep = p.step;

         //??§??????????????????????????§????¶???????
	 if(((p.light >> room[p.pos][j]) & 1) && p.pos != room[p.pos][j]){
	    tmpLight -= (1 << room[p.pos][j]);
	    tmpStep.push_back(mp(1, room[p.pos][j]+1));
	 }

	 //??§?????????????????????????????§???????????????
	 if(((p.light >> room[p.pos][j]) & 1)==0){
	    tmpLight += (1 << room[p.pos][j]);
	    tmpStep.push_back(mp(0, room[p.pos][j]+1));
	 }

	 if(minStep[p.pos][tmpLight] > tmpStep.size()) q.push(State(p.pos, tmpLight, tmpStep));
      }

      for(int to = 0 ; to < edge[p.pos].size() ; to++){
	 if(p.pos==n-1){
	    q.push(State(edge[p.pos][to], p.light, p.step));
	 }
         //?¬??????¨?±??????§??????????????????????????§???????????¨?????§?????????
	 if((p.light >> edge[p.pos][to]) & 1){
	    vector<P> tmpStep = p.step;
	    tmpStep.push_back(mp(2, edge[p.pos][to]+1));
	    if(minStep[edge[p.pos][to]][p.light] > tmpStep.size()) q.push(State(edge[p.pos][to], p.light, tmpStep));
	 }
      }
   }

   // Case 2
   if(exitFlag) cout << "You can not switch off all lights." << endl;
   // Case 3
   else cout << "Help me!" << endl;
}

void init(){
   initLight = 0;

   for(int i = 0 ; i < 16 ; i++){
      edge[i].clear();
      room[i].clear();
   }

   edge[n-1].push_back(n);
}

int main(){
   makeTable();
   while(cin >> n >> m){
      if(n==0 && m==0)break;

      init();

      //input
      for(int i = 0 ; i < m ; i++){
	 int s,t;
	 cin >> s >> t;
	 s--; t--;
	 edge[s].push_back(t);
	 edge[t].push_back(s);
      }

      for(int i = 0 ; i < n ; i++){
	 int v;
	 cin >> v;
	 if(v==1) initLight |= (1 << i);
      }

      for(int i = 0 ; i < n ; i++){
	 int k;
	 cin >> k;
	 for(int j = 0 ; j < k ; j++){
	    int v;
	    cin >> v;
	    v--;
	    room[i].push_back(v);
	 }
	 sort(room[i].begin(), room[i].end());
      }

      bfs();
   }
   return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 15336
final_rank: 20

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

struct action {
	int type;
	int num;
};
struct room {
	vector<int>edges;
	vector<int>switches;
	room():edges(), switches(){
	}
};
vector<action> memo[1 << 15][15];
int N, M;
bool cangoal = false;

struct aa {
	int now;
	bitset<15>lights;
	int time;
};
class Compare {
public:
	bool operator ()(const aa&l, const aa&r) {
		return  l.time > r.time;
	}
};

int main() {
	while (1) {
		
		queue<aa>que;
		cin >> N >> M;
		if (!N)break;
		vector<room>rooms(N);
		{
			cangoal = false;
			for (int i = 0; i < (1 << 15); ++i) {
				for (int j = 0; j < 15; ++j) {
					memo[i][j] = vector<action>();
				}
			}
			
			for (int i = 0; i < M; ++i) {
				int s, t; cin >> s >> t;
				s--; t--;
				rooms[s].edges.push_back(t);
				rooms[t].edges.push_back(s);
			}
			bitset<15>lights;
			for (int i = 0; i < N; ++i) {
				int l; cin >> l;
				lights[i] = l;
				assert(lights[0] == 1);
			}
			for (int i = 0; i < N; ++i) {
				int k; cin >> k;
				for (int j = 0; j < k; ++j) {
					int r; cin >> r;
					r--;
					rooms[i].switches.push_back(r);
				}
				sort(rooms[i].switches.begin(), rooms[i].switches.end());
			}
			que.push(aa{ 0,lights,0 });
		}
		vector<action>ans;
		while (!que.empty()) {
			aa atop(que.front());
			que.pop();
			if (atop.now == N - 1) {
				cangoal = true;
				if (atop.lights.count() == 1) {
					ans = memo[atop.lights.to_ulong()][atop.now];
					break;
				}
			}
			for (auto next : rooms[atop.now].edges) {
				if (atop.lights[next]) {
					if (memo[atop.lights.to_ulong()][next].empty()) {
						memo[atop.lights.to_ulong()][next] = memo[atop.lights.to_ulong()][atop.now];
						memo[atop.lights.to_ulong()][next].push_back(action{ 2,1 + next });
						que.push(aa{ next,atop.lights,atop.time+1 });
					}
				}
			}
			for (auto sw : rooms[atop.now].switches) {
				if (sw == atop.now)continue;
				bitset<15>nextlights(atop.lights);
				nextlights[sw] = !atop.lights[sw];
				if (memo[nextlights.to_ulong()][atop.now].empty()) {
					memo[nextlights.to_ulong()][atop.now] = memo[atop.lights.to_ulong()][atop.now];
					memo[nextlights.to_ulong()][atop.now].push_back(action{ !nextlights[sw],sw+1 });
					que.push(aa{ atop.now,nextlights,atop.time + 1 });
				}
			}
		}
		if (ans.empty()) {
			if (cangoal) {
				cout << "You can not switch off all lights." << endl;
			}
			else {
				cout << "Help me!" << endl;
			}
		}
		else {
			cout << "You can go home in " <<ans.size() << " steps." << endl;
			for (auto a : ans) {
				if (a.type == 0) {
					cout << "Switch on room " << a.num << "." << endl;
				}
				else if (a.type == 1) {
					cout << "Switch off room " << a.num <<"." << endl;
				}
				else {
					cout << "Move to room " << a.num << "." << endl;
				}
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 31724
final_rank: 19

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

typedef pair<int, int> pii;

int n, m;
int startBit;
int dp[1<<15][15];
int prevBit[1<<15][15];
int prevPos[1<<15][15];
vector<int> graph[15];
vector<int> swit[15];


int main() {
	while (cin >> n >> m, n || m) {
		startBit = 0;
		fill(dp[0], dp[1<<15], INF);
		fill(prevBit[0], prevBit[1<<15], INF);
		fill(prevPos[0], prevBit[1<<15], INF);
		REP(i, n) graph[i].clear();
		REP(i, n) swit[i].clear();
				
		REP(i, m) {
			int a, b;
			scanf("%d %d", &a, &b);
			a--; b--;
			graph[a].push_back(b);
			graph[b].push_back(a);
		}
		REP(i, n) {
			int a;
			scanf("%d", &a);
			startBit |= (a<<i);
		}
		REP(i, n) {
			int c;
			scanf("%d", &c);
			REP(j, c) {
				int a;
				scanf("%d", &a);
				a--;
				swit[i].push_back(a);
			}
		}
		
		queue<pii> que;
		que.push(pii(startBit, 0));
		dp[startBit][0] = 0;
		
		while (!que.empty()) {
			int nowBit = que.front().first;
			int nowPos = que.front().second;
			que.pop();
			
			if ((nowBit & (1<<nowPos) == 0)) continue;
			
			REP(i, graph[nowPos].size()) {
				int nextPos = graph[nowPos][i];
				
				if ((nowBit & (1<<nextPos)) > 0
					&& dp[nowBit][nextPos] > dp[nowBit][nowPos] + 1) {
					
					prevBit[nowBit][nextPos] = nowBit;
					prevPos[nowBit][nextPos] = nowPos;
					dp[nowBit][nextPos] = dp[nowBit][nowPos] + 1;
					que.push(pii(nowBit, nextPos));
				}
			}
			
			REP(i, swit[nowPos].size()) {
				int nextBit = nowBit ^ (1<<swit[nowPos][i]);
				
				if (swit[nowPos][i] == nowPos) continue;
				if (dp[nextBit][nowPos] > dp[nowBit][nowPos] + 1) {
					prevBit[nextBit][nowPos] = nowBit;
					prevPos[nextBit][nowPos] = nowPos;
					dp[nextBit][nowPos] = dp[nowBit][nowPos] + 1;
					que.push(pii(nextBit, nowPos));
				}
			}
		}
		
		if (dp[1<<(n - 1)][n - 1] != INF) {
			 printf("You can go home in %d steps.\n", dp[1<<(n - 1)][n - 1]);
			 vector<pii> ans; // first: 1move 2switch on 3switch off	second: number
			 
			 int nowBit = 1<<(n - 1);
			 int nowPos = n - 1;
			 
			 while (dp[nowBit][nowPos] != 0) {
				int nextBit = prevBit[nowBit][nowPos];
				int nextPos = prevPos[nowBit][nowPos];
			 	
			 	if (nextBit == nowBit) ans.push_back(pii(1, nowPos + 1));
			 	else {
			 		int tmp, pat;
			 		if (nextBit > nowBit) {
			 			pat = 3;
			 			tmp = nextBit & ~nowBit;
			 		}
			 		else {
			 			pat = 2;
			 			tmp = nowBit & ~nextBit;
			 		}
			 		
			 		int c = 0;
			 		while (tmp > 0) {
			 			tmp >>= 1;
			 			c++;
			 		}
			 		ans.push_back(pii(pat, c));
			 	}
			 	
			 	nowBit = nextBit;
			 	nowPos = nextPos;
			 }
			 reverse(ans.begin(), ans.end());
			 REP(i, ans.size()) {
			 	if (ans[i].first == 1) {
			 		printf("Move to room %d.\n", ans[i].second);
			 	}
			 	else {
			 		vector<pii> tmp;
			 		while (i < ans.size() && ans[i].first != 1) {
			 			tmp.push_back(pii(ans[i].second, ans[i].first));
			 			i++;
			 		}
			 		i--;
			 		sort(tmp.begin(), tmp.end());
			 		REP(j, tmp.size()) {
			 			if (tmp[j].second == 2) printf("Switch on room %d.\n", tmp[j].first);
			 			else printf("Switch off room %d.\n", tmp[j].first);
			 		}
			 	}
			 }
		}
		else {
			bool flag = false;
			REP(i, 1<<n) if (dp[i][n - 1] != INF) flag = true;
			if (flag) puts("You can not switch off all lights.");
			else puts("Help me!");
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7088
final_rank: 3

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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

typedef pair<int, int> pii;

int n, m;
int startBit;
int dp[1<<16][15];
int prevBit[1<<16][15];
int prevPos[1<<16][15];
vector<int> graph[15];
vector<int> swit[15];


int main() {
	while (cin >> n >> m, n || m) {
		startBit = 0;
		fill(dp[0], dp[1<<16], INF);
		fill(prevBit[0], prevBit[1<<16], INF);
		fill(prevPos[0], prevBit[1<<16], INF);
		REP(i, n) graph[i].clear();
		REP(i, n) swit[i].clear();
				
		REP(i, m) {
			int a, b;
			scanf("%d %d", &a, &b);
			a--; b--;
			graph[a].push_back(b);
			graph[b].push_back(a);
		}
		REP(i, n) {
			int a;
			scanf("%d", &a);
			startBit |= (a<<i);
		}
		REP(i, n) {
			int c;
			scanf("%d", &c);
			REP(j, c) {
				int a;
				scanf("%d", &a);
				a--;
				swit[i].push_back(a);
			}
		}
		
		queue<pii> que;
		que.push(pii(startBit, 0));
		dp[startBit][0] = 0;
		
		while (!que.empty()) {
			int nowBit = que.front().first;
			int nowPos = que.front().second;
			que.pop();
			
			if ((nowBit & (1<<nowPos) == 0)) continue;
			
			REP(i, graph[nowPos].size()) {
				int nextPos = graph[nowPos][i];
				
				if ((nowBit & (1<<nextPos)) > 0
					&& dp[nowBit][nextPos] > dp[nowBit][nowPos] + 1) {
					
					prevBit[nowBit][nextPos] = nowBit;
					prevPos[nowBit][nextPos] = nowPos;
					dp[nowBit][nextPos] = dp[nowBit][nowPos] + 1;
					que.push(pii(nowBit, nextPos));
				}
			}
			
			REP(i, swit[nowPos].size()) {
				int nextBit = nowBit ^ (1<<swit[nowPos][i]);
				
				if (swit[nowPos][i] == nowPos) continue;
				if (dp[nextBit][nowPos] > dp[nowBit][nowPos] + 1) {
					prevBit[nextBit][nowPos] = nowBit;
					prevPos[nextBit][nowPos] = nowPos;
					dp[nextBit][nowPos] = dp[nowBit][nowPos] + 1;
					que.push(pii(nextBit, nowPos));
				}
			}
		}
		
		if (dp[1<<(n - 1)][n - 1] != INF) {
			 printf("You can go home in %d steps.\n", dp[1<<(n - 1)][n - 1]);
			 vector<pii> ans; // first: 1move 2switch on 3switch off	second: number
			 
			 int nowBit = 1<<(n - 1);
			 int nowPos = n - 1;
			 
			 while (dp[nowBit][nowPos] != 0) {
				int nextBit = prevBit[nowBit][nowPos];
				int nextPos = prevPos[nowBit][nowPos];
			 	
			 	if (nextBit == nowBit) ans.push_back(pii(1, nowPos + 1));
			 	else {
			 		int tmp, pat;
			 		if (nextBit > nowBit) {
			 			pat = 3;
			 			tmp = nextBit & ~nowBit;
			 		}
			 		else {
			 			pat = 2;
			 			tmp = nowBit & ~nextBit;
			 		}
			 		
			 		int c = 0;
			 		while (tmp > 0) {
			 			tmp >>= 1;
			 			c++;
			 		}
			 		ans.push_back(pii(pat, c));
			 	}
			 	
			 	nowBit = nextBit;
			 	nowPos = nextPos;
			 }
			 reverse(ans.begin(), ans.end());
			 REP(i, ans.size()) {
			 	if (ans[i].first == 1) {
			 		printf("Move to room %d.\n", ans[i].second);
			 	}
			 	else {
			 		vector<pii> tmp;
			 		while (i < ans.size() && ans[i].first != 1) {
			 			tmp.push_back(pii(ans[i].second, ans[i].first));
			 			i++;
			 		}
			 		i--;
			 		sort(tmp.begin(), tmp.end());
			 		REP(j, tmp.size()) {
			 			if (tmp[j].second == 2) printf("Switch on room %d.\n", tmp[j].first);
			 			else printf("Switch off room %d.\n", tmp[j].first);
			 		}
			 	}
			 }
		}
		else {
			bool flag = false;
			REP(i, 1<<n) if (dp[i][n - 1] != INF) flag = true;
			if (flag) puts("You can not switch off all lights.");
			else puts("Help me!");
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 12848
final_rank: 16

code_snippet: # define _CRT_SECUREj_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

const int MOVE = 0, ON = 1, OFF = 2;
int n, m;
vi d[20];
vi sw[20];
pii pre[1 << 16][20];
pii pw[1 << 16][20];
struct P
{
	int a = 0;
	int bit = 0, now = 0;

	bool operator<(const P r)const
	{
		return a < r.a;
	}
	bool operator>(const P r)const
	{
		return a > r.a;
	}
};
bool ls(const pii& l, const pii& r)
{
	return l.second < r.second;
}
int main()
{
	while (cin >> n >> m&&n)
	{
		FOR(i, 20)
		{
			d[i].clear();
			sw[i].clear();
		}
		FOR(i, 1 << 16)  FOR(j, 20)pre[i][j] = pw[i][j] = pii(-1, -1);

		FOR(i, m)
		{
			int s, t; cin >> s >> t;
			d[s].push_back(t);
			d[t].push_back(s);
		}
		P start;
		vi ss;
		FOR(i, n)
		{
			INT(b);
			ss.push_back(b);
		}
		reverse(ALL(ss));
		FOR(i, n)
		{
			start.bit |= ss[i];
			start.bit <<= 1;
		}
		start.now = 1;
		FOR(i, n)
		{
			INT(k);
			FOR(j, k)
			{
				INT(x);
				sw[i + 1].push_back(x);
			}
			sort(ALL(sw[i + 1]));
		}
		priority_queue< P, vector<P>, greater<P> >que;
		que.push(start);
		bool ok = false;
		while (!que.empty())
		{
			P now = que.top(); que.pop();
			if (now.now == n)ok = true;
			FOR(i, d[now.now].size())
			{
				if (!(now.bit & 1 << d[now.now][i]))continue;
				P next = now;
				next.a++;
				next.now = d[now.now][i];
				if (pre[next.bit][next.now] == pii(-1, -1))
				{
					que.push(next);
					pre[next.bit][next.now] = pii(now.bit, now.now);
					pw[next.bit][next.now] = pii(MOVE, next.now);
				}
			}
			FOR(i, sw[now.now].size())
			{
				if (now.now == sw[now.now][i])continue;
				P next = now;
				next.a++;
				next.bit ^= 1 << sw[now.now][i];

				if (pre[next.bit][next.now] == pii(-1, -1))
				{
					que.push(next);
					pre[next.bit][next.now] = pii(now.bit, now.now);
					pw[next.bit][next.now] = pii(now.bit & 1 << sw[now.now][i] ? OFF : ON, sw[now.now][i]);
				}
			}

		}
		vector<pii> ans;
		if (ok)
		{
			if (pre[1 << n][n] != pii(-1, -1))
			{
				pii now(1 << n, n);
				while (now.first != start.bit || now.second != 1)
				{
					ans.push_back(pw[now.first][now.second]);
					now = pre[now.first][now.second];
				}
				reverse(ALL(ans));
				for (int i = 0; i < ans.size(); i++)
				{
					int l, r;
					if (ans[i].first == ON || ans[i].first == OFF)
					{
						r = l = i;
						while (ans[r].first == ON || ans[r].first == OFF)
						{
							r++;
							if (r == ans.size())break;
						}
						sort(ans.begin() + l, ans.begin() + r, ls);
					}
				}

				printf("You can go home in %d steps.\n", ans.size());
				FOR(i, ans.size())
				{
					switch (ans[i].first)
					{
					case MOVE:
						printf("Move to room %d.\n", ans[i].second);
						break;
					case OFF:
						printf("Switch off room %d.\n", ans[i].second);
						break;
					case ON:
						printf("Switch on room %d.\n", ans[i].second);
						break;
					}
				}
			}
			else
			{
				cout << "You can not switch off all lights." << endl;
			}
		}
		else
		{
			cout << "Help me!" << endl;
		}

	}
}
accuracy: 1
time_ms: 50
memory_kb: 23772
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<cassert>
#include<algorithm>
#include<bitset>
#include<deque>
#define F first
#define S second

using namespace std;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
struct Pox
{
  int cost;
  int p;
  int bit;
  vector<P> step;
  // P(1,R) -> Move to room R.
  // P(2,R) -> Switch off room R.
  // P(3,R) -> Switch on room R.

  Pox(int p,int bit,int cost):p(p),bit(bit),cost(cost){step.clear();}
};

int n,m;

int main()
{
  while(cin >> n >> m, (n || m))
    {
      VVI G;
      G.resize(n);
      for(int i=0;i<m;i++)
	{
	  int s,t;
	  scanf("%d %d",&s,&t);
	  t--,s--;
	  G[s].push_back(t);
	  G[t].push_back(s);
	}
      int initial_bit = 0;
      for(int i=0;i<n;i++)
	{
	  int b;
	  cin >> b;
	  if(b)
	    initial_bit |= (1<<i);
	}
   
      VVI E;
      E.resize(n);
      for(int i=0;i<n;i++)
	{
	  int k;
	  cin >> k;
	  for(int j=0;j<k;j++)
	    {
	      int r;
	      cin >> r;
	      r--;
	      E[i].push_back(r);
	    }
	  sort(E[i].begin(),E[i].end());
	}

      int mincost[1<<n][n];
      for(int i=0;i<=(1<<n)-1;i++)
	for(int j=0;j<n;j++)
	  mincost[i][j] = (1<<29);

      deque<Pox> deq;
      mincost[initial_bit][0] = 0;
      deq.push_back(Pox(0,initial_bit,0));

      int men_cost = (1<<29);
      vector<P> men_step;
      bool sub = false;

      while(!deq.empty())
	{
	  Pox pox = deq.front(); deq.pop_front();
	  
	  if(!((pox.bit >> pox.p) & 1))
	    continue;

	  if(pox.p == n-1 && __builtin_popcount(pox.bit) == 1 && (pox.bit>>(n-1)) & 1) 
	    {
	      if(men_cost > pox.cost)
		{
		  men_cost = pox.cost;
		  men_step = pox.step;
		}
	    }
	  else if(pox.p == n-1)
	    {
	      sub = true;
	    }

	  for(int i=0;i<G[pox.p].size();i++)
	    {// Move
	      int next = G[pox.p][i];

	      if(!((pox.bit>>next)&1))
		continue;

	      if(mincost[pox.bit][next] > pox.cost + 1)
		{
		  mincost[pox.bit][next] = pox.cost + 1;
		  Pox next_pox = Pox(next,pox.bit,pox.cost+1);
		  next_pox.step = pox.step;
		  next_pox.step.push_back(P(1,next));
		  deq.push_back(next_pox);
		}
	    }// Move

	  for(int i=0;i<E[pox.p].size();i++)
	    {
	      int next = E[pox.p][i];
	      
	      if(next == pox.p)
		continue;

	      bool swit = (pox.bit>>next)&1?0:1;
	      int nbit;
	      if(swit)
		  nbit = pox.bit,nbit |= (1<<next);
	      else 
		  nbit = pox.bit, nbit &= ~(1<<next);

	      bitset<10> before(nbit);

	      if(mincost[nbit][pox.p] > pox.cost+1)
		{
   		  mincost[nbit][pox.p] = pox.cost + 1;
		  Pox next_pox = Pox(pox.p,nbit,pox.cost+1);
		  next_pox.step = pox.step;
		  next_pox.step.push_back(P(swit?3:2,next));
		  deq.push_back(next_pox);
		}
	    }
	}

      if(men_cost == (1<<29) && !sub)
	  cout << "Help me!" << endl;
      else if(men_cost == (1<<29) && sub)
	  cout << "You can not switch off all lights." << endl;
      else if(men_cost != (1<<29))
	{
	  cout << "You can go home in " << men_cost << " steps." << endl;
	  for(int i=0;i<men_step.size();i++)
	    {
	      P p = men_step[i];
	      if(p.F == 1)
		cout << "Move to room " << p.S+1 << "." << endl;
	      else if(p.F == 2)
		cout << "Switch off room " << p.S+1 << "." << endl;
	      else if(p.F == 3)
		cout << "Switch on room " << p.S+1 << "." << endl;
	      else
		assert(false);
	    }
	}
    }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5372
final_rank: 4

code_snippet: #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
#define INF 1<<30
#define MAX_V 16

using namespace std;
typedef pair<int,int> P;
class data{
public:
	int cost,v,bit;
	data(){}
	data(int vv,int bb,int cc){
		cost=cc;
		v=vv;
		bit=bb;
	}
	bool operator<(const data &d)const{
		return cost>d.cost;
	}
};
int n,m;
vector<int> G[MAX_V];
int s[MAX_V];
int dp[MAX_V][1<<MAX_V];
int pv[MAX_V][1<<MAX_V],pbit[MAX_V][1<<MAX_V];
bool flag;
int dijk(int x){
	if(!(x & 1))return -1;
	for(int i=0;i<n;i++){
		for(int j=0;j<(1<<n);j++){
			dp[i][j]=INF;
			pv[i][j]=-1;
			pbit[i][j]=-1;
		}
	}
	priority_queue<data> que;
	que.push(data(0,x,0));
	dp[0][x]=0;
	while(que.size()){
		data q=que.top();
		que.pop();
		if(q.cost>dp[q.v][q.bit])continue;
		if(q.v==n-1 && q.bit==1<<(n-1))return q.cost;
		if(q.v==n-1)flag=true;
		for(int i=0;i<G[q.v].size();i++){
			int t=G[q.v][i];
			if(dp[t][q.bit]>q.cost+1 && (q.bit>>t & 1)){
				dp[t][q.bit]=q.cost+1;
				pv[t][q.bit]=q.v;
				pbit[t][q.bit]=q.bit;
				que.push(data(t,q.bit,q.cost+1));
			}
		}
		for(int i=0;i<n;i++){
			if(i==q.v)continue;
			if(pv[q.v][q.bit]==q.v && abs(q.bit-pbit[q.v][q.bit])>(1<<i))continue;
			if(s[q.v]>>i & 1){
				int nbit=q.bit^(1<<i);
				if(dp[q.v][nbit]>q.cost+1){
					dp[q.v][nbit]=q.cost+1;
					pv[q.v][nbit]=q.v;
					pbit[q.v][nbit]=q.bit;
					que.push(data(q.v,nbit,q.cost+1));
				}
			}
		}
	}
	return -1;
}

int main(void){
	while(1){
		scanf("%d%d",&n,&m);
		if(n+m==0)break;
		for(int i=0;i<m;i++){
			int f,t;
			scanf("%d%d",&f,&t);
			f--;
			t--;
			G[f].push_back(t);
			G[t].push_back(f);
		}
		int st=0;
		for(int i=0;i<n;i++){
			int a;
			scanf("%d",&a);
			st+=a<<i;
		}
		for(int i=0;i<n;i++){
			int a;
			scanf("%d",&a);
			for(int j=0;j<a;j++){
				int b;
				scanf("%d",&b);
				b--;
				s[i]|=1<<b;
			}
		}
		flag=false;
		int res=dijk(st);
		if(res==-1){
			if(!flag)printf("Help me!\n");
			else printf("You can not switch off all lights.\n");
		}else{
			printf("You can go home in %d steps.\n",res);
			vector<P> vec;
			int v=n-1,bit=1<<(n-1);
			while(v!=-1){
				vec.push_back(P(v,bit));
				int nv=pv[v][bit];
				int nbit=pbit[v][bit];
				v=nv;
				bit=nbit;
			}
			reverse(vec.begin(),vec.end());
			for(int i=0;i<(int)vec.size()-1;i++){
				if(vec[i].first!=vec[i+1].first)printf("Move to room %d.\n",vec[i+1].first+1);
				else{
					int a=abs(vec[i].second-vec[i+1].second);
					int cnt=1;
					while(a>1){
						a/=2;
						cnt++;
					}
					printf("Switch %s room %d.\n",vec[i].second>vec[i+1].second?"off":"on",cnt);
				}
			}
		}
		memset(s,0,sizeof(s));
		for(int i=0;i<n;i++){
			G[i].clear();
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 12544
final_rank: 10

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

struct data{
    int pos,state,cost;
    data(int pos,int state):pos(pos),state(state){}
};
const int INF=1001001001;

int N,M;
vector<int>G[15];
vector<int>light[15];
int dist[1<<16][15];
int prevstate[1<<16][15],prevpos[1<<16][15];
void solve(int bit){
    fill_n(*dist,(1<<16)*15,INF);
    dist[bit][0]=0;
    queue<data>que;
    que.push(data(0,bit));

    while(que.size()){
        data d=que.front();que.pop();

        for(int i=0;i<light[d.pos].size();i++){
            int l=light[d.pos][i];
            if(l==d.pos)continue;
            int tobit=d.state^(1<<l);
            if(dist[tobit][d.pos]<=dist[d.state][d.pos]+1)continue;
            dist[tobit][d.pos]=dist[d.state][d.pos]+1;
            prevstate[tobit][d.pos]=d.state;
            prevpos[tobit][d.pos]=d.pos;
            que.push(data(d.pos,tobit));
        }

        for(int i=0;i<G[d.pos].size();i++){
            int to=G[d.pos][i];
            if((d.state>>to&1)==0)continue;
            if(dist[d.state][to]<=dist[d.state][d.pos]+1)continue;
            dist[d.state][to]=dist[d.state][d.pos]+1;
            prevstate[d.state][to]=d.state;
            prevpos[d.state][to]=d.pos;
            que.push(data(to,d.state));
        }
    }

    bool flag=false;
    for(int i=0;i<(1<<N);i++)if(dist[i][N-1]!=INF)flag=true;
    if(!flag){
        cout<<"Help me!"<<endl;
        return;
    }

    if(dist[1<<(N-1)][N-1]==INF){
        cout<<"You can not switch off all lights."<<endl;
        return;
    }

    int state=1<<(N-1),pos=N-1;
    cout<<"You can go home in "<<dist[1<<(N-1)][N-1]<<" steps."<<endl;
    vector<string>log;
    while(state!=bit||pos!=0){
        string s;
        if(prevpos[state][pos]!=pos){
            stringstream ss;
            ss<<"Move to room "<<pos+1<<".";
            getline(ss,s);
            log.push_back(s);
            int a=prevstate[state][pos];
            int b=prevpos[state][pos];
            state=a;pos=b;
        }
        else{
            vector<int>id;
            vector<int>is;
            while(prevstate[state][pos]!=state&&(state!=bit||pos!=0)){
                for(int i=0;i<N;i++){
                    if((prevstate[state][pos]>>i&1)!=(state>>i&1)){
                        id.push_back(i);
                        is.push_back(state>>i&1);
                        break;
                    }
                }
                int a=prevstate[state][pos];
                int b=prevpos[state][pos];
                state=a;pos=b;
            }
            for(int i=0;i<id.size();i++){
                for(int j=i+1;j<id.size();j++){
                    if(id[i]<id[j]){
                        swap(id[i],id[j]);
                        swap(is[i],is[j]);
                    }
                }
            }
            for(int i=0;i<id.size();i++){
                stringstream ss;
                if(is[i])ss<<"Switch on room ";
                else ss<<"Switch off room ";
                ss<<id[i]+1;
                ss<<".";
                getline(ss,s);
                log.push_back(s);
            }

        }


    }

    reverse(log.begin(),log.end());
    for(int i=0;i<log.size();i++)cout<<log[i]<<endl;
}





int main(){
    while(cin>>N>>M,N||M){
        for(int i=0;i<N;i++)G[i].clear();
        for(int i=0;i<N;i++)light[i].clear();

        for(int i=0;i<M;i++){
            int a,b;cin>>a>>b;a--;b--;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        int bit=0;
        bool flag=false;
        for(int i=0;i<N;i++){
            int a;cin>>a;
            if(a)bit|=(1<<i);
            if(i==0&&a==0)flag=true;
        }


        for(int i=0;i<N;i++){
            int k;cin>>k;
            for(int j=0;j<k;j++){
                int a;cin>>a;a--;
                light[i].push_back(a);
            }
        }
        if(flag)cout<<"Help me!"<<endl;
        else solve(bit);
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11936
final_rank: 9

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

#define BIT(x) (1 << (x))

struct State {
	int cur;
	int light;
	vector<pair<int, int> > log;
	State(){}
};

const int MOVE = 1;
const int OFF = 2;
const int ON = 3;

int n, m;
vector<int> g[15];
vector<int> sw[15];
bool vis[15][1 << 15];

State initial;

void solve()
{
	memset(vis, 0, sizeof(vis));
	
	int ca = 3;
	vector<pair<int, int> > res;
	
	queue<State> q;
	if (initial.light & BIT(0)) q.push(initial);
	
	while (q.size()){
		State st = q.front();
		q.pop();
			
		if (vis[st.cur][st.light]) continue;
		vis[st.cur][st.light] = true;
		
		if (st.cur == n - 1){
			if (st.light == BIT(n - 1)){
				ca = 1;
				res = st.log;
				break;
			}
			else ca = 2;
		}
		
		for (int i = 0; i < g[st.cur].size(); i++){
			State nx = st;
			nx.cur = g[st.cur][i];
			if (st.light & BIT(g[st.cur][i])){
				nx.log.push_back(make_pair(MOVE, nx.cur));
				q.push(nx);
			}
		}
		
		for (int i = 0; i < sw[st.cur].size(); i++){
			if (st.cur == sw[st.cur][i]) continue;
			State nx = st;
			nx.light ^= BIT(sw[st.cur][i]);
			if (nx.light & BIT(sw[st.cur][i])){
				nx.log.push_back(make_pair(ON, sw[st.cur][i]));
			}
			else {
				nx.log.push_back(make_pair(OFF, sw[st.cur][i]));
			}
			q.push(nx);
		}
	}
	
	if (ca == 1){
		printf("You can go home in %d steps.\n", res.size());
		for (int i = 0; i < res.size(); i++){
			if (res[i].first == MOVE){
				printf("Move to room %d.\n", res[i].second + 1);
			}
			else if (res[i].first == OFF){
				printf("Switch off room %d.\n", res[i].second + 1);
			}
			else if (res[i].first == ON){
				printf("Switch on room %d.\n", res[i].second + 1);
			}
		}
	}
	else if (ca == 2){
		puts("You can not switch off all lights.");
	}
	else if (ca == 3){
		puts("Help me!");
	}
}

int main()
{
	while (scanf("%d %d", &n, &m), n){
		for (int i = 0; i < 15; i++){
			g[i].clear();
			sw[i].clear();
		}
		
		for (int i = 0; i < m; i++){
			int s, t;
			scanf("%d %d", &s, &t);
			g[--s].push_back(--t);
			g[t].push_back(s);
		}
		
		int light = 0;
		for (int i = 0; i < n; i++){
			int a;
			scanf("%d", &a);
			if (a) light |= BIT(i);
		}
		
		for (int i = 0; i < n; i++){
			int k;
			scanf("%d", &k);
			for (int j = 0; j < k; j++){
				int r;
				scanf("%d", &r);
				sw[i].push_back(--r);
			}
			sort(sw[i].begin(), sw[i].end());
		}
		
		initial.cur = 0;
		initial.light = light;
		initial.log.clear();
		
		solve();
	}
}
accuracy: 1
time_ms: 130
memory_kb: 11644
final_rank: 18

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

using namespace std;

#define MAX 15
#define INF 1e9

int N,M,light,swt[MAX];
vector<int> G[MAX];

void init(){
  for(int i = 0 ; i < MAX ; i++){
    G[i].clear();
    swt[i] = 0;
  }
}

struct State{
  int v,l;
  State(){}
  State(int v,int l) : v(v),l(l) {}
};

void solve(){
  int d[MAX][1<<MAX];
  State path[MAX][1<<MAX];
  fill(d[0],d[0]+MAX*(1<<MAX),INF);

  bool found = false;
  d[0][light] = 0;
  queue<State> Q;
  Q.push(State(0,light));
  while(!Q.empty()){
    State s = Q.front(); Q.pop();
    int v = s.v;
    if(v == N-1 && s.l == (1<<(N-1))){
      found = true;
      break;
    }
    for(int i = 0 ; i < (int)G[v].size() ; i++){
      int to = G[v][i];
      if(d[v][s.l] + 1 < d[to][s.l] && (s.l >> to & 1)){
        d[to][s.l] = d[v][s.l] + 1;
        path[to][s.l] = s;
        Q.push(State(to,s.l));
      }
    }
    for(int i = 0 ; i < N ; i++){
      if(i == v){ continue; }
      if(swt[v] >> i & 1){
        int nl = s.l;
        if(s.l >> i & 1){
          nl -= (1<<i);
        }else{
          nl |= (1<<i);
        }
        if(d[v][s.l] + 1 < d[v][nl]){
          d[v][nl] = d[v][s.l] + 1;
          path[v][nl] = s;
          Q.push(State(v,nl));
        }
      }
    }
  }
  if(found){
    cout << "You can go home in " << d[N-1][1<<(N-1)] << " steps." << endl;
    int v = N-1,l = 1<<(N-1);
    vector<State> vec;
    vec.push_back(State(N-1,(1<<(N-1))));
    while(true){
      if(v == 0 && l == light){ break; }
      State next = path[v][l];
      vec.push_back(next);
      v = next.v; l = next.l;
    }
    reverse(vec.begin(),vec.end());
    int size = vec.size();
    for(int i = 0 ; i < size-1 ; i++){
      State cs = vec[i],ns = vec[i+1];
      if(cs.v == ns.v){
        int j;
        bool on = false, off = false;
        for(j = 0 ; j < N ; j++){
          if((cs.l >> j & 1) && !(ns.l >> j & 1)){
            off = true;
            break;
          }
          if(!(cs.l >> j & 1) && (ns.l >> j & 1)){
            on = true;
            break;
          }
        }
        if(on){
          cout << "Switch on room " << j+1 << "." << endl;
        }else if(off){
          cout << "Switch off room " << j+1 << "." << endl;
        }
      }else{
        cout << "Move to room " << ns.v+1 << "." << endl;
      }
    }
  }else{
    for(int i = 0 ; i < (1<<N) ; i++){
      if(d[N-1][i] != INF){
        cout << "You can not switch off all lights." << endl;
        return;
      }
    }
    cout << "Help me!" << endl;
  }
}

int main(){
  int s,t,k,r;
  while(cin >> N >> M, (N | M)){
    init();
    for(int i = 0 ; i < M ; i++){
      cin >> s >> t; s--; t--;
      G[s].push_back(t);
      G[t].push_back(s);
    }
    int x;
    light = 0;
    for(int i = 0 ; i < N ; i++){
      cin >> x;
      if(x == 1){ light |= (1<<i); }
    }
    for(int i = 0 ; i < N ; i++){
      cin >> k;
      for(int j = 0 ; j < k ; j++){
        cin >> r; r--;
        swt[i] |= (1<<r);
      }
    }
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5772
final_rank: 1

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
vector<int>g[20];
int bfs[1<<15][15];
int rev[1<<15][15];
int r[16];
int can[16][16];
int ans[1000000];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		for(int i=0;i<a;i++)g[i].clear();
		for(int i=0;i<b;i++){
			int p,q;
			scanf("%d%d",&p,&q);p--;q--;
			g[p].push_back(q);
			g[q].push_back(p);
		}
		for(int i=0;i<a;i++)scanf("%d",r+i);
		for(int i=0;i<a;i++)for(int j=0;j<a;j++)can[i][j]=0;
		for(int i=0;i<a;i++){
			int c;scanf("%d",&c);
			for(int j=0;j<c;j++){
				int d;scanf("%d",&d);
				d--;
				can[i][d]=1;
			}
		}
		for(int i=0;i<(1<<a);i++)for(int j=0;j<a;j++)
			bfs[i][j]=-1;
		int now=0;
		for(int i=0;i<a;i++)now+=r[i]*(1<<i);
		bfs[now][0]=0;
		queue<pair<int,int> > Q;
		Q.push(make_pair(now,0));
		while(Q.size()){
			int bit=Q.front().first;
			int at=Q.front().second;
			Q.pop();
			for(int i=0;i<g[at].size();i++){
				if((bit&(1<<g[at][i]))&&!~bfs[bit][g[at][i]]){
					bfs[bit][g[at][i]]=bfs[bit][at]+1;
					rev[bit][g[at][i]]=at;
					Q.push(make_pair(bit,g[at][i]));
				}
			}
			for(int i=0;i<a;i++){
				if(at!=i&&can[at][i]&&!~bfs[bit^(1<<i)][at]){
					bfs[bit^(1<<i)][at]=bfs[bit][at]+1;
					rev[bit^(1<<i)][at]=-1-i;
					Q.push(make_pair(bit^(1<<i),at));
				}
			}
		}
		if(r[0]==0){
			printf("Help me!\n");
			continue;
		}
		if(~bfs[(1<<(a-1))][a-1]){
			printf("You can go home in %d steps.\n",bfs[(1<<(a-1))][a-1]);
			int bit=(1<<(a-1));
			int at=a-1;
			for(int i=0;i<bfs[(1<<(a-1))][a-1];i++){
				
				if(rev[bit][at]>=0){
					ans[bfs[(1<<(a-1))][a-1]-i-1]=at;
					at=rev[bit][at];
				}
				else{
					ans[bfs[(1<<(a-1))][a-1]-i-1]=rev[bit][at];
					bit^=(1<<(-rev[bit][at]-1));
				}
			}
			for(int i=0;i<bfs[(1<<(a-1))][a-1];i++){
				if(ans[i]>=0)printf("Move to room %d.\n",ans[i]+1);
				else if(now&(1<<(-ans[i]-1))){
					printf("Switch off room %d.\n",-ans[i]);
					now^=(1<<(-ans[i]-1));
				}else {
					printf("Switch on room %d.\n",-ans[i]);
					now^=(1<<(-ans[i]-1));
				}
			}
		}else{
			bool ok=false;
			for(int i=0;i<(1<<a);i++)if(~bfs[i][a-1])ok=true;
			if(ok)printf("You can not switch off all lights.\n");
			else printf("Help me!\n");
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 6848
final_rank: 2