Ambulance to Rescue

4.1

7 votes
Problem

There are N registered properties numbered from 1 to N in the fictitious city. Out of these, S properties are registered as Hospitals - each of them providing ambulance service for emergency. Moreover, these properties are connected through M number of express highways (bidirectional).

At an instance you are given a list of properties where ambulances are required (some accidents maybe).

Any hospital can send ambulance to any property (that is if there are some highways which connect them). You have to find minimum total number of express highways traveled by different ambulances.

Input Format

First line contains N, M, S, E - number of properties, number of express highways, number of hospitals and number of emergency spots.

Next M lines contains two integers u and v - indicating that there is a express highway between u and v.

Next line contains S space separated distinct integers denoting the hospitals.

Next line contains E space separated integers denoting the emergency spots. These spots may not be distinct, that is they can be repeated (may be multiple ambulances are required).

Output Format:-

Only one line containing the minimum total number of express highways taken. If there exists at least property (from the E emergency sites), where no ambulance can reach, print -1.

Note :-

An ambulance can be used only for one emergency spot.

Constrains

1 <= All the values <= 1000000

Note-Every hospital has infinite no. of ambulances

Author :- Manish M Berwani

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The properties 1,3 and 7 can be reached by only 1 express highway from hospital 2.

And the properties 4 and 6 can also be reached by only 1 express highway from hospital 5.

So total 5 express highways are to be traveled.

Editor Image

?