一 原题
IOI '96 Day 1 Problem 3
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.
PROGRAM NAME: schlnet
INPUT FORMAT
The first line of the input file 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.SAMPLE INPUT (file schlnet.in)
5 2 4 3 0 4 5 0 0 0 1 0
OUTPUT FORMAT
Your program should write two lines to the output file. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
SAMPLE OUTPUT (file schlnet.out)
1 2
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: schlnet LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4208 KB] Test 2: TEST OK [0.000 secs, 4208 KB] Test 3: TEST OK [0.000 secs, 4208 KB] Test 4: TEST OK [0.000 secs, 4208 KB] Test 5: TEST OK [0.000 secs, 4208 KB] Test 6: TEST OK [0.000 secs, 4208 KB] Test 7: TEST OK [0.000 secs, 4208 KB] Test 8: TEST OK [0.000 secs, 4208 KB] Test 9: TEST OK [0.000 secs, 4208 KB] Test 10: TEST OK [0.000 secs, 4208 KB] Test 11: TEST OK [0.000 secs, 4208 KB] All tests OK.
Your program ('schlnet') produced all correct answers! This is your submission #3 for this problem. Congratulations!
/*
ID:maxkibb3
LANG:C++
PROB:schlnet
*/
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXV = 105;
int N;
bool Map[MAXV][MAXV];
vector< int > E[MAXV];
/*** Global Variables for Tarjan ***/
int Cnt;
int Index[MAXV];
int Lowlink[MAXV];
bool Instack[MAXV];
stack< int > S;
vector< vector<int> > Sc;
bool Edges[MAXV][MAXV];
void init() {
int tmp;
scanf("%d", &N);
for(int i = 1; i <= N; i++)
while(true) {
scanf("%d", &tmp);
if(tmp == 0) break;
E[i].push_back(tmp);
Map[i][tmp] = true;
}
}
void tarjan(int _idx) {
if(Index[_idx] != -1) return;
Index[_idx] = Lowlink[_idx] = Cnt++;
S.push(_idx);
Instack[_idx] = true;
for(int i = 0; i < E[_idx].size(); i++) {
int next = E[_idx][i];
if(Index[next] == -1) {
tarjan(next);
if(Lowlink[next] < Lowlink[_idx])
Lowlink[_idx] = Lowlink[next];
}
else if(Instack[next] &&
Lowlink[_idx] > Index[next]) {
Lowlink[_idx] = Index[next];
}
}
if(Index[_idx] == Lowlink[_idx]) {
vector<int> new_sc;
int top_ele;
do {
top_ele = S.top();
new_sc.push_back(top_ele);
Instack[top_ele] = false;
S.pop();
}
while(top_ele != _idx);
Sc.push_back(new_sc);
}
}
void build_graph() {
int ssize = Sc.size();
for(int i = 0; i < ssize; i++)
for(int j = i + 1; j < ssize; j++)
for(int k = 0; k < Sc[i].size(); k++)
for(int l = 0; l < Sc[j].size(); l++) {
if(Map[Sc[i][k]][Sc[j][l]])
Edges[i][j] = true;
if(Map[Sc[j][l]][Sc[i][k]])
Edges[j][i] = true;
}
}
void count() {
if(Sc.size() == 1) {
printf("1\n0\n");
return;
}
int in_ele, out_ele;
int in_zero = 0, out_zero = 0;
for(int i = 0; i < Sc.size(); i++) {
in_ele = out_ele = 0;
for(int j = 0; j < Sc.size(); j++) {
if(Edges[i][j])
out_ele++;
if(Edges[j][i])
in_ele++;
}
if(in_ele == 0) in_zero++;
if(out_ele == 0) out_zero++;
}
printf("%d\n%d\n", in_zero, max(in_zero, out_zero));
}
void solve() {
for(int i = 1; i <= N; i++)
Index[i] = -1;
for(int i = 1; i <= N; i++)
tarjan(i);
build_graph();
count();
}
int main() {
freopen("schlnet.in", "r", stdin);
freopen("schlnet.out", "w", stdout);
init();
solve();
return 0;
}