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 ...
随机推荐
- 常用Git命令大全
Git命令 查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show ...
- maven 项目 pom.xml文件中配置的jar包下载报错
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[ERROR] 'dependencies.depen ...
- Arpa's loud Owf and Mehrdad's evil plan
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- A Truthful (1-ɛ)-Optimal Mechanism for On-demand Cloud Resource Provisioning---INFOCOM 2015
[标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]
- chrom 快捷键 整理版
chrome窗口和标签页快捷键: Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shift+N 在隐身模式下打开新窗口 Ctrl+O,然后选择文件 在谷歌浏览器中打开计算机上的文件 ...
- tablespace
CREATE [UNDO] TABLESPACE tablespace_name [DATAFILE datefile_spec1 [,datefile_spec2] ...... [{MININUM ...
- blog地址
1 Java 设计模式 http://www.cnblogs.com/java-my-life/
- cuckoo数据库变更
1.cuckoo版本升级 cuckoo默认的数据库为sqlite,默认连接方式为sqlite:///os.path.join(REPORT_ROOT, "db", "cu ...
- python之~【空格】可不能随便加唷~
上个礼拜学习从.proto文件转xxpb2.py文件的时候,明明成功了的. 结果周末的时候在家里,以及今天周一来到公司电脑,都遇到同样的一个问题. 我就纳闷了.这个路径确实存在呀. 而且我找遍了搜索引 ...
- php:跨域
一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...