Safety check

3.6

17 votes
Medium
Problem

It is a dangerous job for police escorting this confidential high profile criminal in the city of Mehirez. It is likely that the police escort vehicles will be attacked on their way. This city has N number of towns. From the satellite image the police has obtained, they have been able to predict the number of suspect vehicles with attackers on each main road connecting two towns. There are M main roads connecting the towns with each other. However not all town are connected via main road. The police just need to find out if they are currently in the group of connected towns with maximum number of suspect vehicles because the police cant handle the attack force and mission will fail. If the police are currently in town t, should they proceed with the escort mission?

INPUT:

First line is an integer T indicating the number of test cases.
For each test case, input two integers N and M followed by M lines.
Each line contains three integers a[i], b[i], c[i] representing the road between towns a[i], b[i] with number of suspect vehicles c[i].
Last input is an integer t representing the town the police are in for each test case.
(Each town is indexed 1 to N. So the inputs are integers)

OUTPUT:

"YES" if they can proceed. "NO" otherwise.

CONSTRAINTS:

1<=T<=10
1<=N,M<=100000
1<=c[i]<=10000
1<=t<=N


Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

7-2-5-4 are connected with no of suspect vehicles 17.
6-1-5 are connected with no of suspect vehicles 15.
Since police are now in town 4, they are in the connected group of towns with maximum no of suspect vehicles.
So they shouldn't proceed with the escort mission.

Editor Image

?