Gym 100085E Eve(并查集)(Northeastern European Regional Contest 2011)

Problem

Input file: eve.inOutput file: eve.out

Mitochondrial DNA is the Deoxyribonucleic acid molecule that is contained in mitochondria within cellsof an organism.Mitochondrial DNA is normally passed to a child exclusively from its mother. Because of this fact, it ispossible to speak of “Mitochondrial Eve” which refers to the most recent common matrilineal ancestorof the entire population. Matrilineal ancestry is traced through female line: mother, grandmother, etc.Mitochondrial Eve of the Earth’s human population is estimated to have lived around 200 000 years ago,most likely in the East Africa.

In this problem, we consider a certain population of the same biological species (eukaryotic and anisogamous).The population has been observed for a period of time, and all births and deaths were clearlyrecorded. For some of the individuals within the population, their mitochondrial DNA was sequenced.It is assumed that each individual in the observed population received its mitochondrial DNA from itsmother without any mutations.Your task is to find out, whether all individuals currently alive have the same mitochondrial DNA.InputThe first line of the input file contains integer n (1 ≤ n ≤ 100 000), the number of individuals that werealive at the beginning of the observation period. IDs of these individuals are integers from 1 to n.Next n lines contain one character each. The i-th of these lines describes the sex of the individual withID i. ‘M’ stands for male, ‘F’ stands for female.The next line contains integer m (0 ≤ m ≤ 100 000), the total number of births and deaths that happenedduring the observation period.Next m lines contain description of birth and death events, listed in chronological order.A birth event is described by three space-separated tokens: the ID of the father, the ID of the mother,and a character that describes the sex of the child (‘M’ for male, ‘F’ for female). The ID given to theoffspring is the smallest positive integer that hasn’t been used as an ID by this point of time.A death event is described by a single negative integer, whose absolute value equals the ID of the individualthat died.The next line contains integer k (0 ≤ k ≤ n + m), the number of sequenced mitochondrial DNAs.Each of the next k lines contains two space-separated integers, the ID of the individual whose mitochondrialDNA has been sequenced, and the unique identifier of the mitochondrial DNA of that individual.Unique identifiers of two mitochondrial DNAs are the same if and only if the corresponding sequencedmitochondrial DNAs are the same. All unique identifiers of the mitochondrial DNAs are integers from 1to 109.All the data given in the input file is consistent and non-contradictory. Each individual’s mitochondrialDNA was sequenced at most once. At least one individual was alive at the end of the observation period.OutputThe output file must contain a single word:• YES – if it can be derived that all the individuals that are alive at the end of the experiment havethe same mitochondrial DNA;• NO – if it can be derived that some of the individuals that are alive at the end of the experimenthave different mitochondrial DNA;• POSSIBLY – if none of the cases above takes place.of an organism.Mitochondrial DNA is normally passed to a child exclusively from its mother. Because of this fact, it ispossible to speak of “Mitochondrial Eve” which refers to the most recent common matrilineal ancestorof the entire population. Matrilineal ancestry is traced through female line: mother, grandmother, etc.Mitochondrial Eve of the Earth’s human population is estimated to have lived around 200 000 years ago,most likely in the East Africa.In this problem, we consider a certain population of the same biological species (eukaryotic and anisogamous).The population has been observed for a period of time, and all births and deaths were clearlyrecorded. For some of the individuals within the population, their mitochondrial DNA was sequenced.It is assumed that each individual in the observed population received its mitochondrial DNA from itsmother without any mutations.Your task is to find out, whether all individuals currently alive have the same mitochondrial DNA.InputThe first line of the input file contains integer n (1 ≤ n ≤ 100 000), the number of individuals that werealive at the beginning of the observation period. IDs of these individuals are integers from 1 to n.Next n lines contain one character each. The i-th of these lines describes the sex of the individual withID i. ‘M’ stands for male, ‘F’ stands for female.The next line contains integer m (0 ≤ m ≤ 100 000), the total number of births and deaths that happenedduring the observation period.Next m lines contain description of birth and death events, listed in chronological order.A birth event is described by three space-separated tokens: the ID of the father, the ID of the mother,and a character that describes the sex of the child (‘M’ for male, ‘F’ for female). The ID given to theoffspring is the smallest positive integer that hasn’t been used as an ID by this point of time.A death event is described by a single negative integer, whose absolute value equals the ID of the individualthat died.The next line contains integer k (0 ≤ k ≤ n + m), the number of sequenced mitochondrial DNAs.Each of the next k lines contains two space-separated integers, the ID of the individual whose mitochondrialDNA has been sequenced, and the unique identifier of the mitochondrial DNA of that individual.Unique identifiers of two mitochondrial DNAs are the same if and only if the corresponding sequencedmitochondrial DNAs are the same. All unique identifiers of the mitochondrial DNAs are integers from 1to 109.All the data given in the input file is consistent and non-contradictory. Each individual’s mitochondrialDNA was sequenced at most once. At least one individual was alive at the end of the observation period.OutputThe output file must contain a single word:• YES – if it can be derived that all the individuals that are alive at the end of the experiment havethe same mitochondrial DNA;• NO – if it can be derived that some of the individuals that are alive at the end of the experimenthave different mitochondrial DNA;• POSSIBLY – if none of the cases above takes place.

