首先判断一下两个集合是否能够拓扑排序,顺便记录下每个节点的拓扑序。

然后看T2中每个点在T1中能够放在哪一个位置,记录下这个位置Pi。

然后T2中(按拓扑序排好),计算Pi的一个非严格递增的LIS。LIS长度就是答案。

这题scanf读入都900+ms了,有时直接卡TLE。改用gets整行读入,时间上会有很大改进。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar(); int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=;
int n,m,g[maxn][maxn],r[maxn],d[maxn],a[maxn],LIS[maxn],dp[maxn];
bool f[maxn];
struct Edge {int u,v,nx;}e[maxn*maxn];
int sz,h[maxn];
char s[]; bool Top(int x)
{
queue<int>Q; sz=; memset(h,-,sizeof h); memset(r,,sizeof r);
for(int i=;i<=n;i++) for(int j=;j<=n;j++)
if(f[i]==x&&f[j]==x&&g[i][j]==)
r[j]++, e[sz].u=i, e[sz].v=j, e[sz].nx=h[i], h[i]=sz++;
sz=;
for(int i=;i<=n;i++) if(f[i]==x&&r[i]==) Q.push(i);
while(!Q.empty())
{
int t=Q.front(); Q.pop(); sz++; d[t]=sz;
for(int i=h[t];i!=-;i=e[i].nx)
{ r[e[i].v]--; if(r[e[i].v]==) Q.push(e[i].v); }
}
if(x==) { if(sz!=m) return ; return ; }
else { if(sz!=n-m) return ; return ; }
} int main()
{
while(~scanf("%d%d",&n,&m))
{
getchar(); if(n==&&m==) break;
for(int i=;i<=n;i++)
{
gets(s); int p=;
for(int j=;j<=n;j++) { g[i][j]=s[p]-''; p=p+; }
}
memset(f,,sizeof f); for(int i=;i<=m;i++) {int x; scanf("%d",&x),f[x]=;}
bool x1=Top(), x2=Top();
if(x1==||x2==) { printf("NO\n"); continue; }
for(int i=;i<=n;i++)
{
if(f[i]) continue; int L=m, R=;
for(int j=;j<=n;j++)
{
if(!f[j]) continue;
if(g[i][j]) L=min(L,d[j]-); else R=max(R,d[j]);
}
if(L!=R) a[i]=-; else a[i]=L;
}
memset(LIS,-,sizeof LIS);
for(int i=;i<=n;i++) { if(f[i]) continue; LIS[d[i]]=a[i]; }
int ans=; memset(dp,,sizeof dp);
for(int i=;i<=n-m;i++)
{
if(LIS[i]==-) continue;
int pre=; for(int j=;j<i;j++) if(LIS[j]<=LIS[i]) pre=max(pre,dp[j]);
dp[i]=pre+; ans=max(ans,dp[i]);
}
printf("YES %d\n",ans);
}
return ;
}

HDU 5811 Colosseo的更多相关文章

  1. HDU 5811 Colosseo(拓扑排序+单调DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

随机推荐

  1. centos7 安装openvswitch

    安装 1.安装依赖包:yum -y install openssl-devel wget kernel-devel 2.安装开发工具:yum groupinstall "Developmen ...

  2. python生产者消费者模型

    业界用的比较广泛,多线程之间进行同步数据的方法,解决线程之间堵塞,互相不影响. server --> 生产者 client --> 消费者 在一个程序中实现又有生产者又有消费者 ,生产者不 ...

  3. Git忽略对特定文件的跟踪和提交

    1.有未提交过的文件,并且此文件项目组中的其他人员也需要忽略,可将此文件的完整路径写入项目文件夹下的.gitignore文件. 2.有未提交过的文件,此这些文件与项目组中的其他人员无关,毋须写入.gi ...

  4. sql 将某列转换成一个字符串 for xml path用法

    declare @test table( name varchar(10)) insert into @test values('a') insert into @test values('b') i ...

  5. 基于HTML5 的WebSql本地设备数据库

    HTML5 的 Web SQL Database 用本地和会话存储实现简单的对象持久化,对繁琐的关系数据进行处理. 在 W3C 的 Web SQL Database 规范中(参照 介绍)有这样的描述: ...

  6. 【转】Matrix67:十个利用矩阵乘法解决的经典题目

    好像目前还没有这方面题目的总结.这几天连续看到四个问这类题目的人,今天在这里简单写一下.这里我们不介绍其它有关矩阵的知识,只介绍矩阵乘法和相关性质.    不要以为数学中的矩阵也是黑色屏幕上不断变化的 ...

  7. 《JS权威指南学习总结--8.3 函数的实参和形参》

    内容要点: 一.可选形参 当调用函数的时候传入的实参比函数声明时指定的形参个数要少,剩下的形参都将设置为undefined值. 例如: //将对象o中可枚举的属性名追加至数组 //如果省略a,则创建一 ...

  8. 帝国cms

    1:帝国cms 设置安装完成 2: 帝国cms 封面页模板 内容页模板和列表页模板完成

  9. ajax格式,需要指定交互的data类型

    思路 先打印所有返回值 console.log(d);  {''status'':999} 再查看返回值,能否转换为json dataType:'json',    //大小写敏感. datatype ...

  10. OMCS开发手册(01) -- 多媒体设备管理器

    我们在前面一篇文章中提到:任何一个OMCS的Client都有两种身份,Owner和Guest.多媒体设备管理器工作于OMCS客户端,并以Owner的身份管理本地所有的多媒体设备.多媒体设备管理器对象是 ...