POJ 3660
- 题意:告诉所有奶牛的关系,让你确定奶牛的排名(只有当该奶牛与其他所有奶牛的关系确定了才能确定该奶牛的排名)
- 通过floyd确定一个点是否能到另一个点.
#include <iostream>
using namespace std;
const int maxn = 105;
int e[maxn][maxn];
int n,m;
int floyd() {
for (int k=1; k<=n; k++)
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
e[i][j] = e[i][j] | (e[i][k] & e[k][j]);
int ans = 0;
for (int i=1; i<=n; i++) {
int tmp = 1;
for (int j=1; j<=n; j++) {
if (i == j) continue;
else {
tmp = tmp & (e[i][j] | e[j][i]);
}
}
ans += tmp;
}
return ans;
}
int main() {
cin >> n >> m;
for (int i=1; i<=m; i++) {
int u,v;
cin >> u >> v;
e[u][v] = 1;
}
printf("%d\n",floyd());
return 0;
}