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 ;
}

codeforces Gym 100187J J. Deck Shuffling dfs的更多相关文章

  1. Gym - 100187J J - Deck Shuffling —— dfs

    题目链接:http://codeforces.com/gym/100187/problem/J 题目链接:问通过洗牌器,能否将编号为x的牌子转移到第一个位置? 根据 洗牌器,我们可以知道原本在第i位置 ...

  2. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  3. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  4. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  5. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  6. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  7. codeforces Gym 100500 J. Bye Bye Russia

    Problem J. Bye Bye RussiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...

  8. codeforces gym 100357 J (网络流)

    题目大意 有n种物品,m种建筑,p个人. n,m,p∈[1,20] 每种建筑需要若干个若干种物品来建造.每个人打算建造一种建筑,拥有一些物品. 主角需要通过交易来建造自己的建筑,交易的前提是对方用多余 ...

  9. Codeforces Gym 100114 J. Computer Network

    Description 给出一个图,求添加一条边使得添加后的图的桥(割边)最少. Sol Tarjan. 一遍Tarjan求割边. 我们发现连接的两个点一定是这两个点之间的路径上的桥最多,然后就可以贪 ...

随机推荐

  1. Android手机 Fildder真机抓包

    Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的“进出”的数据(指co ...

  2. gdb 多线程调试

    gdb 多线程调试 http://hi.baidu.com/hcq11/blog/item/9f5bfc6e696209d680cb4a25.html  http://hi.baidu.com/lit ...

  3. thinkphp-2

    php的跨文件变量? global $g是一个脚本文件中, 函数外部的变量在函数中要使用时的 全局变量 $_GET等是所谓的"超全局变量", 但仍然是只能在一个脚本的范围内使用 要 ...

  4. 模板插件aTpl

    摘要: 前面给大家分享了一款js模板插件,后来经过完善推荐给大家.该插件支持ie5+,chrome等浏览器以及移动端浏览器,支持for和if语法,以及表达式. 项目地址:https://github. ...

  5. 自定义 array_map() 对应的递归函数 array_map_recursive()

    array_walk 有个原生递归函数 array_walk_recursive($arr, 'function', 'words'),但是 array_map 却没有对应的递归函数 array_ma ...

  6. 关于delphi PAServer 远程调试DLL文件

    用PAServer调试的话会产生一个默认user-connectionname的文件夹,且这个文件夹不能自定义.因此无法使调试的dll文件生成到host主程序所在的文件夹下而导致无法调试. 变通方法: ...

  7. Resumable Media Uploads in the Google Data Protocol

    Eric Bidelman, Google Apps APIs team February 2010 Introduction The Resumable Protocol Initiating a ...

  8. 如何选择Html.RenderPartial和Html.RenderAction

    Html.RenderPartial与Html.RenderAction这两个方法都是用来在界面上嵌入用户控件的. Html.RenderPartial是直接将用户控件嵌入到界面上: <%Htm ...

  9. Android 和iOS 创建本地通知

    1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...

  10. HDU2084基础DP数塔

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...