poj 1236 Network of Schools(又是强连通分量+缩点)

时间:2022-06-20 23:51:39

http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9481   Accepted: 3767

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

Source

 
【题解】:
  这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。可以先用kosaraju将强连通分支缩点,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息,所以第一问所求即为缩点后的图中入度为0的个数,至于第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。
 
【code】:
 
 /**
Judge Status:Accepted Memory:772K
Time:0MS Language:G++
Code Length:2155B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
#include<algorithm>
#include<vector> #define N 110
using namespace std; vector<int> G[N];
int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt;
stack<int> stk; int visit[N][N],in[N],out[N];
void Tarjan(int u)
{
pre[u] = lowlink[u] = ++dfs_cnt;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
Tarjan(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(pre[u]==lowlink[u])
{
int x;
scc_cnt++;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
}while(x!=u);
}
} void findncc(int n)
{
dfs_cnt = scc_cnt = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
int i;
for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
} void getNewMap(int n)
{
int i,j;
memset(visit,,sizeof(visit));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(i=;i<=n;i++)
{
for(j=;j<G[i].size();j++)
{
int v = sccno[G[i][j]];
int u = sccno[i]; //注意是对sccno[i]数组里的强联通分量进行操作,也就是缩点的过程
if(u!=v)
{
if(!visit[u][v])
{
visit[u][v] = ;
in[v]++; //出度入度统计
out[u]++;
}
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int i;
for(i=;i<=n;i++)
{
int a;
G[i].clear();
while(~scanf("%d",&a)&&a)
{
G[i].push_back(a);
}
}
findncc(n);
getNewMap(n);
int cnt_in = ,cnt_out = ;
for(i=;i<=scc_cnt;i++)
{
if(!in[i]) cnt_in++;
if(!out[i]) cnt_out++;
}
printf("%d\n",cnt_in);
if(scc_cnt!=) printf("%d\n",max(cnt_in,cnt_out)); //联通分量只有一个输出0
else printf("0\n");
return ;
}