题目链接:点击打开链接
思路:建边,跑一发 dfs 就好了,其实就问你第一个位置和 x 所在的位置是否联通
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,int> pii;
int n,k;
int a[200010];
bool vis[200010];
vector<int> v[200010];
map<pii,int> mp;
bool flag;
void dfs(int x)
{
if(x==1)
{
flag=1;
return ;
}
if(vis[x])
return ;
vis[x]=1;
for(int i=0;i<v[x].size();i++)
{
if(!vis[v[x][i]])
dfs(v[x][i]);
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
a[x]=i;
}
for(int i=0;i<200010;i++)
v[i].clear();
scanf("%d",&k);
while(k--)
{
for(int j=1;j<=n;j++)
{
int x;
scanf("%d",&x);
v[j].push_back(x);
}
}
int x;
scanf("%d",&x);
flag=0;
dfs(a[x]);
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}