POJ1236--Network of Schools(强连通+缩点)
来源:互联网 发布:在线制作名片软件 编辑:程序博客网 时间:2024/06/10 18:06
第一问:至少需要多少份软件,才能使得所有学校都能拥有软件;第二问:如果只用一份软件,那么需要添加多少条变,使得所有学校都能拥有软件。
第一问中,如果一个学校有用一份软件,那么它的强连通分量中的其他学校,肯定也能够拥有软件,所以只要求出出度为0的强连通分量的个数即可。
第二问中,就是求需要添加多少条变,使得整个图都成为一个强连通,即任意两个学校都可到达,所以我们只要求出出度为0的个数a,和入度为0的个数b,取两者的最大值即可。
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <math.h>#include <stack>#include <vector>#define M 105using namespace std;int dfn[M],low[M],scc_cnt,index;int sccno[M];stack<int> s;vector<int> G[M];void Tarjan(int u){dfn[u]=low[u]=++index;s.push(u);for(int i=0;i<G[u].size();i++){int v=G[u][i];if(!dfn[v]){Tarjan(v);low[u]=min(low[u],low[v]);}else if(!sccno[v]){low[u]=min(low[u],dfn[v]);}}if(low[u]==dfn[u]){scc_cnt++;for(;;){int x=s.top();s.pop();sccno[x]=scc_cnt;if(x==u)break;}}}void find_scc(int n){index=scc_cnt=0;memset(sccno,0,sizeof(sccno));memset(dfn,0,sizeof(dfn));for(int i=0;i<n;i++)if(!dfn[i])Tarjan(i);}int main(){int n;int in0[M],out0[M];while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)G[i].clear();for(int i=0;i<n;i++){int v;while(scanf("%d",&v)){if(v==0)break;v--;G[i].push_back(v);}}find_scc(n);for(int i=1;i<=scc_cnt;i++)in0[i]=out0[i]=1;for(int u=0;u<n;u++){for(int i=0;i<G[u].size();i++){int v=G[u][i];if(sccno[u]!=sccno[v])in0[sccno[v]]=out0[sccno[u]]=0;}}int a=0,b=0;for(int i=1;i<=scc_cnt;i++){if(in0[i])a++;if(out0[i])b++;}int ans=max(a,b);if(scc_cnt==1)ans=0;printf("%d\n%d\n",a,ans);}return 0;}
0 0
- POJ1236--Network of Schools(强连通+缩点)
- POJ1236 Network of Schools 强连通+缩点
- poj1236 Network of Schools(强连通-缩点)
- POJ1236 Network of Schools 强连通+缩点
- POJ1236 Network of Schools(缩点+结论+强连通分量)
- POJ1236:Network of Schools(强连通)
- [POJ1236]Network of Schools(Tarjan缩点+强连通分量)
- poj1236——Network of Schools(强连通分量+缩点)
- poj1236 - Network of Schools(强连通分量Garbow模板+缩点)
- POJ1236---Network of Schools (强连通分量,缩点,Tarjan算法)
- 【强连通分量】Tarjan(缩点)POJ1236-Network of Schools
- poj1236-network of schools(强连通分量)
- poj1236 Network of Schools 强连通分量
- POJ1236 Network of Schools 【强连通分量】
- poj1236 - Network of Schools (强连通图)
- poj1236 Network of Schools(强连通分量,tarjan)
- poj1236 Network of Schools (Targan强连通图缩点)
- poj1236 Network of Schools--Kosaraju算法 & 缩点 & 强连通分量
- C#打开系统图片查看器查看图片
- linux下修复grub2
- Hadoop集群(第9期)_MapReduce初级案例
- winpcap编程 解析数据包
- 我是怎么招聘程序员的
- POJ1236--Network of Schools(强连通+缩点)
- web开发学习(一) Div+CSS网页设计学习
- 高效能程序员的修炼
- https不能访问时的解决方案
- iOS 关键字 self 的用法
- android 查看联网状态
- Hadoop集群(第10期)_MySQL关系数据库
- 网友吐槽12306网站 专家:已接近技术极限
- Hadoop集群(第10期副刊)_常用MySQL数据库命令