#C6339. Two Hub Cities
Two Hub Cities
Two Hub Cities
In a kingdom, there are N cities connected by M bidirectional roads. The king is planning to designate two of these cities as critical hubs. The requirement is that every other city in the kingdom must be reachable (either directly or through other cities) from at least one of these two selected hub cities.
This condition can be reinterpreted as follows: Is there a city v such that if it is removed from the network, the remaining N-1 cities still form a connected graph? If yes, then we can always choose v along with an appropriate adjacent city to cover all other cities.
Your task is to determine whether such a city exists. If it does, print YES
; otherwise, print NO
.
Note: The cities are numbered from 1 to N.
inputFormat
The first line contains two integers N and M, where N is the number of cities and M is the number of roads.
The next M lines each contain two integers u and v, indicating that there is a bidirectional road connecting city u and city v.
outputFormat
Output a single line containing YES
if there exists a city whose removal leaves the remaining cities connected. Otherwise, output NO
.
5 4
1 2
1 3
1 4
4 5
YES