【codeforces gym 100187J】 【dfs判连通】Deck Shuffling 【给你一堆牌和一些洗牌机,后者可以改变牌的顺序,问你能不能把数字为x的牌洗到第一个位置。】

传送门:J. Deck Shuffling

描述:

J. Deck Shuffling
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The world famous scientist Innokentiy continues his innovative experiments with decks of cards. Now he has a deck of n cards and kshuffle machines to shuffle this deck. As we know, i-th shuffle machine is characterized by its own numbers pi, 1pi, 2, ..., pi, n such that if one puts n cards numbered in the order 12...n into the machine and presses the button on it, cards will be shuffled forming the deckpi, 1pi, 2, ..., pi, n where numbers pi, 1pi, 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 a1a2, ..., 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 a1a2, ..., 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, 1pi, 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.

Examples
input
4
4 3 2 1
2
1 2 4 3
2 3 1 4
1
output
YES
input
4
4 3 2 1
2
1 2 4 3
2 1 3 4
1
output
NO

题意:

给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为x的牌洗到第一个位置。

思路:

除了x其他元素可以直接不用考虑,为了操作方便,反向建边,然后dfs判断连通性

代码:

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
#define mod 1000000007
using  namespace  std;

#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

const int N=2e5+10;

std::vector<int> son[N];
int n,k,x,a[N],vis[N];

bool dfs(int u){
  if(a[u]==x)return true;
  for(auto v : son[u]){//auto的用法
    if(!vis[v]){
      vis[v]=1;
      if(dfs(v))return true;
    }
  }
  return false;
}

int  main(){
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif

  read(n);
  rep(i, 1, n)read(a[i]);
  read(k);
  rep(i, 1, k){
    rep(j, 1, n){
      int t;
      read(t);
      if(t!=j)son[j].push_back(t);
    }
  }
  read(x);
  printf("%s\n", dfs(1)?"YES":"NO");
  return 0;
}



您提供的链接是Codeforces的一个题,题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。GymCodeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个题的页面,但具体的题内容和描述无法通过链接获取。如果您有具体的题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值