Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions:20899 | Accepted: 8245 |
Description
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
題解:
強連通分量縮點,變成了一張無環圖,入度為0的點的個數
便是需要的起點個數,入度為0的點的個數與出度為0的點
的個數的最大值便是需要添加的邊數。
代碼:
#include<cstdio>
#include<stack>
#include<string.h>
using namespace std;
const int maxn=10007;
int head[maxn],out[maxn],in[maxn],dfn[maxn],low[maxn],tol;
int from[maxn],to[maxn],ans,num,par[maxn];
int vis[maxn];
stack<int>P;
struct node
{
int to,next;
} rode[maxn*maxn];
void add(int a,int b)
{
rode[tol].to=b;
rode[tol].next=head[a];
head[a]=tol++;
}
void tarjan(int x)
{
P.push(x);
vis[x]=1;
dfn[x]=low[x]=ans++;
for(int i=head[x]; i!=-1; i=rode[i].next)
{
node e=rode[i];
if(vis[e.to]==0)tarjan(e.to);
if(vis[e.to]==1)low[x]=min(low[x],low[e.to]);
}
if(dfn[x]==low[x])//縮點
{
num++;
int v;
do
{
v=P.top();
vis[v]=2;
P.pop();
par[v]=num;//同一強連通分量中的點看作一個點
}
while(v!=x);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int i,j,cnt=0;
tol=0;ans=num=0;
memset(head,-1,sizeof(head));
for(i=1; i<=n; i++)
{
int x;
while(~scanf("%d",&x))
{
if(!x)break;
add(i,x);
from[cnt]=i;
to[cnt++]=x;
}
}
memset(vis,0,sizeof(vis));
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
for(i=1; i<=n; i++)
{
while(!P.empty())
P.pop();
if(!vis[i])tarjan(i);
}
for(i=0; i<cnt; i++)
{
int x=from[i],y=to[i];
if(par[x]!=par[y])
{
out[par[x]]++;
in[par[y]]++;
}
}
int ans1=0,ans2=0;
for(i=1; i<=num; i++)
{
if(in[i]==0)ans1++;
if(out[i]==0)ans2++;
}
if(num==1)printf("1\n0\n");//整個圖就是一個強連通圖的情況需要特判。
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return 0;
}