Contents
  1. 1. hdu 1704:

hdu 1704:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60



#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 505
#define INF 0x3f3f3f3f
using namespace std;
int mp[MAXN][MAXN];
int n, m;

void init()
{

for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
mp[i][j] = (i == j); //自己到自己是连通的
}
}
}

void floyd()
{

for(int k = 1; k <= n; k++){
for(int i = 1; i <= n; i++){
if(mp[i][k]) //如果k作为中间点,i到k连通,k到j连通,则i到j连通
for(int j = 1; j <= n; j++){
if(mp[k][j])
mp[i][j] = 1;
}
}
}
}

int main()
{

//freopen("input.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--){
int ans = 0;
scanf("%d%d", &n, &m);
init();
while(m--){
int a, b;
scanf("%d%d", &a, &b);
mp[a][b] = 1; //a到b连通
}
floyd();
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){//j不需要再从1开始,而是从i+1开始,因为i+1前面的已经判断过了
if(!(mp[i][j] || mp[j][i])) //if(mp[i][j] == 0 && mp[j][i])
ans++; //不确定的关系
}
}
printf("%d\n", ans);
}
return 0;
}
Contents
  1. 1. hdu 1704: