题目:http://codevs.cn/problem/1019/
思路:建立每个顶点的边集,检测传递性。
题解:
/* 1019 集合论与图论 */
#include <stdio.h>
#define DEBUG
#define MAXN 1001
int t; /* 数据个数 */
int n, m; /* 点,边数 */
int E[MAXN][MAXN]; /* 边集合 */
/* 检测传递性 */
int check_transitivity(){
int x, y, z;
for(x = 1; x <= n; x++){
for(y = 1; y <= n; y++){
if(E[x][y] != 1){
continue;
}
for(z = 1; z <= n; z++){
if(E[y][z] == 1 && E[x][z] != 1){
return 0;
}
}
}
}
return 1;
}
/* 主函数入口 */
int main(int argc, char *argv[]) {
int i; /* 索引值 */
int x, y; /* 边值 */
#ifdef DEBUG
FILE *fp;
if(NULL == (fp = fopen("data.txt", "r"))){
return 1;
}
#endif
/* 获取数据个数 */
#ifdef DEBUG
fscanf(fp, "%d", &t);
#else
scanf("%d", &t);
#endif
while(0 < t--){
/* 获取V和E数 */
#ifdef DEBUG
fscanf(fp, "%d %d", &n, &m);
#else
scanf("%d %d", &n, &m);
#endif
/* 初始化边集 */
for(x = 1; x <= n; x++){
for(y = 1; y <= n; y++){
E[x][y] = 0;
}
}
/* 获取所有边集合 */
for(i = 0; i < m; i++){
#ifdef DEBUG
fscanf(fp, "%d %d", &x, &y);
#else
scanf("%d %d", &x, &y);
#endif
E[x][y] = 1;
}
if(1 == check_transitivity()){
printf("Yes\n");
}
else {
printf("No\n");
}
}
#ifdef DEBUG
fclose(fp);
#endif
return 0;
}