描述:
题意:
给你一堆牌,和一些洗牌机,可以改变牌的顺序,问你能不能通过洗牌机把数字为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;
}