概念解释 [强连通]——有向图G中,任意点对能相互到达,即存在路径,则称为强连通。
[连通分量]——指的是无向图中的极大连通子图。一个连通图的生成树是一个极小连通子图。
//只是判断是否强连通
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
const int MAXV=11000;
vector<int>adj[MAXV],an_adj[MAXV]; //用vector来存原图和转置图
bool visted[MAXV]; //标记
int n,m;
bool dfs(int tmp,vector<int>* Graph)
{
stack<int>st;
int cnt=0; //计算有多少点是连着的
visted[tmp]=1;
st.push(tmp);
while(!st.empty())
{
int u=st.top();
cnt++;
st.pop();
for(int i=0; i<Graph[u].size(); i++){
if(visted[Graph[u][i]])
continue;
visted[Graph[u][i]]=1;
st.push(Graph[u][i]);
}
}
if(cnt==n)
return true;
return false;
}
void Transform() //将原图转置
{
for(int i=1; i<=n; i++)
{
for(int j=0; j<adj[i].size(); j++)
{
an_adj[adj[i][j]].push_back(i);
}
}
}
bool kosaraju()
{
memset(visted,0,sizeof(visted));
if(!dfs(1,adj)) //here is 1
return false;
memset(visted,0,sizeof(visted));
Transform();
if(dfs(1,an_adj)) //there must be 1
return true;
return false;
}
int main()
{
int a,b;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
for(int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
adj[a].push_back(b); //入图
}
bool ans= kosaraju();
ans==true? printf("Yes\n"):printf("No\n");
for(int i=0;i<=n;i++){
adj[i].clear();
an_adj[i].clear();
}
}
return 0;
}