BZOJ1194: [HNOI2006]潘多拉的盒子(tarjan)
Description
.png)
Input
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
解题思路:
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
struct Pr_ma{
struct pnt_{
int ch[];
bool out;
}p[];
int n,m;
int can;
int f;
void Insert(void)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int id;
scanf("%d",&id);
p[id].out=true;
}
for(int i=;i<n;i++)
scanf("%d%d",&p[i].ch[],&p[i].ch[]);
return ;
}
}pr[];
struct pnt{
int hd;
int dfn;
int low;
int blg;
bool vis;
void res(int x)
{
dfn=low=x;
return ;
}
}p[];
struct pnt_{
int hd;
int wgt;
int ind;
}p_[];
struct ent{
int twd;
int lst;
}e[],e_[];
struct int_{int x;int y;};
int n;
int cnt;
int s_t;
int c_t;
int stt;
int col;
int cnt_;
int dp[];
int sta[];
int_ stack_[];
bool vis[][];
std::queue<int>Q;
std::queue<int_>Q_;
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
return ;
}
void ade_(int f_,int t_)
{
cnt_++;
e_[cnt_].twd=t_;
e_[cnt_].lst=p_[f_].hd;
p_[f_].hd=cnt_;
p_[t_].ind++;
return ;
}
bool Bfs(Pr_ma x_,Pr_ma y_)
{
while(!Q_.empty())Q_.pop();
memset(vis,,sizeof(vis));
Q_.push(stack_[]);
while(!Q_.empty())
{
int_ h=Q_.front();Q_.pop();
if(vis[h.x][h.y])continue;
vis[h.x][h.y]=true;
stack_[++s_t]=h;
if(x_.p[h.x].out&&!y_.p[h.y].out)return false;
int nx,ny;
for(int c=;c<;c++)
{
nx=x_.p[h.x].ch[c];
ny=y_.p[h.y].ch[c];
Q_.push((int_){nx,ny});
}
}
return true;
}
void tarjan(int x)
{
p[x].vis=true;
sta[++stt]=x;
p[x].res(++c_t);
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
if(p[to].dfn&&!p[to].blg)
p[x].low=std::min(p[x].low,p[to].dfn);
else if(!p[to].dfn)
{
tarjan(to);
p[x].low=std::min(p[x].low,p[to].low);
}
}
if(p[x].dfn==p[x].low)
{
col++;
int u;
do{
p_[col].wgt++;
u=sta[stt--];
p[u].blg=col;
}while(u!=x);
}
return ;
}
int Top_sort(void)
{
while(!Q.empty())Q.pop();
int ans=;
for(int i=;i<=col;i++)if(!p_[i].ind)
Q.push(i);
while(!Q.empty())
{
int x=Q.front();Q.pop();
dp[x]+=p_[x].wgt;
for(int i=p_[x].hd;i;i=e_[i].lst)
{
int to=e_[i].twd;
p_[to].ind--;
dp[to]=std::max(dp[to],dp[x]);
if(!p_[to].ind)Q.push(to);
}
ans=std::max(ans,dp[x]);
}
return ans;
}
int main()
{
// freopen("pandora.in","r",stdin);
// freopen("pandora.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)pr[i].Insert();
for(int i=,j;i<=n;i++)for(j=;j<=n;j++)if(i!=j)
if(Bfs(pr[i],pr[j]))ade(i,j);
for(int i=;i<=n;i++)if(!p[i].blg)
tarjan(i);
// for(int i=1;i<=n;i++)printf("%d\n",p[i].blg);
for(int u=;u<=n;u++)
{
for(int i=p[u].hd;i;i=e[i].lst)
{
int v=e[i].twd;
int x=p[u].blg;
int y=p[v].blg;
if(x!=y)ade_(x,y);
}
}
printf("%d\n",Top_sort());
return ;
}
BZOJ1194: [HNOI2006]潘多拉的盒子(tarjan)的更多相关文章
- 【强连通分量】Bzoj1194 HNOI2006 潘多拉的盒子
Description Sulotion 首先要对每对咒语机建图,判断机器a是否能生成所有机器b生成的 如果跑一个相同的串,最后结束的点b可输出a不可输出,判断就为否 大概就用这种思路,f[x][y] ...
- 图论(Tarjan缩点):BZOJ 1194: [HNOI2006]潘多拉的盒子
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 344 Solved: 181[Submit][Stat ...
- BZOJ 1194: [HNOI2006]潘多拉的盒子( BFS + tarjan + dp )
O(S²)枚举2个诅咒机, 然后O(n²)BFS去判断. 构成一个有向图, tarjan缩点, 然后就是求DAG的最长路.. ------------------------------------- ...
- 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 456 Solved: 215[Submit][Stat ...
- 1194: [HNOI2006]潘多拉的盒子
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 464 Solved: 221[Submit][Stat ...
- 【bzoj1194】 HNOI2006—潘多拉的盒子
http://www.lydsy.com/JudgeOnline/problem.php?id=1194 (题目链接) 题意 给出S个自动机,如果一个自动机u的所有状态是另一个自动机v的状态的子集,那 ...
- BZOJ 1194 [HNOI2006]潘多拉的盒子 (图论+拓扑排序+tarjan)
题面:洛谷传送门 BZOJ传送门 标签里三个算法全都是提高组的,然而..这是一道神题 我们把这道题分为两个部分解决 1.找出所有咒语机两两之间的包含关系 2.求出咒语机的最长上升序列 我们假设咒语机$ ...
- 1194: [HNOI2006]潘多拉的盒子 - BZOJ
Description Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语机S-1的顺序描述.每一块的 ...
- HNOI2006 潘多拉的盒子
题目描述 题解: 题目的描述比较长,理解起来也有一定难度.仔细读题后我们发现整个任务可以分成两个部分:找出咒语机之间所有的升级关系.求最长升级序列. 1. 求升级关系: 容易看出,咒语机i可以抽象成一 ...
随机推荐
- 洛谷 P1033 自由落体
P1033 自由落体 题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算公 ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- Mvc 返回文件直接下载
今天碰到一个问题,前端点击下载文件,后端判断文件是否存在,不存在则自动生成文件(图片),返回前端会自动下载文件 网上查了一些 Mvc action中返回File类型 设置一些contentType ...
- hive安装用mysql作为元数据库,mysql的设置
mysql的设置 在要作为元数据库的mysql服务器上建立hive数据库: #建立数据库 create database if not exists hive; #设置远程登录的权限 GRANT AL ...
- 项目中log4j的使用
基于ssm项目: 1.导入log4j.slf4j相关jar包 commons-logging-1.1.3.jar.log4j-1.2.12.jar.slf4j-api-1.6.6.jar.slf4j- ...
- IE11 补丁 KB2929437[已过期]
2014年4月 请更新此补丁 KB2929437 开发人员工具有重要更新 DOM 面板右侧新增 "更改"面板,用于记录调试时修改的 CSS Rules: JS 调试面板,新增 so ...
- iOS菜鸟成长笔记(3)——斯坦福公开课学习(1)
一.iOS四层结构 1.Core OS 是用FreeBSD和Mach所改写的Darwin, 是开源.符合POSIX标准的一个Unix核心.这一层包含或者说是提供了整个iPhone OS的一些基础功能, ...
- .NET与JAVA RSA密钥格式转换
一.该篇内容用于记录.net和Java之间,RSA公密钥的转换 using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; ...
- jquery on event
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- hadoop的mapReduce和Spark的shuffle过程的详解与对比及优化
https://blog.csdn.net/u010697988/article/details/70173104 大数据的分布式计算框架目前使用的最多的就是hadoop的mapReduce和Spar ...