测试题目:
http://poj.org/problem?id=1236
Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26580 Accepted: 10487
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
题目大意:
有n个点的图,然后连着一些边,问最少需要从几个点开始走可以走到整个图,最少加几条边可以是任意的点走遍整张图。
解题思路:
首先由Tanjar算法进行缩点,然后再将缩点后的点进行连边;这是会得到入点为0的点,设其个数为ans1, 出点为0的点,设其个数为ans2;
那么,我们最少需要ans1就是第一个问题的答案。max(ans1, ans2)就是第二个问题的答案。
特殊情况,若是cnt == 1;(cnt 为最后强连通分量的个数),这时候第一个问题的答案为1,第二个问题的答案为0.
注意点:
1.遍历边的时候,从h[i] 开始,字母别写错了,这种错误不容易排除。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int N = 110, M = 300000;
int n, idx1, idx, top, num, cnt;
int h1[N], e1[M], ne1[M];
int dfn[N], low[N];
int stack[N], ins[N], c[N];
int h[N], e[M], ne[M];
int in[N], out[N];
vector<int> scc[N];
inline void add1(int a, int b) {
e1[idx1] = b, ne1[idx1] = h1[a], h1[a] = idx1 ++;
}
inline void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
inline void tarjan(int x) {
dfn[x] = low[x] = ++num;
stack[++top] = x, ins[x] = 1;
for(int i = h1[x]; i + 1; i = ne1[i]) {
int v = e1[i];
if(!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if(ins[v]) {
low[x] = min(low[x], dfn[v]);
}
}
if(dfn[x] == low[x]) {
cnt++; int y;
do {
y = stack[top --], ins[y] = 0;
c[y] = cnt, scc[cnt].push_back(y);
} while(x != y);
}
}
int main(void) {
// freopen("in.txt", "r", stdin);
scanf("%d", &n);
memset(h1, -1, sizeof h1);
memset(h, -1, sizeof h);
for(int i = 1; i <= n; i ++) {
int v;
while(scanf("%d", &v)!= EOF && v) {
add1(i, v);
}
}
for(int i = 1; i <= n; i ++)
if(!dfn[i]) tarjan(i);
for(int i = 1; i <= n; i ++) {
for(int j = h1[i]; j + 1; j = ne1[j]) {
int v = e1[j];
if(c[i] == c[v]) continue;
out[c[i]] ++;
in[c[v]] ++;
}
}
int ans1 = 0, ans2 = 0;
for(int i = 1; i <= cnt; i ++) {
if(in[i] == 0) ans1 ++;
if(out[i] == 0) ans2 ++;
}
if(cnt == 1) printf("1\n0");
else printf("%d\n%d", ans1, max(ans1, ans2));
// fclose(stdin);
return 0;
}