An airline catalogconsists of a list of flights between pairs of cities. A trip may be built bysequencing flights. Two airline companies are equivalent if they offerconnections between the same pairs of cities, irrespective of the number ofscales in between.
ProblemGiven the catalogs oftwo airline companies, determine if they are equivalent or not.
InputThe input begins with a single positive integer on a line by itself indicatingthe number of the cases following, each of them as described below.This line is followed by a blank line, and there is also a blank line betweentwo consecutive inputs.The input contains:
- First line: the number N of flights in the catalog of the first company;
- N subsequent lines: two characters separated by one space, for the names of the origin and destination cities of a flight;
- Line N+2: the number M of flights in the catalog of the second company;
- M subsequent lines: two characters separated by one space, for the names of the origin and destination cities of a flight.
One line containing YES or NO
Sample Input
1
6
A B
B E
A E
C F
E C
D A
7
A B
D A
E C
C F
D B
B E
D F
Sample Output
YES
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Scanner;
import java.io.BufferedInputStream;
public class Main implements Runnable{
private static final boolean DEBUG = false;
private static final int N = 256;
private Scanner cin;
private PrintWriter cout;
private HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
private int[] p;
private void init()
{
try {
if (DEBUG) {
cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new Scanner(new BufferedInputStream(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
private String next()
{
try {
return cin.next();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private int find(int x)
{
int root = x;
while (p[root] != root) {
root = p[root];
}
while (p[x] != root) {
int tmp = x;
x = p[x];
p[tmp] = root;
}
return root;
}
private void union(int a, int b)
{
int pa = find(a);
int pb = find(b);
if (pa != pb) {
p[pb] = pa;
}
}
private boolean input()
{
p = new int[N];
for (int i = 0; i < N; i++) p[i] = i;
int n = Integer.parseInt(next());
for (int i = 0; i < n; i++) {
String source = next();
String dest = next();
int u, v;
if (hm.containsKey(source.charAt(0))) {
u = hm.get(source.charAt(0));
} else {
u = hm.size();
hm.put(source.charAt(0), u);
}
if (hm.containsKey(dest.charAt(0))) {
v = hm.get(dest.charAt(0));
} else {
v = hm.size();
hm.put(dest.charAt(0), v);
}
union(u, v);
}
return true;
}
private void solve(int cas)
{
int n = Integer.parseInt(next());
boolean ok = true;
for (int i = 0; i < n; i++) {
String source = next();
String dest = next();
int u = 0, v = 0;
if (hm.containsKey(source.charAt(0))) {
u = hm.get(source.charAt(0));
} else {
ok = false;
}
if (hm.containsKey(dest.charAt(0))) {
v = hm.get(dest.charAt(0));
} else {
ok = false;
}
if (ok && find(u) != find(v)) ok = false;
}
if (ok) cout.println("YES");
else cout.println("NO");
if (cas != 0) cout.println();
cout.flush();
}
public void run()
{
init();
int t = Integer.parseInt(next());
while (t-- > 0){
input();
solve(t);
}
}
public static void main(String[] args)
{
new Thread(new Main()).start();
}
}