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

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 }
				
时间: 2024-10-17 00:38:00

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

POJ 1236 Network of Schools (强连通分量缩点求度数)

题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他地方到达. 对于问题2, 每个入度为0的scc, 都可以补一条边可以变成强连通图, 每个出度为0的scc, 也可以补一条边使其变成强连通图. 所以答案就是max(入度为0scc个数,出度为0scc个数). #include<cstdio> #include<iostream> #inc

[IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. ---------------------------------------------------------------------------------- #include<cstdio> #include<iostream> #include<algorithm> #includ

POJ - 1236 Network of Schools(强连通分量)

题目大意:有N个点,接着给出N个点所能连接的点. 问题1:如果要将一个信息传递给这N个点,至少需要传递给多少个点,然后让这些点进行传播,使N个点都得到信息 问题2:需要添加多少条边才能使这N个点能两两连通 解题思路:求出所有的强连通分量,接着缩点,再以桥为路径,建图 找出这张图中入度为0的,因为只有入度为0的才需要进行通知,其他的点可以通过其他边进行传达 需要添加多少个点,观察这张图,求出每个点的出度和入度,取max(入度为零的点的数量,出度为0的点数量) 注意当强连通分量只有1个的时候 #in

POJ1236 Network of Schools (强连通分量,注意边界)

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 lis

POJ 1236 Network of Schools(强连通分量)

题目地址:POJ 1236 这个题的大意是求最少往多少点发送消息可以使任意一个点都能收到消息和最少增加多少条边可以使图为连通图.对于第一个问题,可以求入度为0的强连通块的块数,因为只有入度为0的强连通块是无法从外界接受信息的,而只要有一个入度的话,那整个连通块就都可以接收到信息.第二个问题则是求入度为0的强连通块与出度为0的强连通块的个数的最大值. 代码如下: #include <iostream> #include <cstdio> #include <string>

POJ 1236 Network Of Schools (强连通分量模板题)

代码: #include<iostream> #include<cstdio> #include<cmath> #include<map> #include<queue> #include<vector> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rev(i,a,b)

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

Network of Schools Time Limit: 1000MS Memory Limit: 10000K 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 softwa

POJ1236Network of Schools(强连通分量 + 缩点)

题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件.   也就是: ?        给定一个有向图,求:   1) 至少要选几个顶

UVALIVE 4287 Proving Equivalences (强连通分量+缩点)

题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的入度和出度至少为1.那么我们求出入度为零的节点数量和出度为零的节点数量.答案取最大值,由于在一个DAG中加入这么多边一定能够使这个图强连通.注意当这个图本身强连通时要特判一下,答案为零. #include<cstdio> #include<cstring> #include<cm