UVa 10763 - Foreign Exchange

统计国家代号出现的次数,如果去的和来的相等就输出YES即可。

import java.util.*;

public class Main10763 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		while(true) {
			int n = scan.nextInt();
			if(n == 0) break;
			int[] a = new int[500000], b = new int[500000];
			Arrays.fill(a, 0);
			Arrays.fill(b, 0);
			int maxg = 0;
			for(int i=0; i<n; i++) {
				int mm = scan.nextInt();
				int nn = scan.nextInt();
				maxg = max(maxg, max(mm, nn));
				a[mm] ++;
				b[nn] ++;
			}
			int cnt = 0;
			for(int i=0; i<=maxg; i++) {
				if(a[i] != b[i]) {
					cnt = 1;
					break;
				}
			}
			if(cnt == 0)
				System.out.println("YES");
			else
				System.out.println("NO");
				
			
		}

	}
	public static int max(int a, int b) {
		if(a >= b)
			return a;
		else
			return b;
	}

}


 

 

posted @ 2014-12-26 22:10  Pickle  阅读(225)  评论(0编辑  收藏  举报