分析
先进行缩点
之后从终点倒着跑
对于一组边如果有一个点不能到达则这组边直接废掉
最后看只用没废掉的边能不能从起点走到终点
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int n,m,vis[100100],gone[100100],a[100100],t;
int sum,cnt,ist[100100],belong[100100],low[100100],dfn[100100];
stack<int>A;
vector<int>b[100100],v[100100],nv[100100],vv[100100];
inline void tarjan(int x){
dfn[x]=low[x]=++cnt;
ist[x]=1;
A.push(x);
for(int i=0;i<v[x].size();i++)
if(!dfn[v[x][i]]){
tarjan(v[x][i]);
low[x]=min(low[x],low[v[x][i]]);
}else if(ist[v[x][i]]){
low[x]=min(low[x],dfn[v[x][i]]);
}
if(low[x]==dfn[x]){
sum++;
while(1){
int u=A.top();
A.pop();
ist[u]=0;
belong[u]=sum;
if(u==x)break;
}
}
}
inline void dfs(int x){
if(vis[x])return;
vis[x]=1;
for(int i=0;i<vv[x].size();i++)dfs(vv[x][i]);
}
inline void pr(){puts("YES");exit(0);}
inline void go(int x){
if(gone[x])return;
if(x==belong[t])pr();
gone[x]=1;
for(int i=0;i<nv[x].size();i++)go(nv[x][i]);
}
int main(){
int i,j,k;
scanf("%d%d",&n,&m);
t=n+1;
for(i=1;i<=m;i++){
scanf("%d%d",&a[i],&k);
for(j=1;j<=k;j++){
int x;
scanf("%d",&x);
b[i].push_back(x);
v[a[i]].push_back(x);
}
if(!k)v[a[i]].push_back(t);
}
for(i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(i=1;i<=n;i++)
for(j=0;j<v[i].size();j++)
if(belong[i]!=belong[v[i][j]])
vv[belong[v[i][j]]].push_back(belong[i]);
dfs(belong[t]);
for(i=1;i<=m;i++){
int ok=1;
for(j=0;j<b[i].size();j++)
if(!vis[belong[b[i][j]]]){
ok=0;
break;
}
if(ok){
for(j=0;j<b[i].size();j++)
nv[belong[a[i]]].push_back(belong[b[i][j]]);
if(!b[i].size())nv[belong[a[i]]].push_back(belong[t]);
}
}
go(belong[1]);
puts("NO");
return 0;
}