栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1979 The Bottom of a Graph

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1979 The Bottom of a Graph

#include <stdio.h>  #include <iostream>  #include <string.h>  #include <memory.h>  #include <stack>  #include <algorithm>  #define MAX 5010  using namespace std;  struct NODE   {      int to;      NODE *next;  }*v[MAX],node[MAX*5];  int n,e;  stack<int> mstack;  int inStack[MAX],dfn[MAX],low[MAX],whichComponent[MAX];  int dag_out[MAX];  int ind,componentNum,nodeind;  void init(void)  {      ind=0;      componentNum=0;      nodeind=0;      memset(v,'',sizeof(v));      memset(inStack,0,sizeof(inStack));      memset(dfn,-1,sizeof(dfn));      memset(low,-1,sizeof(low));      memset(whichComponent,0,sizeof(whichComponent));      memset(dag_out,0,sizeof(dag_out));      while(!mstack.empty())mstack.pop();  }  void input(void)  {      int from,to;      for(int i=0;i<e;++i)      {          scanf("%d %d",&from,&to);          node[nodeind].to=to;          node[nodeind].next=v[from];          v[from]=&node[nodeind++];      }  }  void tarjan(int i)  {      int to;      dfn[i]=low[i]=ind++;      inStack[i]=1;      mstack.push(i);      NODE *head=v[i];      while(head)      {          to=head->to;          if(dfn[to]==-1)          {   tarjan(to);   low[i]=min(low[i],low[to]);          }          else if(inStack[to])          {   low[i]=min(low[i],dfn[to]);          }          head=head->next;      }      if(dfn[i]==low[i])      {          do{   to=mstack.top();   inStack[to]=0;   mstack.pop();   whichComponent[to]=componentNum;          }while(i!=to);          ++componentNum;      }  }  void solve(void)  {      for(int i=1;i<=n;++i)          if(dfn[i]==-1)   tarjan(i);      for(int i=1;i<=n;++i)      {          NODE *head=v[i];          while(head)          {   if(whichComponent[i]!=whichComponent[head->to])       ++dag_out[whichComponent[i]];   head=head->next;          }      }      int flag=1;      for (int i=1;i<=n;++i)          if (!dag_out[whichComponent[i]])          {   if(flag)       flag=0;   else       printf(" ");   printf("%d",i);          }      printf("n");  }  int main(void)  {        while(scanf("%d",&n)&&n)      {          init();          scanf("%d",&e);          input();          solve();      }      return 0;  }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号