You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 1010 , M = N < < 2 ; // 图的最大点数量
// 链式前向星
int e [ M ] , h [ N ] , idx , w [ M ] , ne [ M ] ;
void add ( int a , int b , int c = 0 ) {
e [ idx ] = b , ne [ idx ] = h [ a ] , w [ idx ] = c , h [ a ] = idx + + ;
}
int n , m ; // 表示图中有n个点, m条边
bool st [ N ] ;
int cnt ;
// 深度遍历
void dfs ( int u ) {
st [ u ] = 1 ;
cnt + + ; // 多走了一个结点
for ( int i = h [ u ] ; ~ i ; i = ne [ i ] ) {
int v = e [ i ] ;
if ( ! st [ v ] )
dfs ( v ) ;
}
}
int main ( ) {
# ifndef ONLINE_JUDGE
freopen ( " in1.in " , " r " , stdin ) ;
// freopen("in2.in", "r", stdin);
# endif
// 初始化链式前向星
memset ( h , - 1 , sizeof h ) ;
// 采用邻接表建图
cin > > n > > m ;
// m条边
for ( int i = 1 ; i < = m ; i + + ) {
int a , b ;
cin > > a > > b ;
add ( a , b ) ;
}
// 利用dfs进行检查是不是强连通的
dfs ( 1 ) ;
if ( cnt = = n )
printf ( " 图是连通的 \n " ) ;
else
printf ( " 图不是连通的 \n " ) ;
return 0 ;
}