SWUST OJ 1067: 有向图的邻接表存储强连通判断

1067: 有向图的邻接表存储强连通判断

题目链接-1067: 有向图的邻接表存储强连通判断
SWUST OJ 1067: 有向图的邻接表存储强连通判断_第1张图片
解题思路
f l o y d 思 想 floyd思想 floyd

  • i i i k k k k k k j j j之间联通,那么 i i i j j j之间也必定联通,可以 f l o y d floyd floyd三重循环暴力处理,a[u][v]的值为 1 1 1 0 0 0,表示 u u u v v v之间是否连通
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1010;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int a[N][N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n,e;
	cin>>n>>e;
	memset(a,0,sizeof a);
	while(e--){
		int u,v;
		cin>>u>>v;
		a[u][v]=1;
	}
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][k]&&a[k][j])
					a[i][j]=1;
			}
		}
	}
	bool ass=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(a[i][j]!=a[j][i]){
				ass=1;
				break;
			}
		}
	}
	ass?cout<<"no":cout<<"yes";
	return 0;
}

你可能感兴趣的:(SWUST,OJ)