poj 1236 Network of Schools
来源:互联网 发布:视频特效制作软件 编辑:程序博客网 时间:2024/05/16 15:40
#include<iostream>#include<cstdio>#include<cstring>#define N 110#define M 5010using namespace std;struct node{ int to,next;}edge[M];int head[N],tot;int dfn[N],low[N],stap[N],belong[N];int in[N],out[N];bool instack[N];int index,bcnt,top;void init(){ tot=0; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn));memset(instack,false,sizeof(instack));memset(in,0,sizeof(in));memset(out,0,sizeof(out)); index=top=bcnt=0;}void insert(int u,int v){ edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;}void tarjan(int u){ dfn[u]=low[u]=++index;instack[u]=true; stap[++top]=u; bool flag=true; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(low[u]==dfn[u]) { int x; bcnt++; do{ x=stap[top--];instack[x]=false; belong[x]=bcnt; }while(x!=u); }}int main(){ int n; int i,j; int to; while(scanf("%d",&n)!=EOF) { init(); for(i=1;i<=n;i++) {while(scanf("%d",&to)&&to!=0){insert(i,to);} } for(i=1;i<=n;i++) { if(!dfn[i]) tarjan(i); } for(i=1;i<=n;i++){ for(j=head[i];j!=-1;j=edge[j].next) { int v=edge[j].to; if(belong[i]!=belong[v]){ out[belong[i]]++;in[belong[v]]++;} }}int ansa=0,ansb=0;for(i=1;i<=bcnt;i++){if(in[i]==0)ansa++;if(out[i]==0)ansb++;}printf("%d\n",ansa);if(bcnt==1)printf("0\n");elseprintf("%d\n",max(ansa,ansb)); } return 0;}
0 0
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- poj 1236 Network of Schools
- POJ-1236-Network of Schools
- POJ 1236 Network of Schools
- poj 1236 Network of Schools
- Network of Schools (POJ 1236)
- POJ 1236:Network of Schools
- poj 2762 Going from u to v or from v to u?
- hunnu11323(在n个点中,选择两个使得它们之间的距离最大)
- 教你透彻理解红黑树
- sizeof 操作符(c++primer)
- 用awakeFromNib来做view的初始化太有用了
- poj 1236 Network of Schools
- firefox icon图片显示异常
- 九度剑指Offer面试题10:二进制中1的个数(Java题解)
- POJ S-Nim
- php实现留言板程序功能
- java中的引用
- POJ 2492 A Bug's Life
- LOCAL_MODULE_TAGS 选项说明
- android常用知识收集