Network of Schools POJ - 1236
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.
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
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
题目大意:一些学校连成了网络, 在学校之间存在某个协议:每个学校都维护一张传送表,表明他们要负责将收到的软件传送到表中的所有学校。如果A在B的表中,那么B不一定在A的表中。
现在的任务就是,给出所有学校及他们维护的表,问1、如果所有学校都要被传送到,那么需要几份软件备份;2、如果只用一份软件备份,那么需要添加几条边?
首先我们应该缩点,即把强连通图缩成一个点,这时候我们会得到几棵树,问题1是问至少需要几份软件备份,也就是求出树的个数,即入度==0的点有几个(即根)。问题二是问需要添几条边,即我们需要把所有的树连起来,并且应该没有叶子节点。所以我们可以把叶子节点和根节点连接起来。max(叶子节点,根节点)。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200000;
struct node
{
int v,next;
}edge[maxn];
int head[maxn],cnt,block,in[maxn],out[maxn];
int low[maxn],dfn[maxn],stack[maxn],instack[maxn],belong[maxn];
int index,top;
int n;
void init()
{
memset(head,-1,sizeof(head));
cnt=block=0;
index=top=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(stack,0,sizeof(stack));
memset(instack,0,sizeof(instack));
memset(belong,0,sizeof(belong));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
}
void add_edge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
instack[u]=1;
stack[top++]=u;
int v;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
block++;
do
{
v=stack[--top];
instack[v]=0;
belong[v]=block;
}while(u!=v);
}
}
void solve()
{
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
//printf("%d\n",block);
if(block==1)
{
printf("1\n0\n");
return;
}
int v;
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=edge[j].next)
{
v=edge[j].v;
if(belong[i]!=belong[v])
{
in[belong[v]]++;
out[belong[i]]++;
}
}
}
int ans1=0,ans2=0;
for(int i=1;i<=block;i++)
{
if(in[i]==0) ans1++;
if(out[i]==0) ans2++;
}
printf("%d\n",ans1);
printf("%d\n",max(ans1,ans2));
}
int main ()
{
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
{
while(1)
{
int xx;
scanf("%d",&xx);
if(xx==0)
{
break;
}
add_edge(i,xx);
}
}
solve();
}
}