先判互相的包含关系,因为n<=50,直接bfs,(x,y)表示在A上走到x,B上走到y,若走到的x是输出源而y不是则B不包含y
判完包含关系后缩环拓扑序dp
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
inline void up(int &x,const int &y){if(x<y)x=y;}
inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 55;
int n;
int N[maxn],c[maxn][maxn];
int pi[maxn][maxn][2];
bool e[maxn][maxn];
struct node
{
int x,y;
node(){}
node(const int _x,const int _y){x=_x;y=_y;}
};
bool v[maxn][maxn];
queue<node>q;
// a < b
bool bfs(const int a,const int b)
{
for(int i=0;i<N[a];i++)
for(int j=0;j<N[b];j++) v[i][j]=false;
while(!q.empty()) q.pop();
q.push(node(0,0));
while(!q.empty())
{
const node x=q.front(); q.pop();
int tx,ty;
tx=pi[a][x.x][0],ty=pi[b][x.y][0];
if(c[a][tx]&&!c[b][ty]) return false;
if(!v[tx][ty]) v[tx][ty]=true,q.push(node(tx,ty));
tx=pi[a][x.x][1],ty=pi[b][x.y][1];
if(c[a][tx]&&!c[b][ty]) return false;
if(!v[tx][ty]) v[tx][ty]=true,q.push(node(tx,ty));
}
return true;
}
struct edge
{
int y,nex;
edge(){}
edge(const int _y,const int _nex){y=_y;nex=_nex;}
}a[maxn*maxn<<1]; int len,fir[maxn];
inline void ins(const int x,const int y){a[++len]=edge(y,fir[x]);fir[x]=len;}
int id,dfn[maxn],low[maxn];
int cnt,bel[maxn],s[maxn];
int t[maxn],tp;
bool ev[maxn];
void tarjan(const int x)
{
dfn[x]=low[x]=++id;
t[++tp]=x;
ev[x]=true;
for(int k=fir[x];k;k=a[k].nex)
{
const int y=a[k].y;
if(!dfn[y]) tarjan(y),down(low[x],low[y]);
else if(ev[y]) down(low[x],dfn[y]);
}
if(low[x]==dfn[x])
{
int la=0; ++cnt;
while(la!=x)
{
ev[la=t[tp--]]=false;
bel[la]=cnt;
s[cnt]++;
}
}
}
int d[maxn];
void build()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)if(e[i][j])
ins(i,j);
tp=0;id=0;
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
len=0; memset(fir,0,sizeof fir);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)if(e[i][j]&&bel[i]!=bel[j])
ins(bel[i],bel[j]),d[bel[j]]++;
}
int f[maxn];
queue<int>Q;
int DP()
{
for(int i=1;i<=cnt;i++) if(!d[i]) Q.push(i);
while(!Q.empty())
{
const int x=Q.front(); Q.pop();
f[x]+=s[x];
for(int k=fir[x];k;k=a[k].nex)
{
const int y=a[k].y;
d[y]--; if(!d[y]) Q.push(y);
up(f[y],f[x]);
}
}
int ans=0;
for(int i=1;i<=cnt;i++) up(ans,f[i]);
return ans;
}
int solve()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)if(i!=j)
if(bfs(i,j)) e[i][j]=true;
}
build();
return DP();
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int k; scanf("%d%d",&N[i],&k);
while(k--)
{
int x; scanf("%d",&x);
c[i][x]=1;
}
for(int j=0;j<N[i];j++) scanf("%d%d",&pi[i][j][0],&pi[i][j][1]);
}
printf("%d\n",solve());
return 0;
}