题目描述:
Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17310 | Accepted: 6874 |
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.
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
题目大意:给定一张有向图,求:A、至少从多少个点出发可以遍历全图;B、至少要再连接几条有向边才能从任意一个点出发都可以遍历全图。
思路:
先用强联通分量的tarjan算法预处理缩点,得到一张有向无环图。
A问题就是求新图中入度为0的点的个数。
B问题就是图中入度为0的点的个数和图中出度为0的点的个数的最大值。
注意:当缩完点后的图中只有一个点时应特殊考虑。
代码:
#include<cstdio>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int n;
vector<int> a[110];
int color[110]= {0},num=0;
stack<int> s;
int dfn[110]= {0},low[110]= {0},clock=0;
int tarjan(int x) {
s.push(x);
dfn[x]=low[x]=++clock;
for(int i=0; i<a[x].size(); i++) {
int y=a[x][i];
if(dfn[y]==0) {
low[x]=min(low[x],tarjan(y));
} else if(color[y]==0) {
low[x]=min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]) {
num++;
while(s.top()!=x) {
color[s.top()]=num;
s.pop();
}
color[x]=num;
s.pop();
}
return low[x];
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
int x;
while(scanf("%d",&x)&&x!=0) {
a[i].push_back(x);
}
}
for(int i=1; i<=n; i++) {
if(!color[i]) {
tarjan(i);
}
}
if(num==1){ //特殊情况
printf("1\n0");
return 0;
}
bool g[110][110]={0};
for(int i=1;i<=n;i++){
for(int j=0;j<a[i].size();j++){
if(color[i]!=color[a[i][j]]){
g[color[i]][color[a[i][j]]]=true;
}
}
}
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
if(g[i][j]){
g[i][0]++;
g[0][j]++;
}
}
}
int s1=0,s2=0;
for(int i=1;i<=num;i++){
if(g[i][0]==0) s1++;
if(g[0][i]==0) s2++;
}
printf("%d\n%d",s2,max(s1,s2));
return 0;
}