B - 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
给你n个点,再告诉你所有的边
问你最少投放几个点可以走完所有的点
问你最少再连几条边,可以从任意一点走到所有点
第一个缩点后求有多少入度为0的点
第二个缩点后max(入度为0,出度为0)
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
#define maxn 10010
int n;
vector<int> g[maxn];
int Bcnt; // 强连通分量的个数;
int Top; // 栈顶;
int Index; // 时间戳;
int low[maxn], dfn[maxn];
int belong[maxn], Stack[maxn];
bool instack[maxn];
int in[maxn],out[maxn];
void Init_tarjan()
{
Bcnt= Top= Index= 0;
for(int i=1; i<=n; i++)
{
low[i]= dfn[i]= 0;
}
}
void Tarjan(int u)
{
Stack[Top++]= u;
instack[u]= 1;
low[u]= dfn[u]= ++Index;
for(int i=0; i<g[u].size(); i++)
{
int v= g[u][i];
if( !dfn[v] )
{
Tarjan(v);
low[u]= min( low[v], low[u] );
}
else if( instack[v] )
low[u]= min( low[u], dfn[v] );
}
if( low[u]==dfn[u] )
{
++Bcnt;
int v;
do{
v= Stack[--Top];
instack[v]= 0;
belong[v]= Bcnt;
}while( u!=v );
}
}
int main()
{
while(~scanf("%d",&n))
{
Init_tarjan();
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=1;i<=n;i++)
{
int x;
while(scanf("%d",&x))
{
if(x==0) break;
g[i].push_back(x);
}
}
int t=0;
for(int i=1;i<=n;i++)
{
if(!dfn[i]) Tarjan(i);
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<g[i].size();j++)
{
int v=g[i][j];
if(belong[v]==belong[i]) continue;
else
{
out[belong[i]]++,in[belong[v]]++;
}
}
}
// for(int i=1;i<=n;i++) printf("%d\n",belong[i]);
int sum_in=0,sum_out=0;
for(int i=1;i<=Bcnt;i++)
{
if(!in[i]) sum_in++;
if(!out[i]) sum_out++;
}
//cout<<sum_in<<" "<<sum_out<<endl;
int sum=max(sum_in,sum_out);
for(int i=1; i<=n; i++)
{
g[i].clear();
}
if(Bcnt==1) printf("1\n0\n");//!!!Bcnt不是n
else printf("%d\n%d\n",sum_in,sum);
}
return 0;
}