AKBAR - Akbar , The great

All of us are familiar with the reign of the great mughal ruler, Akbar. He was always concerned with the prosperity and safety of the people. Therefore to safeguard his kingdom (which consisted of N cities) he wanted to place secret soldiers all over his kingdom so as to protect the people. But since his kingdom is very large therefore he wanted to place them in such a way that every city is protected by one and only one soldier.According to Akbar, this is the optimum placement.

As for these soldiers they can protect multiple cities according to their strengths.

The strength of a particular soldier is defined as the maximum distance upto which a guard can protect a city from its base city(base city is the city assigned to the guard). If there are 3 cities C1, C2 and C3 such that C1 C2 and C2 C3 are connected respectively, if a soldier with strength 1 is placed at C2 then all the cities C1, C2 and C3 are protected by that soldier.

Also the kingdom is connected with a network of secret two way roads for faster access only accessible to these soldiers. The length of any road on this network between any two cities is 1 kms. There are R such roads in the kingdom. 

He had given this task to birbal to place the soldiers. Birbal didn't wanted to be a fool in front of the king, therefore took the job and placed M soldiers all over the kingdom but he was not very good at mathematics. But since he is very intelligent he somehow places the guards all over the kingdom and now turns to you (who is a genius mathematician ;) ) to check whether his placements are good or not.

Your task is to check if the placements of the soldiers are optimum or not.

INPUT

The input consists of T test cases. Each test case then consists of 3 parts.The first line consists of N, R and M.

the next R lines consists of two numbers A and B denoting the two cities between which a road exists.

the next M lines consists of 2 numbers, city number K and strength S of that particular soldier.

=> strength 0 means it will only guard the city on which it is present.

=> assume every city is accesible from every other city.

CONSTRAINTS

T <= 10;

1 <= N <= 10^6;

N - 1 <= R <= min(10^7, (N * (N - 1) ) / 2));

1 <= K <= N;

0 <= S <= 10^6

OUTPUT

print "Yes" if the soldiers are placed optimally else print "No", (quotes are for clarity.)

SAMPLE

INPUT
2
3 2 2
1 2
2 3
1 2
2 0
4 5 2
1 4
1 2
1 3
4 2
3 4
2 1
3 0

OUTPUT
No
Yes

WARNING ==> Large input.


Added by:Prayank Mathur
Date:2014-10-12
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:own

hide comments
2020-06-06 17:05:43
cycle!!!
2020-04-26 23:24:54
the question is framed in such a way that it might be confusing.
In tc1, 1 and 2 are guarded and 3 is not.
2020-04-08 16:41:11
O(M*(V+E)) gives TLE where V-no.of vertices /E-no.of edges/M-no. of city point soldier strength
please give constrain on (M) though its needed
better say multiple bfs fail

Last edit: 2020-04-08 16:52:28
2020-03-20 09:08:34
Take care of cycle !!!
2020-02-22 16:25:16
Please help, I have tried all test cases in the comments correctly, but still getting wrong answer on submission: 25439183
2020-02-01 07:45:47
poorly worded, took my one whole day.
2020-01-22 06:04:31
AC in two go
2019-12-13 15:23:39
dont knoww what i am doing wrong my submission id is 25062209
2019-11-04 05:38:31
use multiple bfs depending on where the soldier is placed.
AC in two go!!

Last edit: 2019-11-04 05:38:54
2019-10-13 20:14:12
It was fun! Question is not poorly worded. Read the statement carefully "every city" is protected by "one and only one" soldier. Also take care of TLE, you don't need to clear your visited array every time.

Last edit: 2019-10-13 20:14:46
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.