标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10156 Accepted Submission(s): 4570
看 了两天还是没有看懂 但是我迫切想A一道题,就看着人家的写喽,明天继续看
#include<stdio.h> #include<string.h> #include<stack> #include<algorithm> #define MAX 10010 #define MAXM 100010 using namespace std; int ans; int n,m; int low[MAX],dfn[MAX]; int instack[MAX],scccnt; int clock; stack<int>s; struct node { int beg,end,next; }edge[MAXM]; int head[MAX]; void add(int beg,int end) { edge[ans].beg=beg; edge[ans].end=end; edge[ans].next=head[beg]; head[beg]=ans++; } void init() { memset(head,-1,sizeof(head)); ans=0; } void tarjan(int u,int fa) { int v; low[u]=dfn[u]=++clock; s.push(u); instack[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { v=edge[i].end; if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { scccnt++; while(1) { v=s.top(); s.pop(); instack[v]=0; if(v==u) break; } } } void solve(int l,int r) { memset(instack,0,sizeof(instack)); memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); scccnt=clock=0; for(int i=l;i<=r;i++) if(!dfn[i]) tarjan(i,-1); if(scccnt==1) printf("Yes\n"); else printf("No\n"); } int main() { int i,j,a,b; while(scanf("%d%d",&n,&m),n|m) { init(); while(m--) { scanf("%d%d",&a,&b); add(a,b); } solve(1,n); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4811681.html