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. Android 流媒体系列(一)

    Android   设置铃声分析 代码其实没有几行,这里简单记录下学习的过程. Android系统启动时会扫描系统与SD卡中的对媒体文件,分别存入数据库sqlite中,以contentProvider ...

  2. Maven 添加Jetty

        <build>         <finalName>springmvc</finalName>         <plugins>       ...

  3. (Android) Chinese Character

    Convert Chinese strings to English strings Apply pinyin4j.jar public static class ConvertChineseToPi ...

  4. localstroge与cookie的区别

    HTML5本地存储是一种让网页可以把键值对存储在用户浏览器客户端的方法.像Cookie一样,这些数据不会因为你打开新网站,刷新页面,乃至关闭你的浏览器而消失. 而与Cookie不同的时,这些数据不会每 ...

  5. 使用资源监控工具 glances

    http://www.ibm.com/developerworks/cn/linux/1304_caoyq_glances/ glances 可以为 Unix 和 Linux 性能专家提供监视和分析性 ...

  6. Oh, my goddess(bfs)

    Oh, my goddess 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Shining Knight is the embodiment of justice ...

  7. C# - 通过自定义注解反射生成SQL语句[转]

    转自http://blog.163.com/jong_cai/blog/static/87028045200902033553581/ -------------------------------- ...

  8. HOOK API(四)—— 进程防终止

    HOOK API(四) —— 进程防终止 0x00        前言 这算是一个实战吧,做的一个应用需要实现进程的防终止保护,查了相关资料后决定用HOOK API的方式实现.起初学习HOOK API ...

  9. (转)经典线程同步 互斥量Mutex

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  10. ThinkPHP第十七天(隐藏index.php和简短路径配置)

    1.路由设置,让路径中不显示index.php方法: 第一步:在apache中的httpd.conf中查找: LoadModule rewrite_module modules/mod_rewrite ...