zoukankan      html  css  js  c++  java
  • codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling

    Time Limit: 20 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就好了,其实就问你第一个位置和x所在的位置是否联通

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    #define maxn 200101
    #define mod 1000000009
    #define eps 1e-9
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    int a[maxn];
    vector<int> e[maxn];
    map<pair<int,int> ,int> Q;
    int flag=0;
    int vis[maxn];
    void dfs(int x)
    {
        if(x==1)
            flag=1;
        if(flag)
            return;
        if(vis[x])
            return;
        vis[x]=1;
        for(int i=0;i<e[x].size();i++)
        {
            if(vis[e[x][i]])
                continue;
            dfs(e[x][i]);
        }
    }
    int main()
    {
        int n=read();
        for(int i=1;i<=n;i++)
        {
            int x=read();
            a[x]=i;
        }
        int k=read();
        for(int i=1;i<=k;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int x=read();
                if(Q[make_pair(j,x)]==0)
                {
                    e[j].push_back(x);
                    Q[make_pair(j,x)]=1;
                }
            }
        }
        int x=read();
        dfs(a[x]);
        if(flag)
            puts("YES");
        else
            puts("NO");
    }
  • 相关阅读:
    在iview admin中封装axios请求
    git使用
    css选择器
    vue打包空白及字体路径错误问题
    axios 等待同步请求用法及多请求并发
    在Vuex更新,组件内的视图更新问题
    vue中用ajax上传文件
    在vue中使用lang="scss"出现报错解决思路
    HBuilder打包vue项目app后空白,并请求不到数据
    接口里返回的数据不全问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4657724.html
Copyright © 2011-2022 走看看