Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
給出集合X、X上的關係R,判斷關係R是不是傳遞的。
例如: A={1,2,3} , R={<1,1>,<1,2>,<2,1>,<3,3>,<2,3>,<3,2>, <2,2>,<1,3>,<3,1>} 顯然,R具有傳遞性。
Input
多組輸入,每組輸入第一行爲集合X的元素;第二行爲一個整數n ( n > 0 ),代表X上的關係R中序偶的個數;接下來n行用來描述X上的關係R,每行兩個數字,表示關係R中的一個序偶。細節參考示例輸入。
非空集合X的元素個數不大於500,每個元素的絕對值不大於2^32 - 1。
Output
每組輸入對應一行輸出,如果關係R具有傳遞性輸出 ”true”,否則輸出 ”yes”。
Example Input
1 2 3
9
1 1
2 2
3 3
1 2
2 1
1 3
3 1
2 3
3 2
1 2 3
6
1 1
1 2
3 3
2 3
3 2
2 2
Example Output
true
yes
include <stdio.h>
include <stdlib.h>
include<string.h>
struct
{
int x,y;
} a[10000000]
;
int main()
{
char s[10000];
int i,j,f,n,k;
while(gets(s))
{
f=1;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(a[i].y==a[j].x)
{
for(k=0; k<n; k++)
if(a[j].y==a[k].y&&a[i].x==a[k].x)
break;
if(k==n)
{
f=0;
break;
}
}
}
if(f==0)
break;
}
if(f)
printf("true\n");
else
printf("yes\n");
getchar();//沒有不對。。。~~~~(>_<)~~~~
getchar();
}
return 0;
}