POJ1236 Network of Schools 【強連通分量Garbow】

來源:互聯網
上載者:User

標籤:poj1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11180   Accepted: 4448

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

52 4 3 04 5 0001 0

Sample Output

12
題意:給定各台電腦的串連情況,求兩個問題:1、最少向網路中的幾台電腦投放檔案,則整個網路中的所有電腦能立刻獲得該檔案;2、最少要向網路中添加幾條單向串連可以使得這個網路中只要投放一份檔案到任意一台電腦,則所有電腦都能獲得該檔案。題解:第一道Garbow題,需要注意的地方是當給定圖是連通圖時不需要添加任何邊。改進後的一個版本:0ms
#include <stdio.h>#include <string.h>#define maxn 102#define maxm maxn * maxnint head[maxn], sta1[maxn], sta2[maxn], scc[maxn];int low[maxn], id, id1, id2, sec, sccNum;bool in[maxn], out[maxn];struct Node{    int to, next;} E[maxm];void addEdge(int u, int v){    E[id].to = v;    E[id].next = head[u];    head[u] = id++;}void getMap(int n){    memset(head, -1, sizeof(int) * (n + 1));    int i, v; id = 0;    for(i = 1; i <= n; ++i)        while(scanf("%d", &v), v)            addEdge(i, v);    }void Garbow(int pos){    sta1[id1++] = sta2[id2++] = pos;    low[pos] = ++sec;    for(int i = head[pos]; i != -1; i = E[i].next){        if(!low[E[i].to]) Garbow(E[i].to);        else if(!scc[E[i].to])            while(low[E[i].to] < low[sta2[id2-1]]) --id2;        if(scc[E[i].to]) in[scc[E[i].to]] = 1;    }    if(sta2[id2-1] == pos){        ++sccNum; --id2; int v;        do{            v = sta1[--id1];            scc[v] = sccNum;        } while(sta1[id1] != pos);    }}void solve(int n){    memset(low, 0, sizeof(int) * (n + 1));    memset(scc, 0, sizeof(int) * (n + 1));    memset(in, 0, sizeof(bool) * (n + 1));    memset(out, 0, sizeof(bool) * (n + 1));    int i, j, ans1 = 0, ans2 = 0; id1 = id2 = sec = sccNum = 0;    for(i = 1; i <= n; ++i)        if(!low[i]) Garbow(i);    for(i = 1; i <= n; ++i)        for(j = head[i]; j != -1; j = E[j].next)            if(scc[i] != scc[E[j].to]){                out[scc[i]] = 1; break;            }    if(sccNum != 1){        for(i = 1; i <= sccNum; ++i){            if(!out[i]) ++ans2;            if(!in[i]) ++ans1;        }        if(ans1 > ans2) ans2 = ans1;    }else ans1 = 1;    printf("%d\n%d\n", ans1, ans2);}int main(){    int n;    while(scanf("%d", &n) == 1){        getMap(n);        solve(n);    }    return 0;}

原32ms:

#include <stdio.h>#include <string.h>#define maxn 102#define maxm maxn * maxnint head[maxn], sta1[maxn], sta2[maxn], scc[maxn];int low[maxn], id, id1, id2, sec, sccNum;bool in[maxn], out[maxn];struct Node{    int to, next;} E[maxm];void addEdge(int u, int v){    E[id].to = v;    E[id].next = head[u];    head[u] = id++;}void getMap(int n){    memset(head, -1, sizeof(int) * (n + 1));    int i, v; id = 0;    for(i = 1; i <= n; ++i)        while(scanf("%d", &v), v)            addEdge(i, v);    }void Garbow(int pos){    sta1[id1++] = sta2[id2++] = pos;    low[pos] = ++sec;    for(int i = head[pos]; i != -1; i = E[i].next)        if(!low[E[i].to]) Garbow(E[i].to);        else if(!scc[E[i].to])            while(low[E[i].to] < low[sta2[id2-1]]) --id2;    if(sta2[id2-1] == pos){        ++sccNum; --id2; int v;        do{            v = sta1[--id1];            scc[v] = sccNum;        } while(sta1[id1] != pos);    }}void solve(int n){    memset(low, 0, sizeof(int) * (n + 1));    memset(scc, 0, sizeof(int) * (n + 1));    memset(in, 0, sizeof(bool) * (n + 1));    memset(out, 0, sizeof(bool) * (n + 1));    int i, j, ans1 = 0, ans2 = 0; id1 = id2 = sec = sccNum = 0;    for(i = 1; i <= n; ++i)        if(!low[i]) Garbow(i);    for(i = 1; i <= n; ++i)        for(j = head[i]; j != -1; j = E[j].next)            if(scc[i] != scc[E[j].to]){                out[scc[i]] = 1;                 in[scc[E[j].to]] = 1;            }    if(sccNum != 1){        for(i = 1; i <= sccNum; ++i){            if(!out[i]) ++ans2;            if(!in[i]) ++ans1;        }        if(ans1 > ans2) ans2 = ans1;    }else ans1 = 1;    printf("%d\n%d\n", ans1, ans2);}int main(){    int n;    while(scanf("%d", &n) == 1){        getMap(n);        solve(n);    }    return 0;}


POJ1236 Network of Schools 【強連通分量Garbow】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.