noip2014 looking for road _noip to improve the group

Source: Internet
Author: User

Topic description

In the direction of G, each edge has a length of 1, now given the starting point and end point, please find a path from the beginning to the end point in the diagram, the path satisfies the following conditions:

1. The points pointed to by the edges of all points on the path are directly or indirectly connected to the endpoint.

2. The shortest path is the case when the condition 1 is satisfied.

Note: There may be a heavy edge and a self loop in the graph G, and the topic guarantees that the end point is not out.

Please output the length of the path that meets the criteria. Input output Format input format:

The input file name is road. In.

The first line has two integers n and m separated by a space, indicating that the graph has n dots and m bars.

The next m row is 2 integers x, y in each row, separated by a space, indicating that there is an edge from point x to point Y.

The last line has two integer s and T separated by a space, which means that the starting point is S and the end point is T.

Output format:

The output file name is road. Out.

The output has only one row and contains an integer that represents the length of the shortest path that satisfies the ᧿ of the topic. If such a path does not exist, output-1.

Input and Output sample input sample #:

3 2  
1 2  
2 1 1 3  
  
Output Sample #:
-1
Enter Sample for example:
6 6  
1 2  
1 3  
2  
6  
2 5 4 5 3 4-1 5  
Output Sample:
3
Description

Explanation 1:

As shown in the figure above, the arrows indicate a path, and the dots represent the city. Starting point 1 is not connected with endpoint 3, so satisfying the problem

The path mentioned in ᧿ does not exist, so output-1.

Explanation 2:

As shown in the figure above, the path that satisfies the condition is 1->3->4->5. Note that point 2 cannot be in the answer path because point 2 is connected to a side to 6, and point 6 is not connected to end 5.

For 30% of the data,0<n≤10,0<m≤20;

For 60% of the data,0<n≤100,0<m≤2000;

For 100% of the data, 0<n≤10,000,0<m≤200,000,0<x,y,s,t≤n,x≠t.

I'm afraid of my own Orz Orz o| ̄|_.

The first time to do it is a DFS to determine if a point is not all even points can be successful at the end of the wa9 point Orz,

Because of the DFS you gave to vis[x]=true when you found the X, and then it was still false that the label that did not match the requirement has not been updated

Then you go to the point where it's connected, but if it's a ring, it's connected to it and then it's all set to false qaq.

This method of flapping the street


Then you need to change the train of thought, since the requirements are to the end, then only to build the opposite side, from the end of the search back, record a point of access to the number of times,

If the margin of a point is equal to the number of accesses in the reverse DFS search the description satisfies the condition, then the f[i]=true;

At the same time we want to delete the point of 0, the end will also be deleted, so update the f[i] array after the individual f[endpoint]=true

So what are some of the points that we can pick up in the shortest way?

And then we build the right side and we can run SPFA.

Attention:

Be sure to delete the opposite side after the construction of the positive side, or SPFA will hang (because it will go to the opposite side of the original does not exist ...) )

Adjacent table Delete Edge method is to clear 0 last array and the number of edges L clear 0

The output of attention to 1 of the judgment on the OK =w=

Because I did not delete the opposite side to kneel qaq

var n,m,l,x,y,s,t:longint;
        I:longint;
        QUE,DIS:ARRAY[0..10010] of Longint;
        LAST,OUTDU,TIME:ARRAY[0..10010] of Longint;
        XX,YY:ARRAY[0..200010] of Longint;
        VIS,FLAG:ARRAY[0..10010] of Boolean;
PRE,OTHER:ARRAY[0..400010] of Longint;
Procedure connect (X,y:longint);
   Begin Inc (L);
   PRE[L]:=LAST[X];
   Last[x]:=l;
Other[l]:=y;

End
Procedure Dfs (X:longint);
var p,q:longint;
   Begin Q:=LAST[X];
   Vis[x]:=true;
      while (q<>0) do begin P:=OTHER[Q];
      Inc (Time[p]);
      If not vis[p] then DFS (p);
   Q:=PRE[Q];
End

End
Procedure SPFA;
var p,q,cur,h,tl:longint;
   Begin Fillchar (vis,sizeof (VIS), false);
   Fillchar (dis,sizeof (DIS), 127);
   h:=0;tl:=1;que[1]:=s;dis[s]:=0;
      while (H&LT;&GT;TL) does begin h:=h mod 10000+1;
      CUR:=QUE[H];
      Vis[cur]:=false;
      Q:=last[cur];
       while (q<>0) do begin  P:=OTHER[Q];
            if (dis[p]>dis[cur]+1) and flag[p] then begin dis[p]:=dis[cur]+1;
               If not vis[p] then begin vis[p]:=true;
               TL:=TL MoD 10000+1;
            Que[tl]:=p;
         End
         End
      Q:=PRE[Q];
   End
End

End
   Begin read (N,M);
      For I:=1 to M do begin read (x,y);
      Inc (Outdu[x]);
      Xx[i]:=x;yy[i]:=y;
   Connect (y,x);
   End
   Read (s,t);
   DFS (t);
   For I:=1 to n do if (Outdu[i]=time[i]) and (outdu[i]<>0) then flag[i]:=true; 
   Flag[t]:=true;
      If not Flag[s] then Writeln ( -1) else is begin for I:=1 to n do last[i]:=0;l:=0;
      For I:=1 to M do Connect (Xx[i],yy[i]);
      SPFA;
   If dis[t]=dis[0] then Writeln ( -1) Else Writeln (dis[t));
End
 End.
--by Eirlys

Reprint please indicate the source =w=


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.