poj-1236 Network Of Schools【强连通缩点】
来源:互联网 发布:淘宝漏洞券 编辑:程序博客网 时间:2024/05/21 12:49
/*问题A:选择几个点作为起点发送软件,能最少选择几个起点保证所有学校都能收到问题B:添几条边能使任意一个点作为起点发送软件,让所有学校都能收到解决思路:有向图中强连通分量中任意两点两两可达,所以B就是要把图添最少的边构成一个大的强连通分量。假如把图中所有强连通分量缩点,就可以得到一个有向无环图(DAG图)。DAG图中的0入度点的数量就是A的答案。假如DAG图0入度点向0出度点添边,就能构成一个大环,所以B的答案就是 0入度点和0出度点的最大值*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <vector>#define MAX_SIZE 110using namespace std;vector<int> g[MAX_SIZE];//正向图vector<int> rg[MAX_SIZE];//反向图vector<int> rs;//标号表int n;int used[MAX_SIZE];int cmp[MAX_SIZE];int indeg[MAX_SIZE];int outdeg[MAX_SIZE];int find_max(int x,int y){return x>y?x:y;}void add_edge(int u,int v){g[u].push_back(v);rg[v].push_back(u);}void dfs(int u){used[u]=1;for(int i=0;i<g[u].size();++i){int to=g[u][i];if(!used[to])dfs(to);}rs.push_back(u);}void rdfs(int u,int k){used[u]=1;cmp[u]=k;for(int i=0;i<rg[u].size();++i){int to=rg[u][i];if(!used[to])rdfs(to,k);}}void init(){for(int i=0;i<MAX_SIZE;i++){g[i].clear();rg[i].clear();}}void solve(){//正向搜索一次memset(used,0,sizeof(used));rs.clear();for(int i=1;i<=n;++i)if(!used[i])dfs(i);int k=0;//反向搜索一次memset(used,0,sizeof(used));for(int j=rs.size()-1;j>=0;--j){//printf("%d\n",rs[j]);if(!used[rs[j]])rdfs(rs[j],++k);}memset(indeg,0,sizeof(indeg));memset(outdeg,0,sizeof(outdeg));//记录缩点后的图的每个点的出度和入读for(int u=1;u<=n;++u){for(int v=0;v<g[u].size();++v){int to=g[u][v];if(cmp[to]!=cmp[u]){++indeg[cmp[to]];++outdeg[cmp[u]];}}}//得到0出度的点数,和0入度的点数int s_count=0,t_count=0;for(int t=1;t<=k;++t){//printf("i:%d iu:%d out:%d\n",t,indeg[t],outdeg[t]);if(!outdeg[t])++t_count;if(!indeg[t])++s_count;}if(k==1){printf("1\n0\n");return;}//printf("%d %d\n",s_count,t_count);printf("%d\n",s_count);printf("%d\n",find_max(s_count,t_count));}int main(){while(~scanf("%d",&n)){init();for(int i=1;i<=n;i++){int to;while(1){scanf("%d",&to);if(to==0)break;add_edge(i,to);}}solve();}return 0;}
0 0
- POJ 1236 Network of Schools tarjan强连通缩点
- 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【强连通缩点】【Tarjan算法】
- 【强连通缩点】Network of Schools POJ1236
- 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(强连通分量缩点)
- nodejs 快速实现url图片重定向
- Just Do It
- Asp.Net MVC 教程汇总
- <a>标签设置在新窗口中打开
- oracle 00845,修改memory_target大小
- poj-1236 Network Of Schools【强连通缩点】
- logback logback.xml常用配置详解(一)<configuration> and <logger>
- [转载]你可能不知道的一些JavaScript 奇技淫巧
- apt-get 版本不一致安装错误解决方案
- Android学习笔记-ScollView实现QQ消息界面滑动效果
- 关于C++中结构体及其指针成员的内存分配和释放
- 安卓网络通信框架Volley学习(一) Volley简介和初步使用
- 动态添加碎片
- 小知识1 系统中的默认值