codeforces Gym 100187J J. Deck Shuffling dfs

J. Deck Shuffling

Time Limit: 2

 

Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100187/problem/J

Description

The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and k shuffle machines to shuffle this deck. As we know, i-th shuffle machine is characterized by its own numbers pi, 1, pi, 2, ..., pi, n such that if one puts n cards numbered in the order 1, 2, ..., n into the machine and presses the button on it, cards will be shuffled forming the deck pi, 1, pi, 2, ..., pi, n where numbers pi, 1, pi, 2, ..., pi, n are the same numbers of cards but rearranged in some order.

At the beginning of the experiment the cards in the deck are ordered as a1, a2, ..., an, i.e. the first position is occupied by the card with number a1, the second position — by the card with number a2, and so on. The scientist wants to transfer the card with number x to the first position. He can use all his shuffle machines as many times as he wants. You should determine if he can reach it.

Input

In the first line the only positive integer n is written — the number of cards in the Innokentiy's deck.

The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n) — the initial order of cards in the deck.

The third line contains the only positive integer k — the number of shuffle machines Innokentiy has.

Each of the next k lines contains n distinct integers pi, 1, pi, 2, ..., pi, n (1 ≤ pi, j ≤ n) characterizing the corresponding shuffle machine.

The last line contains the only integer x (1 ≤ x ≤ n) — the number of card Innokentiy wants to transfer to the first position in the deck.

Numbers n and k satisfy the condition 1 ≤ n·k ≤ 200000.

Output

Output «YES» if the scientist can transfer the card with number x to the first position in the deck, and «NO» otherwise.

Sample Input

4
4 3 2 1
2
1 2 4 3
2 3 1 4
1

Sample Output

YES

HINT

题意

给你一堆牌的原始顺序,给你k个洗牌机,给你每次使用完洗牌机之后的顺序,问能否洗到第一张牌是x的情况

题解:

  dfs无脑

代码

  #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define MOD 1000000007
#define maxn 32001
using namespace std;
typedef __int64 ll;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//*******************************************************************
struct ss
{ int to,next;
} edg[];
int st;
bool flag;
int t=;
int visit[];
int head[];
int n;
void init()
{
t=;
memset(head,,sizeof(head));
}
void add(int u,int v)
{
edg[t].to=v;
edg[t].next=head[u];
head[u]=t++;
}
bool NO;
void dfs(int x)
{
if(flag)return ;
if(x == )
{
flag=true;
return;
}
if(visit[x])return ;
visit[x]=true;
for(int i=head[x]; i; i=edg[i].next)
{
if(visit[edg[i].to])continue;
dfs(edg[i].to);
}
}
int main()
{
int a[];
n=read();
init();
for(int i=; i<=n; i++)
{
a[i]=read();
}
int m,x;
m=read();
for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
x=read();
if(x!=j)
{
add(x,j);
}
}
}
st=read();
for(int i=; i<=n; i++)
{
if(a[i]==st)
{
st=i;
break;
}
}
flag=false;
dfs(st);
if(flag)printf("YES\n");
else printf("NO\n");
return ;
}
上一篇:MUI手势锁


下一篇:NHibernate初探(1)