#C3524. Safe Chemical Addition
Safe Chemical Addition
Safe Chemical Addition
In this problem, you are given a list of chemicals currently in storage along with a list of reactive pairs. A reactive pair is defined as two chemicals that can react dangerously if they come into contact. You are also given a proposed chemical to add to the storage. Your task is to determine whether adding the proposed chemical is safe.
Formally, let (S) be the set of chemicals in storage, and let (R) be a set of unordered pairs ((a, b)) representing chemicals that react with each other. A proposed chemical (c) can be safely added if for every chemical (x \in S), the pair ((c, x)) is not in (R). If there exists any chemical (x \in S) such that ((c, x)) is in (R), the addition is deemed "DANGEROUS". Otherwise, it is "SAFE".
inputFormat
The input is provided via standard input (stdin) in the following format:
- The first line contains two integers (n) and (r), where (n) is the number of chemicals currently in storage and (r) is the number of reactive pairs.
- The second line contains (n) space-separated strings representing the chemicals in storage. If (n = 0), this line will be empty.
- The next (r) lines each contain two space-separated strings representing a reactive pair.
- The last line contains a single string, the proposed chemical to be added.
For example:
3 2 H2O NaOH HCl H2O HCl H2O NaOH H2SO4
outputFormat
Output a single line to standard output (stdout) containing either "SAFE" if the proposed chemical can be safely added, or "DANGEROUS" if it would react with any chemical in storage.## sample
0 0
H2O
SAFE