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可以抽象成一 ...
随机推荐
- Qt之QSS(语法高亮)
简述 语法高亮是文本编辑器用来显示文本的,特别是源代码,根据不同的类别来用不同的颜色和字体显示.这个功能有助于编写结构化的语言,例如:编程语言.标记语言,这些语言的语法错误显示是有区别的. 简述 详细 ...
- Domino 使用递归算法获取视图值
在关系数据库中,有两字段是父子关系.通过递归算法,输入一个父ID,能够获取全部相应的子ID.这种数据结构在组织架构中常常使用.显示一般使用树形结构.在Domino中相同能够处理这种情况,下面是个小de ...
- excle查找操作-vlookup的使用心得
百度了一下vlookup的语法规则: 该函数的语法规则例如以下: VLOOKUP(lookup_value,table_array,col_index_num,range_lookup) 參数 简单说 ...
- 设置Webdriver启动chrome为默认用户的配置信息
Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...
- PC/H5端各浏览器兼容性问题及解决方案?
概念:所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码解析的差异,造成页面显示效果不统一的情况. 1>不同浏览器的标签默认的外补丁和内补丁不同问题症状:随便写几个标签,不加样式控制的情况 ...
- [ Git ] [ GitHub ] 如何刪除自己github上面的Repository-轉載
http://www.winwu.cc/2013/03/githubrepository.html
- sql 除法运算 保留两位小数
sql 除法运算 保留两位小数 SELECT 1530/60 select cast(1530*1./60 as decimal(18,1))
- ACM-ICPC 2016 Qingdao Preliminary Contest
A I Count Two Three I will show you the most popular board game in the Shanghai Ingress Resistance T ...
- RMAN备份脚本--DataGuard primary
单机环境全备 export ORACLE_BASE=/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORACL ...
- Nginx的日志管理
vim /usr/local/nginx/conf/nginx.conf #编辑 nginx 配置文件 server{ ...