题目:
A到B地,要有B到A地才行得通
分析:
初始化 a[i] = i,然後讀入x,y,交換a[x],a[y]。最後判斷是否a[i]==i
#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; #define debug puts("here") #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define REP(i,a,b) for(int i=a;i<=b;i++) #define foreach(i,vec) for(unsigned i=0;i<vec.size();i++) #define pb push_back #define RD(n) scanf("%d",&n) #define RD2(x,y) scanf("%d%d",&x,&y) #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w) /******** 程序部份 ********************/ const int MAXN = 500005; int a[MAXN],n; int main(){ #ifndef ONLINE_JUDGE freopen("sum.in","r",stdin); //freopen("sum.out","w",stdout); #endif while(RD(n),n){ rep1(i,500000) a[i] = i; int x,y; rep1(i,n){ RD2(x,y); swap(a[x],a[y]); } bool ok = true; rep1(i,500000) if(a[i]!=i){ ok = false; break; } ok?puts("YES"):puts("NO"); } return 0; }