您的位置:首页 > 其它

POJ 1236 Network of Schools【强联通】

2018-02-22 15:43 531 查看
Network of Schools

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20668 Accepted: 8155
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

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
#define MAX 105
#define mms(x) memset(x, 0, sizeof x)
vector<int > v[MAX];
int mp[MAX][MAX], dfn[MAX], low[MAX], Stack[MAX], col[MAX], vis[MAX], in[MAX], out[MAX];
int n, sig, pos, num;
void init()
{
mms(in);
mms(out);
mms(dfn);
mms(low);
mms(vis);
mms(Stack);
mms(col);
mms(mp);
for(int i = 1; i <= n; i++)
v[i].clear();
}
void Tarjan(int u)
{
vis[u] = 1;
low[u] = dfn[u] = num++;
Stack[++pos] = u;
for(int i = 0; i < v[u].size(); i++)
{
int uv = v[u][i];
if(vis[uv] == 0)
Tarjan(uv);
if(vis[uv] == 1)
low[u] = min(low[u], low[uv]);
}
if(dfn[u] == low[u])
{
sig++;
do
{
col[Stack[pos]] = sig;
vis[Stack[pos]] = -1;
}
while(Stack[pos--] != u);
}
}
void Slove()
{
num = 1, pos = -1, sig = 0;
for(int i = 1; i <= n; i++)
if(vis[i] == 0)
Tarjan(i);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(mp[i][j] && col[i] != col[j])
{
in[col[j]]++;
out[col[i]]++;
}
int t1 = 0, t2 = 0;
for(int i = 1; i <= sig; i++)
{
if(!in[i])
t1++;
if(!out[i])
t2++;
}
if(sig == 1)
cout << "1" << endl << "0" << endl;
else
cout << t1 << endl << max(t1, t2) << endl;
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
init();
for(int i = 1, a; i <= n; i++)
while(cin >> a && a)
{
v[i].push_back(a);
mp[i][a] = 1;
}
Slove();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: