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

然后看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. windows上安装ubuntukylin16.04并且在ubuntukylin上安装jdk

    1.安装ubuntukylin16.04 教程链接:http://jingyan.baidu.com/article/f71d60379824041ab641d19d.html 我是完全按照这个教程来 ...

  2. 4、Xcode8中的钥匙串保存数据取出时候为空的问题

    Xcode7以及之前的版本直接使用Keychain存储数据即可,但是从Xcode8开始,再用之前的方法会发现,读取不到存进去的数据了,或者说,存储不进去了,原因是苹果加强了隐私保护,这个东西需要打开开 ...

  3. xrange()与range()

    range(start,stop,step) range(0,8) >>print range(0,8) [0,1,2,3,4,5,6,7] range()返回一个数字列表. start ...

  4. stm32 串口乱码的解决

    版权声明:本文为博主原创文章. 前几天在中移物联网申请了一个迷你开发板,运行官方提供的程序,感觉板子是正常的.但是自己写的程序能够刷到板子上,但是串口却是乱码.官方和我的额程序都是用的库函数的方式写的 ...

  5. linux的用户,群组和其他用户

    1 linux 安全模型:(多任务,多用户的操作系统) 1)使用user和group控制使用者对文件的存储权限. 2)用户使用账户和口令登录linux 3)每个文件都有owner(创建者),owner ...

  6. 组合,关联,聚合的区别(转自http://jimmyleeee.blog.163.com/blog/static/9309618200932014422932/)

    类间关系 在类图中,除了需要描述单独的类的名称.属性和操作外,我们还需要描述类之间的联系,因为没有类是单独存在的,它们通常需要和别的类协作,创造比单独工作更大的语义.在UML类图中,关系用类框之间的连 ...

  7. java.net.ConnectException connect refured

    原因:tomcat 连接拒绝:tomcat没有完全重启 只是部分重启 解决方案: 连接tomcat服务 命令:1:ps -ef|grep java : 2:kill -9 21060 3:查看tomc ...

  8. Hadoop-2.6.0安装文档

    前段时间在dataguru上报了一个hadoop的培训班,希望能够帮助自己更快的了解.掌握并且熟悉hadoop的开发和原理. 上一期的作业是要自己搭建一个hadoop的环境,并能运行mapreduce ...

  9. ARM驱动调试方法、思路总结、笔记

    驱动程序的调试一. 打印: prink, 自制proc文件UBOOT传入console=ttySAC0 console=tty11. 内核处理UBOOT传入的参数console_setup add_p ...

  10. ZOJ 2710 Two Pipelines

    计算几何+贪心 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm&g ...