【POJ1236】Network of Schools(Tarjan缩点)
来源:互联网 发布:知乎 资产比例 编辑:程序博客网 时间:2024/05/22 07:26
题目:
我是超链接
题意:
Q1:求入度为0的强连通分量的个数
Q2:入度为0的强连通分量的个数 与 出度为0的强连通分量的个数取max(这里记得特判num=1的情况)
代码:
#include <cstdio>#include <iostream>#include <cstring>#define M 1000005#define N 100005using namespace std;int nxt[M*2],point[M*2],v[M*2],tot,tmp,n,m,NN,num;int dfn[N],low[N],strack[N],bb[N],x[N],y[N],out[N],belong[N],in[N];bool vis[N];void addline(int x,int y){++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;}void tarjan(int now){dfn[now]=low[now]=++NN; vis[now]=1; strack[++tmp]=now;for (int i=point[now];i;i=nxt[i]) if (!dfn[v[i]]) { tarjan(v[i]); low[now]=min(low[now],low[v[i]]); } else if (vis[v[i]]) low[now]=min(low[now],dfn[v[i]]);if (low[now]==dfn[now]){num++;while (strack[tmp]!=now){belong[strack[tmp]]=num;vis[strack[tmp]]=0; tmp--;}belong[strack[tmp]]=num;vis[strack[tmp]]=0; tmp--;}}int main(){int n,i,s,m=0,inn=0,outt=0;scanf("%d",&n);for (i=1;i<=n;i++){scanf("%d",&s);while (s!=0){x[++m]=i;y[m]=s;addline(x[m],y[m]);scanf("%d",&s);}}for (i=1;i<=n;i++) if (!dfn[i]) tarjan(i);for (i=1;i<=m;i++) if (belong[x[i]]!=belong[y[i]]) ++in[belong[y[i]]],++out[belong[x[i]]];for (i=1;i<=num;i++){if (!in[i]) inn++;if (!out[i]) outt++;}if (num==1) printf("1\n0");else printf("%d\n%d",inn,max(inn,outt));}
1 0
- POJ1236-Network of Schools(Tarjan + 缩点)
- poj1236 Network of Schools(tarjan缩点)
- 【POJ1236】Network of Schools(Tarjan缩点)
- poj1236 Network of Schools(tarjan缩点)
- POJ1236 Network of Schools,Tarjan
- POJ1236 Network of Schools (Tarjan)
- Network of Schools --POJ1236 Tarjan
- poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点
- [POJ1236]Network of Schools(Tarjan缩点+强连通分量)
- POJ1236---Network of Schools (强连通分量,缩点,Tarjan算法)
- 【强连通分量】Tarjan(缩点)POJ1236-Network of Schools
- POJ1236--Network of Schools(强连通+缩点)
- Network of Schools POJ1236(tarjan缩点+强连通分量模板)
- poj1236 Network of Schools(强连通分量,tarjan)
- poj1236 Network of Schools tarjan算法
- poj1236 Network of Schools (tarjan缩点+求入度为0的点和出度为0的点的个数)
- POJ1236 Network of Schools 强连通+缩点
- poj1236 Network of Schools(强连通-缩点)
- web登录
- eayUI中测试《一》获取数据和展示在列表中的数据展示,删除数据,修改数据,增加数据
- Problem G: 字符串类(II)
- 深入理解ServletRequest与ServletResponse
- 【Java工具类】----产生随机数
- 【POJ1236】Network of Schools(Tarjan缩点)
- C/C++快速排序
- oracle异常处理
- 【Java工具类】----读取.prop配置文件
- 经典面试题(五)
- Centos7下安装nginx过程总结
- 文件指针FILE*及文件描述符fd
- 标注 @JsonInclude(Include.NON_NULL)
- Mac OS tensorflow的环境搭建