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。
代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
vector<int>vec[MAXN];
stack<int>S;
int dfn[MAXN],low[MAXN],Instack[MAXN],in[MAXN],out[MAXN];
int dfs_blocks,scc,sccon[MAXN];
void targin(int u,int fa){
S.push(u);
Instack[u]=;
dfn[u]=low[u]=++dfs_blocks;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!dfn[v]){
targin(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc++;
while(){
int v=S.top();
S.pop();
Instack[v]=;
sccon[v]=scc;
if(u==v)break;
}
}
}
int main(){
int N;
while(~scanf("%d",&N)){
scc=;
for(int i=;i<MAXN;i++)vec[i].clear();
while(!S.empty())S.pop();
mem(in,);mem(out,);mem(Instack,);
mem(dfn,);mem(low,);mem(sccon,);
for(int i=;i<=N;i++){
int x;
while(scanf("%d",&x),x!=){
vec[i].push_back(x);
}
}
for(int i=;i<=N;i++){
if(!dfn[i]){
targin(i,-);
}
}
for(int i=;i<=N;i++){
for(int j=;j<vec[i].size();j++){
int v=vec[i][j];
if(sccon[i]!=sccon[v])in[sccon[v]]++,out[sccon[i]]++;
}
}
// printf("%d\n",scc);
int numin=,numout=;
if(scc==){
puts("1\n0");continue;
}
for(int i=;i<=scc;i++){
if(in[i]==)numin++;
if(out[i]==)numout++;
}
printf("%d\n%d\n",numin,max(numin,numout));
}
return ;
}
上一篇:js之parentElement属性


下一篇:LeetCode: Best Time to Buy and Sell Stock II [122]