Network of Schools

来源:互联网 发布:陶瓷泥 知乎 编辑:程序博客网 时间:2024/05/23 20:44

链接:

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


题目:

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(2< N< 100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,
  1. 初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
  2. 至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。


思路:

  找强连通分量,缩点。记in_deg[i]为缩完点后的新图中各点入度,out_deg[i]为出度,ain_deg[i]==0的点的数目,bout_edg[i]==0的点的数目则第一问为a,第二问则为max{a,b}

  第二问,对于得到的DAG图,考虑其中的出度为0的点和入度为0的点组成的点集V,将这些点相连,最多这需要max{a,b}条边,就能使整个图成为强连通分量。


实现:

#include <iostream>#include <algorithm>#include <set>#include <string>#include <vector>#include <queue>#include <map>#include <stack>#include <functional>#include <iomanip>#include <cstdio>#include <cstring>#include <cmath>#include <climits>#include <cctype>using namespace std;#define maxn 107#define memset(a,b) memset(a,b,sizeof a)struct {    int next,to;}edge[maxn*maxn];int tot, ord, sccnum, top, head[maxn], low[maxn], DFN[maxn], in_deg[maxn], out_deg[maxn], block[maxn], st[maxn];bool instack[maxn];void addedge(int from, int to) {    edge[tot].to = to;    edge[tot].next = head[from];    head[from] = tot++;}void tarjan(int u) {    DFN[u] = low[u] = ++ord;    instack[u] = true;    st[top++] = u;    for(int i = head[u] ; i != -1 ; i = edge[i].next) {        int v = edge[i].to;        if(DFN[v] == -1) {            tarjan(v);            low[u] = min(low[u],low[v]);        } else if(instack[v] && DFN[v] < low[u]) low[u] = DFN[v];    }    int v;    if(DFN[u] == low[u]) {        sccnum++;        do {            v = st[--top];            block[v] = sccnum;            instack[v] = false;        } while(u!=v);    }}void init() {    memset(DFN,-1);    memset(low,-1);    memset(head,-1);    memset(in_deg,0);    memset(out_deg,0);    memset(instack,false);    top = tot = sccnum = ord = 0;}void solve(int n) {    for(int i = 1 ; i <= n ; i++) if(DFN[i] == -1) tarjan(i);    if(sccnum == 1) {        cout << 1 << '\n' << 0 << '\n';        return ;    }    for(int u = 1 ; u <= n ; u++) {        for(int i = head[u] ; i != -1 ; i = edge[i].next) {            int v = edge[i].to;            if(block[u] == block[v]) continue;            out_deg[block[u]]++, in_deg[block[v]]++;        }    }    int a=0,b=0;    for(int i = 1 ; i <= sccnum ; i++) {        if(in_deg[i] == 0) a++;        if(out_deg[i] == 0) b++;    }    cout << a << '\n' << max(a,b) << '\n';}int main() {#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);#endif    ios_base::sync_with_stdio(false);cin.tie(NULL);    int n,u,v;    while(cin >> n) {        init();        for(u=1 ; u<=n ; u++) while(cin >> v, v!=0) addedge(u,v);        solve(n);    }    return 0;}
原创粉丝点击