'commit'
This commit is contained in:
36
TangDou/Topic/HDU1704.cpp
Normal file
36
TangDou/Topic/HDU1704.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
#define inf 0x3f3f3f3f
|
||||
const int N = 510;
|
||||
int n, m, x, y, ans;
|
||||
int g[N][N];
|
||||
|
||||
void floyd() {
|
||||
for (int k = 1; k <= n; k++)
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (!g[i][k]) continue; // floyd优化
|
||||
for (int j = 1; j <= n; j++)
|
||||
g[i][j] |= g[i][k] & g[k][j]; // 通过k传递,或运算
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int T;
|
||||
cin >> T;
|
||||
while (T--) {
|
||||
cin >> n >> m;
|
||||
memset(g, 0, sizeof g);
|
||||
while (m--) {
|
||||
cin >> x >> y;
|
||||
g[x][y] = 1; // x<y
|
||||
}
|
||||
// 计算传递闭包
|
||||
floyd();
|
||||
|
||||
ans = 0;
|
||||
for (int i = 1; i <= n; i++) // 统计答案
|
||||
for (int j = i + 1; j <= n; j++)
|
||||
if (!g[i][j] && !g[j][i]) ans++; // 无法确定大小关系
|
||||
cout << ans << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user