UVa 10763 - Foreign Exchange

10763 - Foreign Exchange


思路:渣算法,开两个数组一个记出发地一个记目的地,地点编号化为数组中位置然后该位置+1。最后只要两数组相同位置的整数都相等就说明人数正好。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int MAXN = 5000010;
int o[MAXN], d[MAXN];

int main(){
	int n;
	int a, b;
	while (~scanf("%d", &n) && n){
		int count = 0;
		memset(o, -1, sizeof(o));
		memset(d, -1, sizeof(d));

		for (int i = 0; i < n; i++){
			scanf("%d%d", &a, &b);
			o[a]++; d[b]++;
			//printf("o[%d]=%d d[%d]=%d\n", a, o[a], b, d[b]);
		}

		int f = 0;

		for (int i = 0; i < MAXN; i++){
			if (o[i] != -1){
				if (o[i] != d[i]){
					f = 1;
					break;
				}
				count++;
				if (count == n) {
					break;
				}
			}
		}

		if (f == 0) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


你可能感兴趣的:(UVa 10763 - Foreign Exchange)