Bitset Optimizing transitive closure template problem
This relationship is modeled directly on graph theory, which is actually a transitive closure.
Transitive closures There is a simple way to do this is to Floyd.
And the scope of the problem is \ (n \leq 1000\),\ (n^3\) The violence will obviously t.
And the use of bitset, heard can be optimized to the original method of \ (\frac{1}{32}\) even better!
Directly to the code is the fact that you don't understand the principle
#include<cstdio>#include<bitset>const int maxn = 1005;std::bitset<maxn> b[maxn];int n, m;int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) b[i][i] = true; while(m--) { int u, v; scanf("%d%d", &u, &v); b[u][v] = true; } for(int k = 1; k <= n; k++) { for(int i = 1; i <= n; i++) { if(b[i][k]) { b[i] |= b[k]; } } } int ans = 0; for(int i = 1; i <= n; i++) ans += b[i].count(); ans -= n; ans = n * (n - 1) / 2 - ans; printf("%d\n", ans); return 0;}
P2881 [Usaco07mar] ranked cattle ranking the cows