UVa869 - Airline Comparison(并查集)

本文介绍了一种算法,用于判断两家航空公司的航班目录是否等价。通过使用并查集结构,该算法能够有效地确定两个航空公司提供的城市间连接是否相同,而不论其具体的转机次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Background

   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.

Problem

   Given 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.
OutputFor each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blank line.

   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();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值