YCOJ one-way road

topic:

description

 

There are many towns in an area, but not every town has road connections related to other towns, and some roads are not two-way traffic. Now the road between the towns distribution and permissible driving directions tell you that you need to address whether the program can be reached from one town to another town by road. (We require cities and towns with their own can reach each other, that is a reachable a).

 

Entry

 

The first row is only a few N, 2N lines will follow the data.

N previous line data, a beginning of each line data of the first digital number, this line indicate a total number of number, the next number of the number i, the representative number of towns i. This line is to have the rest of the highway linking the town with a list of i, and only towards the town from the other towns i. As 4123, showed that: This line number 4, with 1 urban roads in towns towns linked are numbered 2 and 3, and only allows 1 from town towards towns 2 and 3, but not from 2 to 1 or 3-1.

After the data N rows, each row consisting of two numbers a, b.

0≤input_number≤1000 have the following relationship for each number entered.

 

Export

 

For each of the input data a, b, determines whether a town by town road to reach from B, and if so, output Yes; otherwise outputs No.

 

Sample input 1 

3
4 1 2 3
3 4 5
3 5 8
1 2
1 8
4 8

Sample Output 1

Yes 
No 
Yes 


This is a classic search problem with BFS or DFS will do, because bloggers DFS has been playing very well, ready to challenge yourself, so I'll say something about BFS practice.

As shown, because the road is one-way, although all points are connected so obtained point 2, point 2 but did not get to a point, so it is a directed graph.


First, let's define a wave:
int n;
queue<int>q;
vector<int>g[10100];

Here there is no need to speak of it, the definition bfs essential vector and queue so it was.


 

Next, we solve the input and output, the input is particularly weird. N has 2n input row, the first n rows will first enter a number, it is representative of this line plus the total number of number; the number of 2 n lines each, a query asking whether a and b are in communication book.

    cin>>n;
    for (int i=1;i<=n;i++){
        int tmp1,tmp2;
        cin>>tmp1>>tmp2;
        for (int j=1;j<tmp1-1;j++){
            int x;
            cin>>x;
            g[tmp2].push_back(x);
        }
        
    }

 

Then output, if the type bool value is either true or equal bfs and a B, on the output Yes, otherwise, outputs No.

    for (int i=1;i<=n;i++){
        int a,b;
        cin>>a>>b;
        if(bfs(a,b)==true||a==b){
            cout<<"Yes"<<endl;
        }
        else {
            cout<<"No"<<endl;
        }
    }

Finally, we start the BFS!

First of all, the starting point to push queue.

bool bfs(int start,int end){
    q.push(start);
}

Then start BFS routine: while (q.empty ()!)

Next, the current position is taken out (now), then pop the queue.

Finally, traversing the g [now], if reached the end, then return ture, otherwise it will point found on the queue. If the queue have been emptied, it can only regret to tell you, he was not found. No the only output.

Whole BFS:

bool bfs(int start,int end){
    
    q.push(start);
    while (!q.empty()){
        int now=q.front();
        q.pop();
        for (int i=0;i<g[now].size();i++){
            if(g[now][i]==end){
                return true;
            }
            q.push(g[now][i]);
        }
    }
    return false;
}

Complete code:

#include <bits/stdc++.h>
using namespace std;
int n,vis[10100];
queue<int>q;
vector<int>g[10100];

bool bfs(int start,int end){
    
    q.push(start);
    while (!q.empty()){
        int now=q.front();
        q.pop();
        for (int i=0;i<g[now].size();i++){
            if(g[now][i]==end){
                return true;
            }
            q.push(g[now][i]);
        }
    }
    return false;
}

int main(){
    cin>>n;
    for (int i=1;i<=n;i++){
        int tmp1,tmp2;
        cin>>tmp1>>tmp2;
        for (int j=1;j<tmp1-1;j++){
            int x;
            cin>>x;
            g[tmp2].push_back(x);
        }
        
    }
    for (int i=1;i<=n;i++){
        int a,b;
        cin>>a>>b;
        if(bfs(a,b)==true||a==b){
            cout<<"Yes"<<endl;
        }
        else {
            cout<<"No"<<endl;
        }
    }
    return 0;
}

 Finally, Accept given to everyone!

 

Guess you like

Origin www.cnblogs.com/herobrine-life/p/10959958.html