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(2<=n<=100)个学校,学校之间有单向网络,一个学校在得到软件之后可以顺着单向网络向其他学校传输
输入给出每个学校可以向其他学校传输的传输列表
1.初始时至少几个学校得到该软件才能使得网络内所有学校都收到该软件
2.至少添加几条边,才能使得向网络中任意一个学校发送该软件,最终能传遍网络中每一个学校
思路:
我们可以先进行缩点求出dag图,然后我们考虑第一个问题,求最少发几套软件可以全覆盖,首先题意已经保证了是联通的。然后我们可以想,如果我们把所有没有入边的点都放上软件,是一定可行的。有入边的一定会通过一些边最终从一定有出边的发放软件的地方获得软件。
然后我们考虑第二个问题。这是一个连通图。如果我们有些点没有入点,有些点没出点。那我们如果想办法将入点和一些出点相连,就能保证最后会成为很多圆相连。这样子答案就是没有入边的点和没有出边的点的最大值。
首先这是借鉴一位大佬的想法:
做这道题,需要用到的知识:
1、链式前向星,这个大佬写的很好。
2、tarjan算法。这个大佬超级详细。不愧全网最详细。
3、最后,再给tarjan出的每一个强连通分量,打上标记,计出入度。
将所有点缩成几个分量后,就可以看成是一个点,这时候考虑出入度进很简单了。
注意,如果只有一个分量,这道题的解是1和0.
AC代码:
idex=1
id=1#全局
idd=0
M=10000
def adde(x,y):#idd计数,做全局变量
global idd
e[idd][0]=y
e[idd][1]=head[x]
head[x]=idd
idd+=1
def tarjan(x):
global idex,id
s.append(x)
sin[x]=1
dfn[x]=idex
low[x]=idex
idex+=1
j=head[x]
while j!=-1:
y=e[j][0]
if(not dfn[y]):
tarjan(y)
low[x]=min(low[x],low[y])
elif sin[y]:
low[x]=min(low[x],dfn[y])
j=e[j][1]
if low[x]==dfn[x]:
while s[-1]!=x:
pre[s[-1]]=id
sin[s[-1]]=0
s.pop(-1)
pre[x]=id
sin[x]=0
s.pop(-1)
id+=1
n=int(input())
e=[[0,0]for i in range(M)]#to and next
head=[-1]*(n+1)
s=[]#zhan
sin=[0]*(n+1)#zai zhan ma
dfn=[0]*(n+1)
low=[0]*(n+1)
pre=[0]*(n+1)#在哪个连通分量里
out=[0]*(n+1)#连通分量出度
ru=[0]*(n+1)#连通分量ru度
for i in range(1,1+n):
a=[int(s) for s in input().split()]
for j in a:
if j!=0:
adde(i,j)
for i in range(1,n+1):
if not dfn[i]:
tarjan(i)
#print(pre)
if id==2:#只有一堆,任意一点,不用加边
print(1)
print(0)
else:
for i in range(1,n+1):
j=head[i]
while j!=-1:
v=e[j][0]
if pre[i]!=pre[v]:
out[pre[i]]+=1
ru[pre[v]]+=1
j=e[j][1]
s1=0#记录聚堆后出度=0的个数
s2=0#记录聚堆后入度=0的个数
for i in range(1,id):
if out[i]==0:
s1+=1
if ru[i]==0:
s2+=1
print(s2)
print(max(s1,s2))