2018年牛客多校寒假 第四场 F (call to your teacher) (图的连通性)
题目链接
传送门:https://ac.nowcoder.com/acm/contest/76/F
思路:
题目的意思就是判断图的连通性可以用可达性矩阵来求,至于图的存储可以用邻接矩阵来储存,求出来可达性矩阵后判断下 a[1] [n] 是不是为零就好了,不为零的话说明从1(自己)到 n(teacher)是连通的,为零的话就说明不是连通的
代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 const int MAXN = 60; 5 int n, m, a[MAXN][MAXN]; 6 7 int main() 8 { 9 while (cin >> n >> m) 10 { 11 int x, y; 12 while (m -- ) 13 { 14 cin >> x >> y; 15 a[x][y] = 1; 16 } 17 18 for (int i = 1; i <= n; i ++ ) 19 for (int j = 1; j <= n; j ++ ) 20 { 21 if (a[j][i] == 1) 22 { 23 for (int k = 1; k <= n; k ++ ) 24 if ((a[j][k] + a[i][k]) >= 1) 25 a[i][k] = 1; 26 } 27 } 28 29 if (a[1][n] != 0) puts("Yes"); 30 else puts("No"); 31 memset(a,0,sizeof(a)); 32 } 33 return 0; 34 }