PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000036. So sánh mạng bay (Airline Comparison)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

So sánh mạng bay (Airline Comparison)

Airline Comparison

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Two airlines have directed flight networks over uppercase-letter cities. Determine whether the networks have the same reachability relation for every ordered pair.

Input

Integer m1m_1 and m1m_1 uppercase-letter directed edges for network 1, then m2m_2 and m2m_2 edges for network 2.

Output

Print YES if both networks induce the same reachability, otherwise NO.

Subtasks

  • Subtask 1 (40 points): m1,m2≤12m_1,m_2\le12; 0≤m1,m2≤6500\le m_1,m_2\le650; endpoints are A–Z, no loops or duplicate directed edges.
  • Subtask 2 (60 points): 0≤m1,m2≤6500\le m_1,m_2\le650; endpoints are A–Z, no loops or duplicate directed edges.

Examples

Input

2
A B
B C
3
A B
B C
A C

Output

YES

Explanation

The extra direct route A to C in the second graph was already implied by A to B to C in the first, so the reachable ordered pairs coincide.