離散題目13(判斷自反關係)

離散題目13
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

DaYu平時只顧着看電影,沒有學習離散,學期末快考試的時候才慌了神,因爲時間不夠,因此他決定只複習一個知識點,但是他發現他一個知識點都不會,因此他跑過來請你幫他解決一個問題。求一個集合是否是自反的。
Input

第一行輸入組數T(T<10),每組的第一行輸入集合元素個數m(m < 100)和對應關係個數n(n < 100),集合中元素爲1,2,…,m,接下來n行每行輸入兩個數x,y(0 < x, y < 100)
Output

如果滿足自反關係,則輸出true,否則輸出false。
Example Input

2
5 9
1 1
1 2
2 2
3 3
2 3
4 4
4 5
5 5
5 1
5 8
1 1
1 2
2 2
3 3
2 3
4 4
4 5
5 1
Example Output

true
false

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int a[1000][1000];
int main()
{
    int i,j,k,n,m,f,x,y,t;
    scanf("%d",&t);
    while(t--)
    {
        f=1;
        memset(a,0,sizeof(a));
        scanf("%d%d",&m,&n);
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            a[x][y]=1;
        }
        for(i=1; i<=m; i++)
            for(j=1; j<=m; j++)
            {

                if(i==j)
                {
                    if(a[i][j]==0)
                    {
                        f=0;
                        break;
                    }
                    else
                        continue;
                }
            }
        if(f)
            printf("true\n");
        else
            printf("false\n");
    }


    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章