首页 > 代码库 > UVA 10763 Foreign Exchange

UVA 10763 Foreign Exchange

建立两个数组同时存储原学校与目标学校。分别按优先原学校和优先目标学校排序,一一配对,验查是否可行。

 

 

 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4  5 struct node { 6     int x,y; 7 }a[500010],b[500010]; 8  9 bool cmp1 (node x,node y){10     if ((x.x<y.x)||(x.x==y.x&&x.y<y.y))11         return true ;12     return false ;13 }14 bool cmp2 (node x,node y){15     if ((x.y<y.y)||(x.y==y.y&&x.x<y.x))16         return true ;17     return false ;18 }19 20 int main (){21     int n;22     while (cin>>n&&n){23         for (int i=0;i<n;i++){24             cin>>a[i].x>>a[i].y;25             b[i].x=a[i].x;26             b[i].y=a[i].y;27         }28         sort (a,a+n,cmp1);29         sort (b,b+n,cmp2);30         int flag=1;31         for (int i=0;i<n;i++){//cout<<i<<":"<<a[i].x<<" "<<a[i].y<<"  "<<b[i].x<<" "<<b[i].y<<endl;32             if (a[i].x==b[i].y&&a[i].y==b[i].x)33                 continue ;34             flag=0;break ;35         }36         if (flag)37             cout<<"YES"<<endl;38         else cout<<"NO"<<endl;39     }40     return 0;41 }