Binary graph and code1170 double stack sort

Source: Internet
Author: User

6.6 Two-part diagram

A binary graph is a graph with two vertex sets and two vertices of each edge of the graph in two vertices set, with no edges connected directly to each vertex set.

The sufficient and necessary condition for the two-point graph of the graph G is that G has at least two vertices and that all of its loops are of an even length.

The common method of judging the dichotomy is staining: begin to dye any one of the non-staining vertex, then judge its adjacent vertex, if not staining it will be infected with the adjacent vertices of different colors, if it has been dyed and the color and the adjacent vertices of the same color is not a binary picture, if the color is different then continue to judge, BFS and Dfs.

Easy to know: Any non-circuit diagram is a two-part diagram.

Code:

BOOL Color (int u)

{

for (int i=head[u]; ~i; i=edge[i].next)

{

int v = edge[i].to;

if (!col[v])

{

COL[V] =!col[u];

if (! Color (v)) return false;

}

Else if (Col[v] = = Col[u])

return false;

}

return true;

}

Codevs1170 Double Stack sort NOIP2008

Topic Analysis:

Analyzing the condition, we abstract the problem into a mathematical model. Set the input sequence to S, consider s[i],s[j] Two elements can not enter the same stack of conditions. Note that the "s[i],s[j" mentioned here "two elements can not enter the same stack", not only can not be in a stack at the same time, but can not enter a stack, that is, if there is a solution, then s[i],s [j] must have entered a different stack.

Conclusion P: s[i],s[j] Two elements cannot enter the same stack <=> exist K, satisfies i<j<k, makes S[k]<s[i]<s[j]. Proof Skip, please refer to Sqybi. After trying to find the conclusion P is correct.

Each element is numbered in the order of the input sequence and is treated as each vertex in a diagram. At this point, we are satisfied with all of the (I,J) I<j, determine whether the conclusion p, or s[i],s[j] Two elements can enter the same stack. If P is met, an edge is connected between the i,j.

We stain the graph, because there are only two stacks, the graph we get must be a binary graph to satisfy the condition. Because the dictionary order is required to be the smallest, that is, as far as possible to enter the stack 1, we are numbered in ascending order from each of the non-dyed vertex dyeing, adjacent vertices with different colors, if there is a conflict, is non-solvable. Otherwise we can get each vertex color, which is the stack that should be entered.

The next step is the output sequence, which knows the decision of each element and simulates it directly.

When judging whether or not a number pair (i,j) satisfies p, the enumeration checks if the time complexity of K is O (n), then the total time complexity is O (n^3), which is too large for n=1000. This is because there is too much enumeration of k, and we can use dynamic programming to turn the enumeration K into an O (1) algorithm.

Set F[i] to min{s[i],s[i+1],s[i+2]. S[n-1],s[n]}, the state transition equation is f[i]=min{s[i], f[i+1]}. The boundary is a f[n+1]= maximum value.

Determine whether the number pair (i,j) satisfies p, just judge (S[i]<s[j] and f[j+1]<s[i]). The time complexity is O (n^2).

Thanks to the puzzle, otherwise I can't think of the conclusion P ah ...

Simulation is also a problem:

Knowing the color (cc) of each point, it's easy to get into the stack. The question is when to get out of the stack.

Set A to the element that currently needs to be out of the stack, B is a+1

When the stack top of S1 is a, it is immediately out of the stack. Because the priority of the S1 is 2, only into the S1 higher than it, but then the elements should be out of the pressure, obviously not. Therefore, the direct out of the dictionary order to ensure the smallest.

When S2 's stack top is a, trouble comes. Because the priority of the S2 is lower than the S1, so if it is possible to first enter the S1, put the element in the S2 regardless of it, until forced to go out.

S2 out of the stack: S2 's stack top is a, cc[i+1]==2 | | (Cc[i+1]==1&&!s1.empty () &&s1.top () ==b) | | S1.empty ()

Note that the cc[n+1]=2 is not subject to the stack limit (cc[i+1]==2) until the stack is fully completed, prior to the simulation.

See the code:

#include <iostream>#include<stack>using namespacestd;Const intSize=1005;intN,data[size];intF[size];intCc[size];BOOLflag=true;intA=1, b=2; Stack<int>s1,s2;//Edgestructe{intto ; E*next;}*Head[size];voidAddintAintb) {E*p=NewE; P->to=b; p->next=Head[a]; Head[a]=p;}voidColorintu) {     for(e* i=head[u]; i!=null; i=i->next) {          intv = i->to ; if(!Cc[v]) {Cc[v]= cc[u]==1?2:1;            Color (v); if(!flag)return; }          Else if(Cc[v] = =Cc[u]) {Flag=false; return; }    }  }void  out(inti) {    BOOLok=true;  while(OK) {OK=false; if(!s1.empty () &&s1.top () = =a) {S1.pop (); cout<<"b"; a++;b++; ok=true; }        if(!s2.empty () &&s2.top () = =a) {            if(cc[i+1]==2|| (cc[i+1]==1&&!s1.empty () &&s1.top () ==b) | |S1.empty ()) {S2.pop (); cout<<"D"; a++;b++; ok=true; }        }    }}intMain () {CIN>>N;  for(intI=1; i<=n;i++) head[i]=NULL;  for(intI=1; i<=n;i++) {cin>>Data[i]; } f[n+1]=0x3f3f3f3f;  for(inti=n;i>=1; i--) {F[i]=min (data[i],f[i+1]); }         for(intI=1; i<=n;i++){         for(intj=i+1; j<=n;j++){            if(data[i]<data[j]&&f[j+1]<Data[i])            {Add (i,j); add (j,i); }        }    }         for(intI=1; i<=n;i++){        if(cc[i]==0) {Cc[i]=1;            Color (i); if(!flag) Break; }} cc[n+1]=2; if(flag) { for(intI=1; i<=n;i++){            if(cc[i]==1) S1.push (Data[i]),cout<<"a"; ElseS2.push (Data[i]),cout<<"C";  out(i); }    }    Elsecout<<0; return 0;} 

Binary graph and code1170 double stack sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.