离散题目15
Time Limit: 1000MS
Memory Limit: 65536KB
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 <bits/stdc++.h> using namespace std; struct node { int a; int b; } cnt[100000]; string ss; char str[100010]; int main() { int i, n; while(gets(str)) { scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d %d", &cnt[i].a, &cnt[i].b); int j, k; int g = 1; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { if(cnt[i].b == cnt[j].a ) { int flag = 0; for(k = 0; k < n; k++) { if(cnt[k].a == cnt[i].a && cnt[k].b == cnt[j]. b) { flag = 1; break; } } if(flag== 0) { g = 0; break; } } if(g == 0) break; } if(g == 0) break; } if(g) printf("true\n"); else printf("yes\n"); getline(cin, ss); } return 0; }