Codeforces Round 270
C
Design Tutorial: Make It Nondeterministic
Codeforces
1400
原题
← BD →
翻译中…

A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.

翻译中…

Let's try to make a new task. Firstly we will use the following task. There are $*n*$ people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are $*n*$ people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation $*p*$?

翻译中…

More formally, if we denote the handle of the $*i*$-th person as $*h*_{*i*}$, then the following condition must hold: image.

翻译中…

Input

翻译中…

The first line contains an integer $*n*$ $(1 ≤ *n* ≤ 10^{5})$ — the number of people.

翻译中…

The next $*n*$ lines each contains two strings. The $*i*$-th line contains strings $*f*_{*i*}$ and $*s*_{*i*}$ $(1 ≤ |*f*_{*i*}|, |*s*_{*i*}| ≤ 50)$ — the first name and last name of the $*i*$-th person. Each string consists only of lowercase English letters. All of the given $2*n*$ strings will be distinct.

翻译中…

The next line contains $*n*$ distinct integers: $*p*_{1}, *p*_{2}, ..., *p*_{*n*}$ $(1 ≤ *p*_{*i*} ≤ *n*)$.

翻译中…

Output

翻译中…

If it is possible, output "YES", otherwise output "NO".

翻译中…

Note

翻译中…

In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.

翻译中…

In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.

翻译中…

样例

Input #1
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output #1
NO
InputCopy OutputCopy
Input #2
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output #2
YES
InputCopy OutputCopy
Input #3
2
galileo galilei
nicolaus copernicus
2 1
Output #3
YES
InputCopy OutputCopy
Input #4
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output #4
NO
InputCopy OutputCopy
Input #5
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output #5
YES
InputCopy OutputCopy
题目信息
题号
472C
时间限制
2 s
内存限制
256 MB
标签
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。