bzoj1194 [HNOI2006] Pandora's Box

http://www.elijahqi.win/archives/3189
Description

According to legend, there is a magical Pandora's box. If anyone can open it, they can have happiness, wealth, and love. However, it was not until it was really opened that it was discovered that there were disasters and misfortunes
accompanying . In fact, when Pandora made this treasure box, some spells were set up to block disasters and misfortunes. However, it is not until today, when
technology is highly advanced, that people can hope to understand these spells. Therefore, for thousands of years, people had to endure the
pain . However, the fate of mankind has since changed. After decades of research, the NOI organization has finally figured out how Pandora's spell works. Spells
are produced by a machine called a spell machine. Explained in terms of current terms, the spell machine is actually a binary generator, and a binary string (this string is called a spell source) generated by
it is encrypted to form a spell. The structure of the binary generator is as follows: it consists of n elements, and
the labels of these n elements may be set from 0 to n-1. At each moment, there is one and only one signal, which stays on a certain element. A signal is a
binary string. Initially, there is an empty string signal staying on element 0. At a certain moment, if there is a signal s staying on the component I,
then the component i can add a 0 after the signal, and then pass the signal to the component pi, 0, or add a 1 after the signal, and then Pass it to the element pi,1.
That is to say , the next moment is possible, one may be a signal S0 (representing a string formed by adding a 0 after the string S) on the element pi,0, and the other
may be a signal S1 staying on element pi,1 on. Some components can output the signal that stays on it, and the output signal becomes the
source , such a component is called the output element of the spell source. It is not difficult to find that some spoken sources may be produced by a spell machine, while others are not.
For example, the spell function in the figure below can generate spell sources such as 1, 11, 111, 1111, ..., etc., but cannot generate spell sources such as 0, 10, 101. On this box, there are K
spell machines, which may be numbered from 0 to K-1. It may be the case that a spoken language source that can be produced by a spell machine i can be produced by a spell machine j.
At this time, we call spell machine j an upgrade of spell machine i. And one way to gauge the complexity of this example is to look at
the . That is: find a longest upgrade sequence a1, a2...at. The upgrade sequence satisfies: the labels of any two spell machines in the sequence are different,
and both are integers between 0 and k-1 (including 0 and k-1), and the spell machine a2 is an upgrade of the spell machine a1, the spell Machine a3 is an upgrade of spell machine a2... and spell
machine at is an upgrade of spell machine at-1. Do you want to stay away from disaster and misfortune? Do you want to bathe in the sunshine of happiness from now on? Please open your Pandora's box.
But before arching it, you have to calculate the longest upgrade sequence on the chest.

Input

The first line is a positive integer S, which represents the number of spell machines on the treasure box, (1≤S≤50).
The file is divided into S blocks below, each block describes a spell machine, in the order of spell machine 0, spell machine 1...spell machine S-1.
The format of each block is as follows.
The first row of a block has two positive integers n, m. Respectively represent the number of components in the spell machine and the number of output elements of the spell source
(1≤m≤n≤50).
The next line has m numbers, representing the labels of the m spell source output elements (all between 0 and n-1).
Next there are n lines, each with two numbers. The two numbers in row i (0≤i≤n-1) represent pi,0 and pi,1
(both between 0 and n-1, of course).
Output

The first line has a positive integer t, which represents the length of the longest escalation sequence.

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

The meaning of the question: to judge whether the string that can be output by one automaton can be output by the other. If so, it is considered to be contained in another string. How to do violence is exponential, then wide search because the state of wide search is n^2 The complexity of the level is n^2
. Let {x, y} this two-tuple represent the x position of the a automaton. The y position of the b automaton is judged. If x is the exit and y is not, just exit directly.
Otherwise when all the After all the states are searched and considered to be included,
then violently enumerate each pairing situation and each is violently bfs verified. If it is included, build an edge. Because there is a ring, first tarjan and then topology dp to find the longest chain.

#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int M=55;
struct Auto_M{
    int n,m,trans[M][2],end[M];
}am[M];
struct node{
    int y,next;
}data[M*M],data1[M*M];
struct node1{int x,y;};bool stackf[M];
int h1[M],h[M],b[M],s,T,in[M],num,dfn[M],low[M],q[M],top,size[M],dp[M];
inline bool bfs(const Auto_M &a,const Auto_M &b){
    static node1 q[100000];static int l,r;l=1;r=0;
    static bool visit[M][M];memset(visit,0,sizeof(visit));
    q[++r]=(node1){0,0};visit[0][0]=1;
    while(l<=r){static int x,y,xx,yy;
        node1 lt=q[l++];x=lt.x;y=lt.y;
        if(a.end[x]&&!b.end[y]) return 0;
        for (int i=0;i<2;++i){
            xx=a.trans[x][i],yy=b.trans[y][i];
            if(visit[xx][yy]) continue;
            visit[xx][yy]=1;q[++r]=(node1){xx,yy};
        }
    }return 1;
}
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;
}
inline void insert2(int x,int y){
    data1[++num].y=y;data1[num].next=h1[x];h1[x]=num;
}
inline void tarjan(int x){
    low[x]=dfn[x]=++num;stackf[x]=1;q[++top]=x;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if(!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if (stackf[y]) low[x]=min(low[x],dfn[y]);
    }
    if (dfn[x]==low[x]){
        ++s;int y;
        do{y=q[top--];b[y]=s;++size[s];stackf[y]=0;
        }while(x!=y);
    }
}
int main(){
    freopen("bzoj1194.in","r",stdin);
    T=read();
    for (int i=1;i<=T;++i){
        am[i].n=read();am[i].m=read();
        for (int j=1;j<=am[i].m;++j) am[i].end[read()]=1;
        for (int j=0;j<am[i].n;++j) am[i].trans[j][0]=read(),am[i].trans[j][1]=read();
    }
    for (int i=1;i<=T;++i){
        for (int j=1;j<=T;++j){
            if (i==j) continue;
            if(bfs(am[i],am[j])) insert1(i,j);//printf("%d %d\n",i,j);
        }
    }num=0;queue<int>q;
    for (int i=1;i<=T;++i) if (!dfn[i]) tarjan(i);num=0;
    for (int x=1;x<=T;++x){
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;if (b[x]==b[y]) continue;
            ++in[b[y]];insert2(b[x],b[y]);
        }
    }for (int i=1;i<=s;++i) if (!in[i]) q.push(i),dp[i]=size[i];
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h1[x];i;i=data1[i].next){
            int y=data1[i].y;if (!--in[y]) q.push(y);
            dp[y]=max(dp[y],dp[x]+size[y]);
        }
    }int ans=0;
    for (int i=1;i<=s;++i) ans=max(ans,dp[i]);
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324793767&siteId=291194637