Magical road

Topic background

Under compulsion of a coach, I'm a konjac actually has a question for our! ! ! Out of the question! ! ! (I do not find too much water data qwq)

Good, JL said that a good title brought 100 fast

Title Description

We have a directed graph, each edge weight of 1, we have to find a magic way to meet the above:

All points on the side of the road 1. The magic points and end points are communicating directly or indirectly.

2. In the case that the shortest path condition 1 is satisfied.

3. The path is a path from the origin to the destination

Garbage out of the question said they can not find this path, so I hope you help qwq

Input Format

The first two rows separated by a space of the integers n and m, there is a view showing the n points and edges m.

The next two lines each m integers x, y, between separated by a space, there is an edge point represents a point y from the point x.

The last line has two integers separated by a space s, t, expressed as a starting point s, the end point as t.

Output Format

Only output line, comprising an integer representing the length of the shortest path to meet the described subject. If such a path does not exist, the output "orz" (without quotation marks)

Sample input and output

Input # 1

3 2  
1 2  
2 1  
1 3  

Output # 1

orz

Input # 2

7 8
5 2
3 1
5 3
1 6
3 6
6 7
2 7
2 4
5 7

Output # 2

3

Description / Tips

For a sample: a start point and end point 3 is not in communication, the subject is satisfied described path does not exist, so the output orz.

data range

FIG communication is guaranteed 30%

100% 0<n≤10000,0<m≤200000,0<x,y,s,t≤n,x,s≠t。

 

 

Cancer morning the topic and told me to run up the road 60 minutes. . . . . .

 

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int tot,total,n,m,ss,tt,l[500050],r[500050],pre[500050],last[10050],other[500050];

int that [ 10050 ] to [ 10050 ];

bool judge[10050],vis[10050],point[10050];

inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            w=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}

void add(int u,int v) {
    pre [ ++ to] = load [u];
    last [u] = up;
    other[tot]=v;
}

void bfs(int x) {
    int h=0,t=1;
    to [ 1 ] = x;
    am [x] = 1 ;
    point[x]=1;
    total++;
    while (h<t) {
        int cur=que[++h];
        for (int p=last[cur]; p; p=pre[p]) {
            int q=other[p];
            if (!vis[q]) {
                to [q] = 1 ;
                that [ ++ t] = q;
                total++;
                point[q]=1;
            }
        }
    }
}

void spfa(int x) {
    int h=0,t=1;
    to [ 1 ] = x;
    memset(d,127,sizeof d);
    d[x]=0;
    while (h<t) {
        int cur=que[++h];
        vis[cur]=0;
        for (int p=last[cur]; p; p=pre[p]) {
            int q=other[p];
            if (!point[q]) continue; 
            if (judge[q]) continue;
            if (d[q]>d[cur]+1) {
                d [m] = d [why] + 1 ;
                if (! force [g]) {
                    to [q] = 1 ;
                    that [ ++ t] = q;
                }
            }
        }
    }
}

int main () {
    n=read();
    m=read();
    for (int i=1; i<=m; i++){
        l[i]=read();
        r[i]=read();
    }
    ss=read();
    tt=read();
    for (int i=1; i<=m; i++) add(r[i],l[i]);
    bfs(tt);
    if (!point[ss]) {
        printf("orz\n");
        return 0;         
    }
    for (int i=1; i<=n; i++) {
        if (point[i]) continue;
        for (int p=last[i]; p; p=pre[p]) {
            int q=other[p];
            judge[q]=1;
        }
    }
    memset(que,0,sizeof que);
    memset(vis,0,sizeof vis);
    memset(last,0,sizeof last);
    tot=0;
    for (int i=1; i<=m; i++) add(l[i],r[i]);
    spfa(ss);
    printf("%d",d[tt]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11357179.html