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.


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.


T <= 10;

1 <= N <= 10^6;

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

1 <= K <= N;

0 <= S <= 10^6


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


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


WARNING ==> Large input.

hide comments
murtazasaify: 2023-03-11 06:35:50

Can anyone help me to solve I am getting WA at last case:
[Simes]: Read the footer - Don't post source code here, use the forum.

Last edit: 2023-03-11 10:11:52
sarthak_comder: 2023-02-07 17:37:55

If you are getting WA try this test case
6 6 1
1 2
1 3
2 4
3 5
4 6
5 6
1 3
ans - YES

really_fake: 2022-12-02 07:34:21

I didn't get the first test case.. Please anyone explain it.

wvernaschi: 2022-11-20 22:06:33

-> Each guard MUST protect his own city
-> A guard is allowed to protect less cities than his power level allows
-> All cities must be protected

Last edit: 2022-11-20 22:19:01
shivanshut105: 2022-11-08 16:55:25

output 'Yes' only not 'YES'

anupam_akib: 2022-07-10 10:07:25

Good problem. Solved it using DFS. You have to keep track of parent node and handle the cycle correctly.

sk214: 2022-06-27 21:12:34

Do not glorify invaders. They were murderers and rapists.

Last edit: 2022-06-27 21:18:46
spirit1: 2022-05-29 06:48:45

I am getting TLE, I am applying bfs at every city with a soldier and if I find a city with 2 soldiers I break and print NO immediately. what is wrong here?

sankalp_7: 2022-04-21 16:04:15

Take care of cycles :)

Anna Szybalska: 2022-03-27 13:04:25

Anyone managed to fit into time limits in C#?

Added by:Prayank Mathur
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)