【Codeforces Gym - 100187J】 + DFS

J. Deck Shuffling
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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 的牌能否出现在第一个位置

建边~dfs ~

AC代码:

#include<cstdio>
#include<vector>
using namespace std;
const int K = 2e5 + 10;
vector <int> v[K];
int a[K],ok,vis[K];
void dfs(int p){
    if(ok || p == 1){
        ok = 1;
        return ;
    }
    vis[p] = 1;
    for(int i = 0; i < v[p].size(); i++)
        if(!vis[v[p][i]]) dfs(v[p][i]);
    return ;
}
int main()
{
    int n,k,p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
        scanf("%d",&k),a[k] = i;
    scanf("%d",&k);
    while(k--){
        for(int i = 1; i <= n; i++)
            scanf("%d",&p),v[p].push_back(i),v[i].push_back(p);
    }
    scanf("%d",&p);
    ok = 0;
    dfs(a[p]);
    if(ok) printf("YES\n");
    else printf("NO\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值