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 ...
随机推荐
- chrome手动添加拓展
https://www.crx4chrome.com/crx/978/ Free Download Postman REST Client CRX 0.8.4.19 for ------------- ...
- JPA 系列教程 异常 集锦
异常1.hibernate升级到3.5版本 异常信息摘要: Associations marked as mappedBy must not define database mappings like ...
- 简单的java程序通过对话框输出 计算加减乘除运算(运算方法可选择)
import javax.swing.JOptionPane; // import class JOptionPane public class Addition { public static vo ...
- 二十七、oracle 例外
一.例外分类oracle将例外分为预定义例外.非预定义例外和自定义例外三种.1).预定义例外用于处理常见的oracle错误.2).非预定义例外用于处理预定义例外不能处理的例外.3).自定义例外用于处理 ...
- 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题
1.有关调用的一些问题: (1).在C#中直接调用java中的代码,无返回值: 在java中: public static void setAge(Context context , int leve ...
- 【LeetCode】434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- sublime text2教程
代码编辑器或者文本编辑器,对于程序员来说,就像剑与战士一样,谁都想拥有一把可以随心驾驭且锋利无比的宝剑,而每一位程序员,同样会去追求最适合自己的强大.灵活的编辑器,相信你和我一样,都不会例外. 我用过 ...
- HDU 1264 Counting Squares(模拟)
题目链接 Problem Description Your input is a series of rectangles, one per line. Each rectangle is speci ...
- MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参
在xml操作中经常会用到在某一个节点后或前面插入一个节点,MSXML DOM 中使用的函数是insertBefore(IXMLDOMNode *newChild, VARIANT refChild): ...
- winform实现矩形框截图
使用方法如下: private void button1_Click(object sender, EventArgs e) { s.GerScreenFormRectangle(); } priva ...