AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design
// 10763 - Foreign Exchange
#include <iostream>
#define MAXN 500000
using namespace std;
int a[MAXN+1];
int main(void)
{
int i, n, x, y;
while(cin >> n && n != 0)
{
for(i = 1; i <= MAXN; i++)
a[i] = i;
for(i = 1; i <= n; i++)
{
cin >> x >> y;
swap(a[x], a[y]);
}
for(i = 1; i <= MAXN; i++)
if(a[i] != i)
{
cout << "NO" << endl;
break;
}
if(i == MAXN+1)
cout << "YES" << endl;
}
return 0;
}