poj 1236 强连通分量缩点求入度为0出度为0的分量个数 kosaraju算法
来源:互联网 发布:jquery ajax json分页 编辑:程序博客网 时间:2024/06/02 15:01
B - Network of Schools
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.
Output
Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input
52 4 3 04 5 0001 0
Sample Output
12
题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题
1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
解题思路:
首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么第一问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点能够找到然后第二问添加多少条边可以得到使整个图达到一个强连通分量,答案是入度为0的个数和出度为0的个数中最大的
那个,为什么会这样呢,经过我同学的讨论,将这个图的所有子树找出来,然后将一棵子树的叶子结点(出度为0)连到另外一棵子树的根结点上(入度为0),这样将所有的叶子结点和根节点全部消掉之后,就可以得到一整个强连通分量,看最少多少条边,这样就是看叶子结点和根节点哪个多,即出度为0和入度为0哪个多ACcode:
#include <iostream>#include<cstdio>#include<string.h>#define Maxn 110using namespace std;int map[Maxn][Maxn],order[Maxn],belong[Maxn],indegree[Maxn],outdegree[Maxn];int n,num,count;bool vis[Maxn];void dfs(int x){ int i; vis[x]=1; for(i=1;i<=n;i++) if(map[x][i]&&!vis[i]) dfs(i); order[++num]=x;}void dfst(int x){ int i; belong[x]=count;//记录结点属于哪个连通分量 vis[x]=1; for(i=1;i<=n;i++) if(map[i][x]&&!vis[i]) dfst(i);}void kosaraju(){ int i; memset(vis,0,sizeof(vis)); num=count=0; for(i=1;i<=n;i++) if(!vis[i]) dfs(i); memset(vis,0,sizeof(vis)); for(i=n;i>=1;i--) if(!vis[order[i]]) { count++;//连通分量个数 dfst(order[i]); }}void output(){int i,j,inzero=0,outzero=0;for(i=1;i<=n;i++){indegree[i]=outdegree[i]=0;}for(i=1;i<=n;i++)//找连通分量入度与出度for(j=1;j<=n;j++)if(map[i][j] && belong[i]!=belong[j]){indegree[belong[j]]++;outdegree[belong[i]]++;}for(i=1;i<=count;i++){//找入度与出度为0的点if(!indegree[i]) inzero++;if(!outdegree[i]) outzero++;}if(count==1)//只有1个结点要特判printf("1\n0\n");elseprintf("%d\n%d\n",inzero,max(inzero,outzero));}int main(){ int i,a; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { while(scanf("%d",&a)&&a) map[i][a]=1; } kosaraju(); output(); } return 0;}
0 0
- poj 1236 强连通分量缩点求入度为0出度为0的分量个数 kosaraju算法
- poj2182 强连通分量+缩点+出度为0
- POJ 2186 Kosaraju算法 强连通分量
- hdu2767 等价性证明(求强连通分量缩点后的出度和入度为0的个数)
- poj 2186(强连通分量的kosaraju算法)
- POJ 1236(强连通分量+Kosaraju)
- 强连通分量的Kosaraju算法
- 强连通分量 的 Kosaraju算法
- 强连通分量 Kosaraju算法
- 强连通分量 -- Kosaraju算法
- 强连通分量-kosaraju算法
- 强连通分量 Kosaraju算法
- 强连通分量(Kosaraju算法)
- POJ 2186 Popular Cows 强连通分量 Kosaraju算法
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
- 强连通分量算法Kosaraju 和 Tarjan
- 强连通分量——kosaraju算法
- Kosaraju算法---求解强连通分量
- 巧用Ajax的beforeSend 提高用户体验
- solrJ
- BOA安装,WEB控制
- online_judge_1020
- android(18)(内容观察者,监听系统短信)
- poj 1236 强连通分量缩点求入度为0出度为0的分量个数 kosaraju算法
- Fedora 23(Server Edition)中构建Mail Server(Postfix+Dovecot)
- ios_Cocoa框架 “Foundation”和“UIKit”的区别
- ZooKeeper 工作原理
- java中TreeMap统计字符个数
- hibernate配置详解笔记
- Python break 语句
- 初学链表第一天总结笔记
- Java 泛型(一) 泛型使用基础