064:Cow Contest(弗洛伊德模板題)
思路:
以一個點爲跳板,比較他已明確的兩個點之間的關係,一共存在兩種關係:
1:i>k&&k>j then i>j
2:i<k&&k<j then i<j
暴力跑就完事了
代碼:
#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <algorithm>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll INF = 1e9 + 10;
ll dp[500][500];
ll n, m, k, i, j, t, x, y;
ll Floyd() {
for (k = 1; k <= n; k++) {
for (i = 1; i <= n; i++) {
if (dp[i][k] == 0)continue;
for (j = 1; j <= n; j++) {
if (dp[i][k] == dp[k][j]) {
dp[i][j] = dp[i][k];
}
}
}
}
ll sum = 0;
for (i = 1; i <= n; i++) {
ll ans = 0;
for (j = 1; j <= n; j++) {
if (dp[i][j])ans++;
}
if (ans == n - 1)sum++;
}
return sum;
}
int main() {
FAST_IO;
while (cin >> n >> m) {
mes(dp, 0);
for (i = 0; i < m; i++) {
cin >> x >> y;
dp[x][y] = 1;
dp[y][x] = -1;
}
cout << Floyd() << endl;
}
}