HDU 5811 Colosseo
首先判断一下两个集合是否能够拓扑排序,顺便记录下每个节点的拓扑序。
然后看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的更多相关文章
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
随机推荐
- Byte Array to Hexadecimal String
Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...
- SQLServer性能优化
http://www.cnblogs.com/studyzy/archive/2008/11/24/1339772.html
- 更换jdk版本:jdk1.8更换为jdk1.7之后输入java -version还是出现1.8的版本号
安装了1.7之后修改了JAVA_HOME的环境变量 修改成功之后,在cmd输入java -verson还是出现1.8的版本号 解决办法:将环境变量Path中的%JAVA_HOME%/bin 移到最前面 ...
- boolean attribute(布尔值属性) attribute vs property
boolean attribute(布尔值属性) boolean attribute HTML - Why boolean attributes do not have boolean val ...
- jfianl返回自定义的404页面
public class MyErrorRenderFactory implements IErrorRenderFactory{ public Render getRender(int errorC ...
- poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少
import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...
- 代码块(Block)回调一般阐述
本章教程主要对代码块回调模式进行讲解,已经分析其他回调的各种优缺点和适合的使用场景. 代码块机制 Block变量类型 Block代码封装及调用 Block变量对普通变量作用域的影响 Block回调接口 ...
- android项目实战 --ListView 头部ViewPager广告轮询图效果
看开源框架:https://github.com/tianshaojie/AndroidFine,里面有如下效果,特记录学习下,以后项目中用也好能够立刻想起来. 如上面所示,是常见项目中的图片轮训 ...
- Oracle第二天
Oracle第二天 整体安排(3天) 第一天:Oracle的安装配置(服务端和客户端),SQL增强(单表查询). 第二天:SQL增强(多表查询.子查询.伪列-分页),数据库对象(表.约束.序列),Or ...
- html历史
方法一: location.hash = 'abc' window.onhashchange = function(){} location.hash 返回结果 #abc => locati ...