# [Swust OJ 85]--One Way Road (BFS)

Source: Internet
Author: User

Time limit (ms): Memory Limit (KB): 65535 Description

There are many towns in a certain area, but not every town has a road connection with other towns, and not all roads can travel in two directions. Now let's tell you about the road distribution between these towns and the permissible driving directions, and you need to program to solve whether the road can be reached from one town to another. (We stipulate that towns and cities themselves can reach each other, i.e. a can reach a).

Input

The first line has only one number n, followed by 2N rows of data. In the first n rows of data, for each row of data, the number that starts at the beginning of the line indicates that there is a total number of numbers in this line, and the next number of numbers is I, which represents the town with the number I. The remainder of this line is the list of towns with road links to I, and can only sail from town I to other such as 4 1 2 3, indicating that: this line has 4 numbers, towns 1 with roads connecting towns are numbered 2 and 3 towns. are from 1 to 2 and 3, not from 2 and 3 to 1. In the latter row of data, each row consists of two numbers, a, a, a, and a (representing the town's number). For each input the number has the following relationship 0 <= input_number <= 1000.

Output

For each B in the input data, determine whether the town B can be reached from town a by road, and if so, output yes;

Sample Input

 12345678 34 1 2 33 4 53 5 81 21 84 8
Sample Output
 123 Yesnoyes
Problem solving idea: the direct bfs~~~ code after processing good data is as follows:
`1#include <iostream>2#include <queue>3#include <cstring>4#include <algorithm>5 using namespacestd;6 intmpt[1001][1001], vis[1001];7 intBFsintXintYintN) {8queue<int>Q;9 Q.push (x);Ten      while(!Q.empty ()) { One         intnow =Q.front (); A Q.pop (); -         if(now = = y)return 1; -          for(inti =0; I < n; i++){ the             if(!vis[i] &&Mpt[now][i]) { -Vis[i] =1; - Q.push (i); -             } +         } -     } +     return 0; A } at intMain () { -     intN, t, x, Y, I, j, Maxn =-1; -CIN >>T; -      for(i =0; I < T; i++){ -CIN >> N >>x; -          for(j =0; J < N-2; J + +){ inCIN >>y; -Mpt[x][y] =1; toMAXN = Max (MAXN, Max (x, y) +1);//Find out the maximum number of points that occur +1, avoid large-scale search for unnecessary points +         } -     } the      for(i =0; I < T; i++){ *memset (Vis,0,sizeof(Vis)); \$CIN >> x >>y;Panax Notoginsengcout << (BFS (x, y, MAXN)?"yes\n":"no\n"); -     } the     return 0; +}`
View Code

There used to be a code, personal feeling no problem, but has been rutime Error, there is a big God passing for a reason it ~ ~ ~

The code is as follows:

`1#include <stdio.h>2#include <string.h>3 #defineMAXN 100014 intMAP[MAXN][MAXN], Max;5 intDfsintStarintnext)6 {7     intI, flag, VIS[MAXN];8memset (Vis,0,sizeof(Vis));9Vis[star] =1;Ten     if(Map[star][next] = =1) One         return 1; A      for(i =1, flag =0; I <= max&&!flag; i++) -     { -         if(Vis[i] = =0&& Map[star][i] = =1) the         { -Vis[i] =1; -Flag =DFS (i, next); -Vis[i] =0; +         } -     } +     returnFlag; A } at intMain () - { -     intI, J, M, N; -     intstar, Next; -scanf"%d", &n); -memset (Map,0,sizeof(map)); in      for(i =0; i<maxn; i++) -Map[i][i] =1; toMax =0; +      for(i =1; I <= N; i++) -     { thescanf"%d%d", &m, &star); *         if(star>max) \$Max =Star;Panax Notoginseng          for(j =1; J <= M-2; J + +) -         { thescanf"%d", &next); +             if(Next >max) AMax =Next; theMap[star][next] =1; +         } -     } \$      for(i =1; I <= N; i++) \$     { -scanf"%d%d", &star, &next); -         if(Dfs (star, next)) theprintf"yes\n"); -         ElseWuyiprintf"no\n"); the     } -     return 0; Wu}`
View Code

[Swust OJ 85]--One Way Road (BFS)

Related Keywords:

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.