离散题目14

来源:互联网 发布:惠州人民政府网络问政 编辑:程序博客网 时间:2024/06/04 23:10

离散题目14

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

判断集合是不是对称的。

Input

首先输入两个数n,m表示集合中元素的个数,以及存在的关系数。

接下来1行包含n个以空格分隔的整数。

接下来m行,每行包含两个数a,b表示关系。

(1< = n < = 1000,1 < = a,b < = n,m < = n*(n-1)&& m < = 1000)

Output

对于每组输入,如果这个集合是对称的则输出“YES”,否则输出“NO”。(均不包含引号)

Example Input

5 81 11 22 13 32 33 24 55 45 91 11 22 13 32 33 24 55 45 1

Example Output

YESNO

Hint

Author

UMR

point:for循环,谨防超时。
a,b是属于集合的元素,R是关系,则有:

1自反性---即对集合中的每一个元素a都有aRa

2对称性---即对集合中的任意元素aRb,aRb成立当且仅当bRa成立

3传递性---即对集合中的任意元素abc若aRb和bRc成立则aRc一定成立
#include <iostream>#include <bits/stdc++.h>using namespace std;int main(){    int m,n;    int x,y;    while(cin>>m>>n)    {        int flag = 1;        int a[1001][1001];        memset(a,0,sizeof(a));        set<int>v1,v2;        set<int>::iterator it1,it2;        v1.clear();        v2.clear();        while(n--)        {            cin>>x>>y;            a[x][y] = 1;            v1.insert(x);//由于是非连续集合,为了不浪费多余时间,存储x,y,方便下面作比较时直接调用            v2.insert(y);        }        for(it1 = v1.begin(); it1 != v1.end(); it1++)            for(it2 = v2.begin(); it2 != v2.end(); it2++)            {                if(a[*it1][*it2] != a[*it2][*it1])//因为在所设定01矩阵中,对称即相等,不对称即不相等,十分清晰的判断思路                {                    flag = 0;                    break;                }            }        if(flag)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;    }    return 0;}/***************************************************User name:Result: AcceptedTake time: 112ms****************************************************/


原创粉丝点击