POJ3275 Ranking the Cows floyd的bitset优化
1 #include <iostream> 2 #include <cstdio> 3 #include <bitset> 4 using namespace std; 5 const int maxn = 1005; 6 int n, m; 7 bitset<maxn> maps[maxn]; 8 void floyd() { 9 for (int k = 1; k <= n; k++) { 10 for (int i = 1; i <= n; i++) { 11 if (maps[i][k]) maps[i] |= maps[k]; 12 } 13 } 14 } 15 int main() { 16 scanf("%d%d",&n,&m); 17 for (int i = 1; i <= m; i++) { 18 int u, v; scanf("%d%d",&u,&v); 19 maps[u][v] = true; 20 } 21 floyd(); 22 int ans = 0; 23 for (int i = 1; i <= n; i++) { 24 for (int j = i+1; j <= n; j++) { 25 if (!maps[i][j] && !maps[j][i]) ans++; 26 } 27 } 28 printf("%d\n",ans); 29 return 0; 30 }