floyd+bitset传递闭包
#include <cstdio> #include <bitset> using namespace std; const int maxn = 2000 + 10; int n; bitset<maxn> dis[maxn]; char s[maxn]; int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%s", s + 1); for(int j = 1; j <= n; j++) if(s[j] == '1' || i == j) dis[i][j] = true; } for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) if(dis[i][k]) dis[i] |= dis[k]; int ans = 0; for(int i = 1; i <= n; i++) ans += dis[i].count(); printf("%d ", ans); return 0; } 转载于:https://www.cnblogs.com/ruoruoruo/p/7805252.html