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. Jsoup代码解读之一-概述

    Jsoup代码解读之一-概述 今天看到一个用python写的抽取正文的东东,美滋滋的用Java实现了一番,放到了webmagic里,然后发现Jsoup里已经有了…觉得自己各种不靠谱啊!算了,静下心来学 ...

  2. 字符串的MD5的32位加密和16位加密

    import java.security.MessageDigest; import java.util.Locale; public class MD5Util { public static St ...

  3. Linux c 信号—pause、sigsuspend 的相同于区别

    pause函数:      功能:让进程暂停直到信号出现 #include<unistd.h> intpause(); 函数说明:pause()会令目前的进程暂停(进入睡眠状态),直至信号 ...

  4. 发送请求工具—Advanced REST Client

    Advanced REST Client是Chrome浏览器下的一个插件,通过它能够发送http.https.WebSocket请求.在Chrome商店下搜索Advanced REST Client, ...

  5. [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175 昨天收到了电子工业出版社寄过来的<cocos2d-x游戏开发之 ...

  6. 移动端开发(四):swiper.js

    swiper.js中文网:http://www.swiper.com.cn/ 文档结构 swiper.jquery.js    是需要引用jquery.js 或者 zepto.js 时,只需直接引用该 ...

  7. C++知识点整理——持续更新

    virtual是C++的一个关键字,virtual修饰的函数可以被子类重写.   用法:在返回值类型的前面添加关键字即可. override是C++的保留字(注意不是关键字),表示当前函数重写了基类的 ...

  8. 关于input标签的需要注意的几个小问题

    1.input元素没有结束标签,只有开始标签,即使写上结束标签也不起作用.如下 <input type="text" value="text" /> ...

  9. poj 2417

    Accepted 8508K 391MS C++ 2004B 相比下边,,优化太多太多了... /** baby-step-giant-step 因为数据量太大,,自己写hash **/ #inclu ...

  10. c风格字符串函数

    十一.C 风格字符串  1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat( ...