有n个点,两两之间有一条有向边,划分成两个点集,要求先判断每个点集的导出子图没有环,然后求在导出子图不形成环的前提下点集A中最多可以加入多少点集B中的点。
判断YES或NO的时候拓扑排序或者暴力O(N^2)判断都可以. 接下来对于T2中的每个点,只需要在T1中扫一遍就可以判断出是否可以放进T1,如果不能放进去就直接丢掉,如果可以就确定放在哪个位置. 注意因为是个竞赛图,所以这个位置是唯一的. 给T2剩下的点按在T2中的拓扑顺序(因为是竞赛图,所以这个顺序也是唯一的)标上它们在T1中的位置值,对得到的这个位置值数列求最长上升子序列的长度就是答案. 总复杂度O(N^2).
北大:卡常数题!!!
admin:时限已经设了标程的3倍
讲道理,这题卡常数==
用了gets乱读才跟上标程的脚步
用这题感受一下DAG里的拓扑关系挺舒服的 :)
#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
int f=1;char c;x=0;
while(c=getchar(),c<48)if(c=='-')f=-1;
do x=x*10+(c^48);
while(c=getchar(),c>47);
x*=f;
}
template<class T>void nt(T x){
if(!x)return;
nt(x/10);
putchar(x%10+'0');
}
template<class T>void pt(T x){
if(x<0)putchar('-'),x=-x;
if(!x)putchar('0');
else nt(x);
}
template<class T>void ptn(T x){
pt(x);putchar('\n');
}
template<class T>void pts(T x){
pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}
const int maxn=1005;
int mark[maxn],allc;
int n,m;
int w[maxn][maxn];
int a[maxn],tota;
int b[maxn],totb;
int d[maxn],q[maxn];
bool chk(int *dat,int tot){
rep(i,1,tot+1)d[dat[i]]=0;
rep(i,1,tot+1)rep(j,1,i)
if(w[dat[i]][dat[j]]==1)d[dat[i]]++;
else d[dat[j]]++;
int L,H;L=H=0;
rep(i,1,tot+1)if(!d[dat[i]])q[++H]=dat[i];
while(L<H){
int p=q[++L];
rep(i,1,tot+1)if(w[dat[i]][p]){
d[dat[i]]--;
if(!d[dat[i]])q[++H]=dat[i];
}
}
if(H<tot)return false;
rep(i,1,tot+1)dat[i]=q[i];
return true;
}
void solve(){
rep(i,1,totb+1){
int pos=0;
rep(j,1,tota+1){
if(w[b[i]][a[j]])++pos;
else break;
}
q[i]=pos;
rep(j,pos+1,tota+1)if(w[b[i]][a[j]]){
q[i]=-1;
break;
}
}
int tot=0;
rep(i,1,totb+1){
if(q[i]==-1)continue;
if(!tot||q[i]>=b[tot])b[++tot]=q[i];
else *upper_bound(b+1,b+tot+1,q[i])=q[i];
}
ptn(tot);
}
char ch[maxn<<1];
int main(){
// freopen("1003.in","r",stdin);
// freopen("chk.out","w",stdout);
// int cas;sc(cas);
// while(cas--)solve();
while(sc(n),sc(m),n&&m){
rep(i,1,n+1){
gets(ch);
rep(j,1,n+1)w[i][j]=ch[j-1<<1]-'0';
}
tota=m;totb=0;
++allc;
rep(i,1,m+1)sc(a[i]),mark[a[i]]=allc;
rep(i,1,n+1)if(mark[i]!=allc)b[++totb]=i;
if(!chk(a,tota)||!chk(b,totb)){
puts("NO");
continue;
}
printf("YES ");
solve();
}
return 0;
}