链接:https://ac.nowcoder.com/acm/contest/558/F
来源:牛客网
给定一张N个点,M条边的有向图,问有多少点对(u,v)(u<v),满足u能到达v且v也能到达u。
题解:用floyd来全部跑一遍,就可以得出答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
int T,n,m,u,v;
int a[350][350];
int main(){
cin>>n>>m;
for(int i = 0 ; i < m ; i++){
cin>>u>>v;
a[u][v] = 1;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
for(int k = 1 ; k <= n ; k++){
if(a[i][k]&&a[k][j]){
a[i][j] = 1;
}
}
}
}
int ans = 0;
for(int i = 1 ; i <= n ; i++){
for(int j = i+1 ; j <= n ; j++){
if(a[i][j]&&a[j][i])ans++;
}
}
cout<<ans<<endl;
}