本题记录一噶Floyd的一个用法:
floyed不仅能求任意两点的最短路,还能求一个点能否到另一个点。
f[i][j]=f[i][j]|(f[i][k]&f[k][j])表示i能否走到j,即要么一开始i能到j,要么i能到k,k再能到j。
对于本题而言 即i能否胜j。代码如下:
#include<algorithm> #include<iostream> #include<cstring> #include<queue> #include<cstdio> #include<map> #define N 1000 using namespace std; int n, m, a, b, f[N][N], ans; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; f[a][b] = 1; } for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] = f[i][j] | f[i][k] & f[k][j]; for (int i = 1; i <= n; i++) { int gg = 1; for (int j = 1; j <= n; j++) if (i ==j) continue; else gg = gg & (f[i][j] | f[j][i]); ans += gg; } cout << ans << endl; return 0; }