POJ1236Network of Schools(tarjan 算法)

来源:互联网 发布:网络打印机服务器脱机 编辑:程序博客网 时间:2024/06/06 02:23

题目描述

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.

输入格式

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.

输出

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.

样例输入

5
2 4 3 0
4 5 0
0
0
1 0

样例输出

1
2

算法讲解:https://www.byvoid.com/blog/scc-tarjan/

题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题
1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
2:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
解题思路:
首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点能够找到
然后第二问添加多少条边可以得到使整个图达到一个强连通分量,答案是入度为0的个数和出度为0的个数中最大的
那个,为什么会这样呢,经过我同学的讨论,将这个图的所有子树找出来,然后将一棵子树的叶子结点(出度为0)连到另外一棵子树的根结点上(入度为0),这样将所有的叶子结点和根节点全部消掉之后,就可以得到一整个强连通分量,看最少多少条边,这样就是看叶子结点和根节点哪个多,即出度为0和入度为0哪个多
我只能说这种策略是正确的,适用于一般图,但其实我觉得也不能够很有力的证明这个结论成立

(题目大意及解题思路转自:http://blog.csdn.net/wangjian8006/article/details/7888558)感谢!!吐舌头

参考代码:

#include<iostream>#include<algorithm>#include<stack>#include<cstdio>using namespace std;const int N=303;struct edgeT{//可以用结构体,也可以用数组。int adj;edgeT *next;};struct node{int low,dfn,belong,i,o;bool isroot;//<span style="font-family:'Lucida Console';">判断是不是根用,一个强连通分量有一个根</span>edgeT *head;};int n,idx=0;node g[N]={0};bool instack[N]={0};stack<int> q;void init(){//链表<span style="font-family:'Lucida Console';">建图</span>edgeT* p;int x;scanf("%d",&n);for (int v=1;v<=n;v++)while (scanf("%d",&x)&&x){p=new(edgeT); p->adj=x; p->next=g[v].head; g[v].head=p;}for (int i=1;i<=n;i++) g[i].belong=i,g[i].isroot=false,g[i].i=g[i].o=0;}void tarjan(int u){<span style="font-family:'Lucida Console';">//tarjan算法</span>q.push(u);instack[u]=true;g[u].dfn=g[u].low=++idx;int v;edgeT*p=g[u].head;while (p){v=p->adj;if (!g[v].dfn){tarjan(v);g[u].low=min(g[u].low,g[v].low);}else if (instack[v]) g[u].low=min(g[u].low,g[v].dfn);p=p->next;}if (g[u].low==g[u].dfn){do{v=q.top();q.pop();instack[v]=false;g[v].belong=u;}while (v!=u);g[u].isroot=true;}}void update(){//缩点操作并统计入度及出度edgeT* p;for (int i=1;i<=n;i++){p=g[i].head;while (p){if (g[i].belong!=g[p->adj].belong)  g[g[i].belong].o++,g[g[p->adj].belong].i++;p=p->next;}}}void print(){//统计出度为0和入度为0的int in0=0,out0=0,num=0;for (int u=1;u<=n;u++){if (g[u].isroot){if (!g[u].i) in0++;if (!g[u].o) out0++;num++;}}if(in0) printf("%d\n",in0);    if(num==1) printf("0\n");    else printf("%d\n",in0>out0?in0:out0);}int main(){init();for (int i=1;i<=n;i++)  if (!g[i].dfn) tarjan(i);update();print(); return 0;}


0 0
原创粉丝点击