心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0

题目大意:给出一些整数对(a,b),判断是否每个(a,b)都有(b,a)与之配对。
使用map即可快速地查找。
以下是我的代码:

#include<map>
#include
<cstdio>
using namespace std;

struct Type
{
    Type(
int a,int b):a_(a),b_(b) {}
    
int a_,b_;
};
bool operator<(const Type &a,const Type &b)
{
    
return (a.a_<b.a_ || (a.a_==b.a_ && a.b_<b.b_));
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen(
"data.in","r",stdin);
    freopen(
"data.out","w",stdout);
    
#endif

    
int n;
    
while(scanf("%d",&n)==1 && n)
    {
        map
<Type,int> r;
        
for(int i=1;i<=n;i++)
        {
            
int a,b;
            scanf(
"%d%d",&a,&b);
            r[Type(a,b)]
++;
        }

        
bool success(true);
        
for(map<Type,int>::iterator i=r.begin();i!=r.end();i++)
        {
            
int a(i->first.a_),b(i->first.b_);
            
if(i->second!=r[Type(b,a)])
            {
                success
=false;
                
break;
            }
        }

        
if(success)
            printf(
"YES\n");
        
else
            printf(
"NO\n");
    }

    
return 0;
}
posted on 2011-05-16 17:29 lee1r 阅读(495) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟题目分类:数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理