Codeforces Gym 100187J J. Deck Shuffling Dfs

Source: Internet
Author: User

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

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

Description

The world famous scientist Innokentiy continues he innovative experiments with decks of cards. Now he had a deck of n cards and K shuffle machines to shuffle this deck. As we know, i-th shuffle machine are characterized by its own numbers pi, 1, pi, 2, ..., pi, n such that if one puts N card s numbered in the order 1, 2, ..., n into the machine and presses the button on it, cards'll be shuffled forming the DEC K Pi, 1, pi, 2, ..., pi, n where numbers pi, 1, pi, 2, ..., PI, n is the same numbers of cards but rearranged in some Ord Er.

At the beginning of the experiment the cards in the deck are ordered as A1, A2, ..., a, i.e. the first position is Occupi Ed 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 the all his shuffle machines as many times as he wants. You should determine if he can reach it.

Input

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 have.

Each of the next K lines contains n distinct integers pi, 1, pi, 2, ..., PI, n (1≤pi, j≤n) characterizing the CORRESPO Nding Shuffle machine.

The last line contains the only integer x (1≤x≤n)-the number of card innokentiy wants to transfer to the first posit Ion 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

40 '

Test instructions

Give you a pile of cards, give you k a shuffle machine, to you shuffle machine after washing the order, and then asked if you wash the cards, the first card for X

Exercises

The graph theory builds the side, then carries on the DFS, asks you whether the beginning and the end point Unicom. As for looping what to prevent with Vis.

Debug: The first time in the test22 timeout, the reason is not because the scale is too large to reach 20w, but because 1. The node at the beginning is not Vis=1 2. A child Dfs ends without a vis recovery, because the state is the same, and it is unnecessary to re-start from another father node to that child node 3. Test instructions don't understand? (In fact, the second example is still not very clear)

Code:

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <ctime>5#include <iostream>6#include <algorithm>7#include <Set>8#include <vector>9#include <sstream>Ten#include <queue> One#include <typeinfo> A#include <fstream> -#include <map> -#include <stack> thetypedefLong Longll; - using namespacestd; - #defineTest Freopen ("1.txt", "R", stdin) - #defineMAXN 2000001 + #defineMoD 10007 - #defineEPS 1e-9 + Const intinf=0x3f3f3f3f; A Constll infll =0x3f3f3f3f3f3f3f3fll; atInlineintRead () - { -ll x=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; in } -Inlinevoid  out(intx) { to    if(x>9) out(x/Ten); +Putchar (%Ten+'0'); - } the //************************************************************************************** * inta[200010]; $vector<int>e[200010];Panax Notoginseng intn,k; - voidInit () the { +n=read (); A      for(intI=1; i<=n;i++) theA[read ()]=i; +k=read (); -      for(intI=1; i<=k;i++) $     { $          for(intj=1; j<=n;j++) -         { -             intv=read (); the             if(j!=v) e[j].push_back (v); -         }Wuyi     } the } - BOOLvis[200000+ -]; Wu intDfsintu) - { About     if(u==1)return 1; $      for(intI=0; I<e[u].size (); i++) -     { -         intv=E[u][i]; -         if(!Vis[v]) { Avis[v]=1; +             if(Dfs (v)) the                 return 1; -         } $     } the     return 0; the } the voidSolve () the { -memset (Vis,0,sizeof(Vis)); in     intu=read (); thevis[a[u]]=1; the     if(Dfs (A[u])) Aboutprintf"yes\n"); the     Elseprintf"no\n"); the } the intMain () + { - init (); the solve ();Bayi     return 0; the}
View Code

Codeforces Gym 100187J J. Deck Shuffling Dfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.