UVa 11838 - Come and Go

xiaoxiao2021-02-28  9

题目:已知一个有向图,问是否所有节点间都连通(可以双向到达)。

分析:图论。利用floyd算法求传递闭包,判断输出即可。

说明:( ⊙ o ⊙ )。

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int maps[2002][2002]; int main() { int N, M, V, W, P; while (~scanf("%d%d",&N,&M) && N) { memset(maps, 0, sizeof(maps)); for (int i = 1; i <= M; ++ i) { scanf("%d%d%d",&V,&W,&P); if (P == 1) { maps[V][W] = 1; }else { maps[V][W] = 1; maps[W][V] = 1; } } for (int k = 1; k <= N; ++ k) { for (int i = 1; i <= N; ++ i) { for (int j = 1; j <= N; ++ j) { maps[i][j] |= maps[i][k]&maps[k][j]; } } } int ans = 1; for (int i = 1; i <= N; ++ i) { for (int j = 1; j <= N; ++ j) { if (i != j && (!maps[i][j] || !maps[j][i])) { ans = 0; } } } printf("%d\n",ans); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-450310.html

最新回复(0)