Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)...

本文探讨了如何在学校的计算机网络中优化软件分发流程,包括确定最少的软件接收节点以覆盖所有学校,并通过增加最少数量的链接实现网络的强连通性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13801 Accepted: 5505

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
题解:

    1. /题意:  
    2. //给你一个N个点的有向图,问1,最少连接几个点可以直接或间接 连接到所有点。  
    3. //                         2,最少增加几条边使图强连通。  
    4. //思路SCC + 缩点后。统计出入度为0的SCC数sumin和出度为0的SCC数sumout。  
    5. //答案一就是sumin,答案二就是max(sumin, sumout)。注意只有一个SCC时,输出1 0。 
代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<vector> 
 7 #include<stack>
 8 #define mem(x,y) memset(x,y,sizeof(x))
 9 using namespace std;
10 const int INF=0x3f3f3f3f;
11 const int MAXN=110;
12 vector<int>vec[MAXN];
13 stack<int>S;
14 int dfn[MAXN],low[MAXN],Instack[MAXN],in[MAXN],out[MAXN];
15 int dfs_blocks,scc,sccon[MAXN];
16 void targin(int u,int fa){
17     S.push(u);
18     Instack[u]=1;
19     dfn[u]=low[u]=++dfs_blocks;
20     for(int i=0;i<vec[u].size();i++){
21         int v=vec[u][i];
22         if(!dfn[v]){
23             targin(v,u);
24             low[u]=min(low[u],low[v]);
25         }
26         else if(Instack[v]){
27             low[u]=min(low[u],dfn[v]);
28         }
29     }
30     if(low[u]==dfn[u]){
31         scc++;
32         while(1){
33             int v=S.top();
34             S.pop();
35             Instack[v]=0;
36             sccon[v]=scc;
37             if(u==v)break;
38         }
39     }
40 }
41 int main(){
42     int N;
43     while(~scanf("%d",&N)){
44         scc=0;
45         for(int i=0;i<MAXN;i++)vec[i].clear();
46         while(!S.empty())S.pop();
47         mem(in,0);mem(out,0);mem(Instack,0);
48         mem(dfn,0);mem(low,0);mem(sccon,0);
49         for(int i=1;i<=N;i++){
50             int x;
51             while(scanf("%d",&x),x!=0){
52                 vec[i].push_back(x);
53             }
54         }
55         for(int i=1;i<=N;i++){
56             if(!dfn[i]){
57                 targin(i,-1);
58             }
59         }
60         for(int i=1;i<=N;i++){
61             for(int j=0;j<vec[i].size();j++){
62                 int v=vec[i][j];
63                 if(sccon[i]!=sccon[v])in[sccon[v]]++,out[sccon[i]]++;
64             }
65         }
66     //    printf("%d\n",scc);
67         int numin=0,numout=0;
68         if(scc==1){
69             puts("1\n0");continue;
70         }
71         for(int i=1;i<=scc;i++){
72             if(in[i]==0)numin++;
73             if(out[i]==0)numout++;
74         }
75         printf("%d\n%d\n",numin,max(numin,numout));
76     }
77     return 0;
78 }

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值