·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
題目來源:http://acm.hdu.edu.cn/showproblem.php?pid=1269
這題是果的強聯通,沒有什麼鯁
Tarjan(i)會求出與i聯通的所有強聯通分量。
彈棧時,不要彈空整個棧,而是彈出整棵搜索子樹。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=11000,M=110000;
int ver[M<<1],nxt[M<<1],head[N],tot=1,cnt=0;
int dfn[N],low[N],t=0;
bool inq[N],fail;
int q[N],top=0;
int n,m;
void add (int u,int v) {
ver[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
void Tarjan (int x) {
dfn[x]=low[x]=++t;
inq[x]=1;q[++top]=x;
for (int i=head[x];i;i=nxt[i])
if (!dfn[ver[i]]) {//
Tarjan(ver[i]);
low[x]=min(low[x],low[ver[i]]);
}
else if (inq[ver[i]]) low[x]=min(low[x],dfn[ver[i]]);//
if (low[x]==dfn[x]) {
int now=0;
while (now!=x&&top) {//
now=q[top--];
++cnt;
inq[now]=0;
}
if (cnt!=n) fail=1;
}
}
int main () {
int u,v;
while (scanf("%d%d",&n,&m)!=EOF) {
if (n==0&&m==0) break;
cnt=t=top=0,tot=1;fail=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(inq,0,sizeof(inq));
memset(head,0,sizeof(head));
for (int i=1;i<=m;i++) {
scanf("%d%d",&u,&v); add(u,v);
}
Tarjan(1);//This will handle all the verticles that are connected to the node 1
if (!fail) puts("Yes");
else puts("No");
}
}