Tyvj P2059(传递闭包)

内容目录

朴素的传递闭包

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<functional>
#include<algorithm>
#include<cctype>
using namespace std;
#define MAXN (100+10)
#define MAXM (4500+10)
bool f[MAXN][MAXN]={0};
int n,m;
int main()
{
	cin>>n>>m;
	for (int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		f[x][y]=1;
	}
	for (int k=1;k<=n;k++)
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				f[i][j]=f[i][j]||f[i][k]&&f[k][j];
	int tot=0;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=n;j++)
			if (i!=j&&!(f[i][j]||f[j][i])) {tot--;break;}

		tot++;
	}
//	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) cout<<f[i][j]<<' ';

	cout<<tot;
	return 0;
}