【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP
1194: [HNOI2006]潘多拉的盒子
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 456 Solved: 215
[Submit][Status][Discuss]
Description


Input
第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50)。文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述。每一块的格式如下。
一块的第一行有两个正整数n,m。分别表示该咒语机中元件的个数、咒语源输出元的个数(1≤m≤n≤50)。
接下来一行有m个数,表示m个咒语源输出元的标号(都在0到n-1之间)。接下来有n行,每一行两个数。第i行(0≤i≤n-1)的两个数表示pi,0和pi,1(当然,都在0到n-1之间)。
Output
第一行有一个正整数t,表示最长升级序列的长度。
Sample Input
1 1
0
0 0
2 1
0
1 1
0 0
3 1
0
1 1
2 2
0 0
4 1
0
1 1
2 2
3 3
0 0
Sample Output
HINT
Source
Solution
这道题还是比较有趣的..如果能够得到包含关系,可以直接拓扑排序dp求解最长链。
然后如何判断是否包含?需要利用自动机的相关性质。
考虑dp,$f[x][y]$表示自动机1和自动机2分别对应状态$x$和$y$,然后枚举转移更新$f[x'][y']$。
考虑什么情况不满足包含,即$f[x][y]=1$且$end[x]=1$且$end[y]=0$表示不包含,否则一定包含。
所以只要暴力的去枚举两两自动机跑即可,主要的思想就是去尝试构造01串使得一个能匹配另一个不能。
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int T,N,M;
struct DFANode{
int son[55][2],end[55];
}DFA[55],A,B;
bool f[55][55],flag; inline void Run(int x,int y)
{
if (!x || !y || f[x][y] || !flag) return;
f[x][y]=1;
if (!A.end[x] && B.end[y]) flag=0;
for (int i=0; i<=1; i++)
Run(A.son[x][i],B.son[y][i]);
}
inline bool Check(DFANode x,DFANode y)
{
memset(f,0,sizeof(f));
A=x,B=y; flag=1;
Run(1,1);
return flag;
} struct GraphNode{
struct EdgeNode{
int next,to;
}edge[55*55];
int head[55],cnt;
inline void AddEdge(int u,int v) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}
inline void InsertEdge(int u,int v) {AddEdge(u,v);}
}G1,G2; int visit[55],stack[55],top,dfn[55],low[55],dfsn;
int belong[55],size[55],scc;
inline void Tarjan(int x)
{
visit[x]=1; stack[++top]=x;
dfn[x]=low[x]=++dfsn;
for (int i=G1.head[x]; i; i=G1.edge[i].next)
if (!dfn[G1.edge[i].to])
Tarjan(G1.edge[i].to),low[x]=min(low[x],low[G1.edge[i].to]);
else
if (visit[G1.edge[i].to]) low[x]=min(dfn[G1.edge[i].to],low[x]);
if (dfn[x]==low[x]) {
int tops=0;
scc++;
while (tops!=x) {
tops=stack[top--];
belong[tops]=scc,visit[tops]=0,size[scc]++;
}
}
}
inline void Tarjan() {for (int i=1; i<=T; i++) if (!dfn[i]) Tarjan(i);} int mp[55][55],d[55];
inline void Rebuild()
{
for (int x=1; x<=T; x++)
for (int i=G1.head[x]; i; i=G1.edge[i].next)
if (belong[x]!=belong[G1.edge[i].to])
G2.InsertEdge(belong[x],belong[G1.edge[i].to]),d[belong[G1.edge[i].to]]++;
} queue<int>q;
int dp[55];
inline void Toposort()
{
for (int i=1; i<=scc; i++) if (!d[i]) q.push(i);
for (int i=1; i<=scc; i++) dp[i]=size[i];
while (!q.empty()) {
int now=q.front(); q.pop();
for (int i=G2.head[now]; i; i=G2.edge[i].next) {
dp[G2.edge[i].to]=max(dp[G2.edge[i].to],dp[now]+size[G2.edge[i].to]);
if (!--d[G2.edge[i].to]) q.push(G2.edge[i].to);
}
}
} int main()
{
T=read();
for (int i=1; i<=T; i++) {
N=read(),M=read();
for (int x,j=1; j<=M; j++)
x=read()+1,DFA[i].end[x]=1;
for (int j=1; j<=N; j++)
DFA[i].son[j][0]=read()+1,DFA[i].son[j][1]=read()+1;
}
for (int i=1; i<=T; i++)
for (int j=1; j<=T; j++)
if (i!=j && Check(DFA[i],DFA[j]))
G1.InsertEdge(i,j);
Tarjan();
Rebuild();
Toposort();
int ans=0;
for (int i=1; i<=scc; i++) ans=max(ans,dp[i]);
printf("%d\n",ans);
return 0;
}
【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP的更多相关文章
- BZOJ 1194: [HNOI2006]潘多拉的盒子( BFS + tarjan + dp )
O(S²)枚举2个诅咒机, 然后O(n²)BFS去判断. 构成一个有向图, tarjan缩点, 然后就是求DAG的最长路.. ------------------------------------- ...
- 图论(Tarjan缩点):BZOJ 1194: [HNOI2006]潘多拉的盒子
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 344 Solved: 181[Submit][Stat ...
- 1194: [HNOI2006]潘多拉的盒子
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 464 Solved: 221[Submit][Stat ...
- [BZOJ1194][HNOI2006][强连通分量Tarjan+dfs]潘多拉的盒子
[BZOJ1194][HNOI2006]潘多拉的盒子 Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- [NOIP2017]逛公园 最短路+拓扑排序+dp
题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...
- 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp
正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...
- 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP
[BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...
- bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...
随机推荐
- orcale数据库分配用户
account lock:创建用户的时候锁定用户 account unlock:创建用户的时候解锁用户,默认该选项 create user zhou8–用户名 identified by zhou88 ...
- Android 浏览器启动应用程序
点击浏览器中的URL链接,启动特定的App. 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]" ...
- OR 连接查询注意
用or 查询时, 取得是 每个or中条件的 查询的结果集union. select * from categorysecond t where ISNULL(null); ort.csid in (' ...
- Centos7.3安装vsftp服务
我们需要向centos操作系统的服务器上上传文件或者下载文件,这时候,ftp有必要安装下, 我们选择主流的vsftp: 第一步:安装vsftp yum install -y vsftpd 第二步:设置 ...
- python3项目之数据可视化
数据可视化指的是通过可视化表示来探索数据,它与数据挖掘紧密相关,而数据挖掘指的是使用代码来探索数据集的规律和关联. 数据科学家使用Python编写了一系列令人印象深刻的可视化和分析工具,其中很多也可供 ...
- 日志、字段备注查询、自增ID联系设置、常用存储过程
-----获取数据字典SQL(表字段说明)SELECT [Table Name] = OBJECT_NAME(c.object_id), [Column Name] = c.name, ...
- ObjectInputStream与ObjectOutputStream
雇员类 package io; import java.io.Serializable; @SuppressWarnings("serial") public class Emp ...
- HBase(一)HBase入门简介
一 HBase 的起源 HBase 的原型是 Google 的 BigTable 论文,受到了该论文思想的启发,目前作为 Hadoop 的子项目来开发维护,用于支持结构化的数据存储. Apache H ...
- bzoj 2752
2752 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> #includ ...
- 在Chrome浏览器中保存的密码有多安全?
本文由 伯乐在线 - 黄利民 翻译.未经许可,禁止转载!英文出处:howtogeek.欢迎加入翻译组. [2013-08-09 更新]:最近又开始讨论“Chrome浏览器明文保存密码这个话题了,国外一 ...