Description

Mitochondrial DNA is the Deoxyribonucleic acid molecule that is contained in mitochondria within cellsof an organism.Mitochondrial DNA is normally passed to a child exclusively from its mother. Because of this fact, it ispossible to speak of “Mitochondrial Eve” which refers to the most recent common matrilineal ancestorof the entire population. Matrilineal ancestry is traced through female line: mother, grandmother, etc.Mitochondrial Eve of the Earth’s human population is estimated to have lived around 200 000 years ago,most likely in the East Africa.In this problem, we consider a certain population of the same biological species (eukaryotic and anisogamous).The population has been observed for a period of time, and all births and deaths were clearlyrecorded. For some of the individuals within the population, their mitochondrial DNA was sequenced.It is assumed that each individual in the observed population received its mitochondrial DNA from itsmother without any mutations.Your task is to find out, whether all individuals currently alive have the same mitochondrial DNA.

Input

The first line of the input file contains integer n (1 ≤ n ≤ 100 000), the number of individuals that werealive at the beginning of the observation period. IDs of these individuals are integers from 1 to n.Next n lines contain one character each. The i-th of these lines describes the sex of the individual withID i. ‘M’ stands for male, ‘F’ stands for female.The next line contains integer m (0 ≤ m ≤ 100 000), the total number of births and deaths that happenedduring the observation period.Next m lines contain description of birth and death events, listed in chronological order.A birth event is described by three space-separated tokens: the ID of the father, the ID of the mother,and a character that describes the sex of the child (‘M’ for male, ‘F’ for female). The ID given to theoffspring is the smallest positive integer that hasn’t been used as an ID by this point of time.A death event is described by a single negative integer, whose absolute value equals the ID of the individualthat died.The next line contains integer k (0 ≤ k ≤ n + m), the number of sequenced mitochondrial DNAs.Each of the next k lines contains two space-separated integers, the ID of the individual whose mitochondrialDNA has been sequenced, and the unique identifier of the mitochondrial DNA of that individual.Unique identifiers of two mitochondrial DNAs are the same if and only if the corresponding sequencedmitochondrial DNAs are the same. All unique identifiers of the mitochondrial DNAs are integers from 1to $10^9$.All the data given in the input file is consistent and non-contradictory. Each individual’s mitochondrialDNA was sequenced at most once. At least one individual was alive at the end of the observation period.

Output

The output file must contain a single word:• YES – if it can be derived that all the individuals that are alive at the end of the experiment havethe same mitochondrial DNA;• NO – if it can be derived that some of the individuals that are alive at the end of the experimenthave different mitochondrial DNA;• POSSIBLY – if none of the cases above takes place.