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
https://www.byvoid.com/zhs/blog/scc-tarjan
#include
#include
#include
#include
#include
#include
#define nn 110
#define inff 0x3fffffff
using namespace std;
int n,num,d,cnt;
struct node
{
int en,next;
}E[nn*nn];
int dfn[nn],low[nn],p[nn],fa[nn],vis[nn];
int init()
{
memset(p,-1,sizeof(p));
num=0;
}
int add(int st,int en)
{
E[num].en=en;
E[num].next=p[st];
p[st]=num++;
}
int cd[nn],rd[nn],df;
stacks;
int dfs(int id)
{
dfn[id]=low[id]=++df;//为节点设定次序编号和low初值
vis[id]=true;
s.push(id);
int w;
for(int i=p[id];i+1;i=E[i].next)//枚举每一条边
{
w=E[i].en;
if(dfn[w]==-1)//如果未被访问
{
dfs(w);//继续向下找
low[id]=min(low[id],low[w]);
}
else if(vis[w])//如果还在栈中
low[id]=min(low[id],dfn[w]);
}
if(dfn[id]==low[id])//如果节点id是强连通分量的根
{
++cnt;
int ix;
while(1)
{
ix=s.top();
s.pop();//讲ix退栈,为强连通分量的一个顶点
vis[ix]=false;
fa[ix]=cnt;
if(ix==id)
break;
}
}
}
int slove()
{
int w;
memset(cd,0,sizeof(cd));
memset(rd,0,sizeof(rd));
memset(dfn,-1,sizeof(dfn));
memset(vis,false,sizeof(vis));
df=0;
cnt=0;
for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
dfs(i);
}
for(int i=1;i<=n;i++)
{
for(int j=p[i];j+1;j=E[j].next)
{
w=E[j].en;
if(fa[w]!=fa[i])
{
cd[fa[i]]++;
rd[fa[w]]++;
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
init();
for(int i=1;i<=n;i++)
{
while(~scanf("%d",&d)&&d)
{
add(i,d);
}
}
slove();
int ans1=0,ans2=0;
for(int i=1;i<=cnt;i++)
{
if(rd[i]==0)
ans1++;
if(cd[i]==0)
ans2++;
}
printf("%d\n",ans1);
if(cnt==1)
ans1=ans2=0;
printf("%d\n",max(ans1,ans2));
}
}