[codevs5578] [Salted fish]tarjan/concluding question

Source: Internet
Author: User

5578 Salted Fishtime limit: 1 sspace limit: 128000 KBTitle Description Description

In the vast square of land there are n horizontal rivers and M vertical rivers, the developed family of salted fish in the M*n River intersection of the establishment of the city. However, because the river has a single flow, and the salted fish do not have developed the lower body, so only downstream. 22 The flow between rivers does not affect each other.

Now, the salted fish adventurer Sorey decided to set out to see the world, but Sorey worried that he might be trapped in a city and unable to return to his hometown. So Sorey night to see the sky, to figure out the flow of each river, he would like to ask you to help him to determine whether his journey will be smooth.

Enter a description Input Description

First line an integer T representing the number of data groups (T<=10)

The first row of each group of data two integers n,m, meaning as above

The second row, two integers, x, y, represents the initial coordinates of the Sorey (from the beginning, X for the row, and Y for the column)

The next line n number, with a space between, where I is the direction of the horizontal river, 0 left, 1 right

The next line is the number of M, separated by a space, where I represents the direction of the vertical river of article I, 0 downward, 1 upward

Output description Output Description

A set of data rows

If Sorey can reach any city and cannot be trapped, output "Yes.", otherwise output "No." (without quotation marks)

Sample input Sample Input

1

4 6

1 1

0 1 0 1

0 1 0 1 0 1

Sample output Sample Output

Yes.

Data range and Tips Data Size & Hint

Sample Example

For 40% of data n,m<100,t=1;

For 60% of data n,m<1000;

For 100% of data 2<=n,m<=1000000,1<=x<=n,1<=y<=m;

Very large data, attention to read-in optimization

Analysis:

"Salted fish do not have developed the lower body" and yes behind the full stop, Groove point or quite a lot of. The data range is very large, should be the conclusion question

Corollary 1:

"But Sorey worried that he might be trapped in a city and not return to his hometown" and "if Sorey can reach any city and can't be trapped, output" Yes. ", otherwise output" No. " (without quotation marks) ", obviously the topic requires us to determine whether we can from the source point S to any point and can be from any point back to the source point S, that is, any two points i,j to each other (I to S, and then S to J), so the requirement of the original is a strong connected component. The Tarjan algorithm can be used to judge. But the complexity of O (nm), only 60 points.

Corollary 2:

We have to optimize to O (n) or below, consider a special sentence, look at the sample found: Only the outermost is a ring must be satisfied, because from the s along the edge can certainly go to the periphery, the outside circle to each side of the entrance can go in.

Corollary 3:

Adequacy to the proof, just complete: as long as the periphery is not a ring, then the four corners must be in the corner of the inevitable can not be a strong connected components.

Now only need to judge the periphery is not the ring, the Complexity O (1), but the data is too large, to optimize the read, you can skip the middle section with Fseek, with GetChar () can also be (lazy I use the latter, Complexity O (n+m))

1#include <cstdio>2InlineintRead () {3     CharC=GetChar ();4      while(c!='0'&&c!='1') c=GetChar ();5     returnC-'0';6 }7 intMain () {8     intn,m,x,y,a,b,c,d,t;9scanf"%d",&T);Ten      while(t--){ Onescanf"%d %d%d%d",&n,&m,&x,&y); AA=read (); n-=2; -          while(n--) read (); -B=read (); C=read (); m-=2; the          while(m--) read (); -D=read (); -         if((a==0&&b==1&&c==0&&d==1)|| -(a==1&&b==0&&c==1&&d==0)) printf ("yes.\n"); +         Elseprintf"no.\n"); -     } +     return 0; A}

[codevs5578] [Salted fish]tarjan/concluding question

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.