O(S²)枚举2个诅咒机, 然后O(n²)BFS去判断. 构成一个有向图, tarjan缩点, 然后就是求DAG的最长路..

-----------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
 
using namespace std;
 
const int maxn = 59;
 
typedef pair<int, int> pii;
 
struct edge {
int to;
edge* next;
} E[10000], *pt = E, *head[maxn];
 
void addedge(int u, int v) {
pt->to = v; pt->next = head[u]; head[u] = pt++;
}
 
struct O {
int n, p[maxn][2];
bool F[maxn];
void init() {
memset(F, 0, sizeof F);
int m; scanf("%d%d", &n, &m);
while(m--) {
int t; scanf("%d", &t);
F[t] = true;
}
for(int i = 0; i < n; i++)
scanf("%d%d", p[i], p[i] + 1);
}
} o[maxn];
  

int N, T = 0, n = 0, CK = 0, vis[maxn][maxn];

int dfn[maxn], low[maxn], scc[maxn], w[maxn];
queue<pii> q;
stack<int> S;
 
void tarjan(int x) {
dfn[x] = low[x] = ++CK;
S.push(x);
for(edge* e = head[x]; e; e = e->next) if(!dfn[e->to]) {
tarjan(e->to);
low[x] = min(low[x], low[e->to]);
} else if(!~scc[e->to])
low[x] = min(low[x], dfn[e->to]);
if(dfn[x] == low[x]) {
int t;
do {
t = S.top(); S.pop();
w[scc[t] = n]++;
} while(t != x);
n++;
}
}
 
bool BFS(int A, int B) {
T++;
O &a = o[A], &b = o[B];
while(!q.empty()) q.pop(); q.push(make_pair(0, 0));
while(!q.empty()) {
pii o = q.front(); q.pop();
for(int i = 0; i < 2; i++) {
int x = a.p[o.first][i], y = b.p[o.second][i];
if(vis[x][y] == T) continue;
if(!a.F[x] && b.F[y]) return false;
vis[x][y] = T;
q.push(make_pair(x, y));
}
}
return true;
}
  

namespace G {

edge* head[maxn];
int deg[maxn], ans[maxn];
void addedge(int u, int v) {
pt->to = v; pt->next = head[u]; head[u] = pt++;
}
void init() {
for(int x = 0; x < N; x++)
for(edge* e = ::head[x]; e; e = e->next) 
if(scc[x] != scc[e->to]) addedge(scc[x], scc[e->to]);
}
void dfs(int x) {
if(ans[x]) return;
for(edge* e = head[x]; e; e = e->next) {
dfs(e->to);
ans[x] = max(ans[x], ans[e->to]);
}
ans[x] += w[x];
}
void solve() {
init();
memset(ans, 0, sizeof ans);
for(int i = 0; i < n; i++) dfs(i);
printf("%d\n", *max_element(ans, ans + n));
}
}
 
int main() {
scanf("%d", &N);
for(int i = 0; i < N; i++) o[i].init();
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if(i != j && BFS(i, j)) addedge(i, j);
memset(dfn, 0, sizeof dfn);
memset(scc, -1, sizeof scc);
memset(w, 0, sizeof w);
for(int i = 0; i < N; i++) if(!dfn[i]) tarjan(i);
G::solve();
return 0;
}

-----------------------------------------------------------------------------

1194: [HNOI2006]潘多拉的盒子

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 241  Solved: 127
[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

4
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

3

HINT

Source

BZOJ 1194: [HNOI2006]潘多拉的盒子( BFS + tarjan + dp )的更多相关文章

  1. 图论(Tarjan缩点):BZOJ 1194: [HNOI2006]潘多拉的盒子

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 344  Solved: 181[Submit][Stat ...

  2. BZOJ 1194 [HNOI2006]潘多拉的盒子 (图论+拓扑排序+tarjan)

    题面:洛谷传送门 BZOJ传送门 标签里三个算法全都是提高组的,然而..这是一道神题 我们把这道题分为两个部分解决 1.找出所有咒语机两两之间的包含关系 2.求出咒语机的最长上升序列 我们假设咒语机$ ...

  3. BZOJ 1194: [HNOI2006]潘多拉的盒子 [DP DFA]

    传送门 题意: s个DFA,选出尽量多的自动机a0, a1, a2, . . . , at,使得a1包含a0.a2包 含a1,以此类推.s ≤ 50. DFA的字符集为{0,1},有的节点是输出源,节 ...

  4. 1194: [HNOI2006]潘多拉的盒子

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 464  Solved: 221[Submit][Stat ...

  5. 1194: [HNOI2006]潘多拉的盒子 - BZOJ

    Description  Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述.每一块的 ...

  6. 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 456  Solved: 215[Submit][Stat ...

  7. BZOJ1194: [HNOI2006]潘多拉的盒子(tarjan)

    Description 传说中,有个神奇的潘多拉宝盒.如果谁能打开,便可以拥有幸福.财富.爱情.可是直到真的打开,才发现与之 相随的还有灾难.不幸.其实,在潘多拉制造这个宝盒的时候,设置了一些咒语来封 ...

  8. 【bzoj1194】 HNOI2006—潘多拉的盒子

    http://www.lydsy.com/JudgeOnline/problem.php?id=1194 (题目链接) 题意 给出S个自动机,如果一个自动机u的所有状态是另一个自动机v的状态的子集,那 ...

  9. 【强连通分量】Bzoj1194 HNOI2006 潘多拉的盒子

    Description Sulotion 首先要对每对咒语机建图,判断机器a是否能生成所有机器b生成的 如果跑一个相同的串,最后结束的点b可输出a不可输出,判断就为否 大概就用这种思路,f[x][y] ...

随机推荐

  1. 数组序列的capacity及size函数

    #include<iostream>#include<vector>using namespace std;int main(){    vector<int> i ...

  2. MySQL Select 优化

    准备: create table t(x int primary key,y int unique,z int); insert into t(x,y,z) values(1,1,1),(2,2,2) ...

  3. 男装电子零售商East Dane即将面世_衣装_YOKA时尚网

    男装电子零售商East Dane即将面世_衣装_YOKA时尚网 男装电子零售商East Dane即将面世

  4. CCTableView 简单样例

    非常像android中的listview #pragma once; #include "cocos2d.h" using namespace cocos2d; //使用CCTab ...

  5. Android 开发佳站【转】

    Android控件拖动 这里演示控件拖动的动画:     原理就是响应控件的Touch事件,在Touch事件中对移动进行处理,注意,一定要在布局文件中设置控件的 android:clickable=& ...

  6. 【HDU】病毒侵袭持续中(AC自己主动机+map)

    一開始一直WA,之后发现这道题不止一组输入,改成多组输入之后就过了. 利用map把每一个字符串映射到它相应的结点上即可了. 11909467 2014-10-19 11:54:00 Accepted ...

  7. unity3d插件Daikon Forge GUI 中文教程-5-高级控件listbox和progress bar的使用

    (游戏蛮牛首发)大家好我是孙广东.官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上,要观看是须要FQ的. 只是教 ...

  8. Windows 配置JAVA的环境变量

    Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...

  9. Python开发环境Spyder安装方法

    Spyder(Scientific PYthon Development EnviRonment)是一个强大的交互式 Python 语言开发环境,提供高级的代码编辑.交互测试.调试等特性,支持包括 W ...

  10. SSL握手流程

    一.SSL是什么? 安全套接字(SSL)协议是Web浏览器和Web服务器之间安全交换信息的协议. SSL介于应用层和TCP层之间,应用层数据不再直接传递给传输层,而是传递给SSL层,SSL层对从应用层 ...