hdu 1269即模板题,缩完点后判断点数是否为1即可。
图论题一定要记着memset(head,-1,sizeof(head))(如果题目不含0号点也可以memset成0),否则就坐等TLE吧。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
using namespace std;
const int maxn=10004,maxm=100004;
stack<int> S;
int n,m;
int head[maxn],nxt[maxm],cnt,dest[maxm];
int low[maxn],dfn[maxn],scnt;
bool ins[maxn];
void init() {
scnt=cnt=0;
memset(ins,false,sizeof(ins));
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
}
void adde(int from,int to) {
nxt[++cnt]=head[from];
dest[cnt]=to;
head[from]=cnt;
}
void dfs(int p) {
S.push(p);
ins[p]=1;
low[p]=dfn[p]=++cnt;
for (int i=head[p];~i;i=nxt[i]) {
int j=dest[i];
if (!dfn[j]) {
dfs(j);
low[p]=min(low[p],low[j]);
}
else if (ins[j]) {
low[p]=min(low[p],dfn[j]);
}
}
if (dfn[p]==low[p]) {
++scnt;
while (!S.empty()) {
int t=S.top();
ins[t]=0;
S.pop();
if (t==p) break;
}
}
}
void tarjan() {
for (int i=1;i<=n;i++)
if (!dfn[i]) dfs(i);
}
int main() {
while (scanf("%d%d",&n,&m)&&(n||m)) {
int u,v;
init();
while (m--) {
scanf("%d%d",&u,&v);
adde(u,v);
}
tarjan();
if (scnt==1) printf("Yes\n");
else printf("No\n");
}
return 0;
}