原題地址
Tarjan直接上既珂,Tarjan板子不用多說罷
:
# include <bits/stdc++.h>
using namespace std;
int n,m;
const int N=10010;
vector <int> g[N];
int low[N],dfn[N];
bool vis[N],in[N];
int totd=0,ans=0;
int q[N*5];
int top=0;
void compare(void)
{
for(int i=1;i<=n;i++)
{
low[i]=dfn[i]=0;
vis[i]=in[i]=false;
g[i].clear() ;
}
totd=top=ans=0;
return ;
}
void Input(void)
{
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v) ;
}
return ;
}
void js(int x)
{
low[x]=dfn[x]=++totd;
vis[x]=in[x]=true;
q[++top]=x;
for(int i=0;i<(int)g[x].size();i++)
{
int nice=g[x][i];
if(!vis[nice])
{
js(nice) ;
low[x]=min(low[x],low[nice]) ;
}
else if(in[nice])
{
low[x]=min(low[x],low[nice]) ;
}
}
if(dfn[x]==low[x])
{
int v=q[top];
do
{
v=q[top--];
in[v]=false;
}while(x!=v);
ans++;
}
return ;
}
void solve(void)
{
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
js(i) ;
}
}
if(ans==1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return ;
}
int main(void)
{
scanf("%d%d",&n,&m);
while(n||m)
{
compare() ;
Input() ;
solve() ;
scanf("%d%d",&n,&m);
}
return 0;
}