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可以抽象成一 ...
随机推荐
- duplicate报ORA-01017权限问题
duplicate报ORA-01017权限问题 环境: OS:RedHat EnterPrise Linux 5.8 x64 Cluster:Oracle Grid 11.2.0.4 Databa ...
- iOS之数据请求NSURLConnection
iOS之数据请求NSURLConnection NSString *lcsUrl = @"http://192.168.1.1:8080/lcsUrl"; //假设网址中有汉字.须 ...
- angularjs ng-app
<!DOCTYPE HTML> <html ng-app> //ng-app是初始化指令,整个页面都会被angularjs解析,写在div或者其他标签上表示只是局部的div和标 ...
- ckeidtor编辑器添加图片上传功能
1.ckeditor默认没有上传图片功能,只能通过Url显示图片,图下图 2.首先说明,ckeditor是有上传功能的,只是隐藏了,需要通过配置让它显示 找到ckeditor/plugins/imag ...
- Oozie框架基础
* Oozie框架基础 官方文档地址:http://oozie.apache.org/docs/4.0.0/DG_QuickStart.html 除Oozie之外,类似的框架还有: ** Zeus:h ...
- 验证备份集-使用DBVERIFY工具
DBVERIFY确认备份集是否存在坏块 验证TEST03.DBF 文件的是否存在坏块 C:\Documents and Settings\Administrator>dbv file=D:\or ...
- 使用goroutine+channel和java多线程+queue队列的方式开发各有什么优缺点?
我感觉很多项目使用java或者c的多线程库+线程安全的queue数据结构基本上可以实现goroutine+channel开发能达到的需求,所以请问一下为什么说golang更适合并发服务端的开发呢?使用 ...
- Bayes++ Library入门学习之熟悉UKF相关类
UKF-SLAM是一种比较流行SLAM方案.相比EKF-SLAM,UKF利用unscented transform代替了EKF的线性化趋近,因而具有更高的精度.Bayes++库中的unsFlt.hpp ...
- 关于 nginx 的配置的 location
精准匹配和普通匹配: server{ location =/index.htm{ ////精准匹 ...
- TCP服务器如何区分不同的用户
CS架构:使用SOCKET(一般是一个整数),当服务器侦听到连接请求的时候,accept会返回一个SOCKET(用于识别不同的连接,可以理解成,SOCKET是区分不同连接的ID).当服务器listen ...