#K79612. Taco: Can Visit All Cities?

    ID: 35348 Type: Default 1000ms 256MiB

Taco: Can Visit All Cities?

Taco: Can Visit All Cities?

You are given a directed graph with n cities labeled from 1 to n and m one-way roads. Each road connects city u to city v. Starting from a given city s, your task is to determine whether it is possible to visit every city by following these roads without visiting any city more than once.

In other words, check if all cities are reachable from the starting city s in the directed graph. Note that the condition "without revisiting" implies that you can only count the first visit to each city. Thus, the problem reduces to verifying whether the graph is s-connected.

Hint: You can solve this problem by performing a breadth-first search (BFS) or depth-first search (DFS) starting from city s and checking if all cities are visited. Mathematically, if the set of reachable nodes R(s) satisfies

R(s)=n,|R(s)| = n,

then output YES; otherwise, output NO.

inputFormat

The input is read from stdin and contains multiple lines:

  • The first line contains three integers: n, m, and s — the number of cities, the number of roads, and the starting city respectively.
  • The following m lines each contain two integers u and v, representing a directed road from city u to city v.

outputFormat

Output a single line to stdout containing YES if all cities are reachable from city s, and NO otherwise.

## sample
5 5 1
1 2
2 3
1 3
3 4
4 5
YES