POJ 1236--Network of Schools【scc縮點構圖 && 求scc入度為0的個數 && 求最少加幾條邊使圖變成強聯通】

來源:互聯網
上載者:User

標籤:

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13325   Accepted: 5328

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

以下解析轉自斌神的部落格

強連通分量縮點求入度為0的個數和出度為0的分量個數
題目大意:N(2<N<100)各學校之間有單向的網路,每個學校得到一套軟體後,可以通過單向網路向周邊的學校傳輸,問題1:初始至少需要向多少個學校發放軟體,使得網路內所有的學校最終都能得到軟體。2,至少需要添加幾條傳輸線路(邊),使任意向一個學校發放軟體後,經過若干次傳送,網路內所有的學校最終都能得到軟體。

也就是:
— 給定一個有向圖,求:

1) 至少要選幾個頂點,才能做到從這些頂點出發,可以到達全部頂點

2) 至少要加多少條邊,才能使得從任何一個頂點出發,都能到達全部頂點

解題思路:
— 1. 求出所有強連通分量


— 2. 每個強連通分量縮成一點,則形成一個有向非循環圖DAG。


— 3. DAG上面有多少個入度為0的頂點,問題1的答案就是多少


在DAG上要加幾條邊,才能使得DAG變成強連通的,問題2的答案就是多少


加邊的方法:
要為每個入度為0的點添加入邊,為每個出度為0的點添加出邊
假定有 n 個入度為0的點,m個出度為0的點,如何加邊?
把所有入度為0的點編號 0,1,2,3,4 ....N -1
每次為一個編號為i的入度0點可達的出度0點,添加一條出邊,連到編號為(i+1)%N 的那個出度0點,
這需要加n條邊
若 m <= n,則
加了這n條邊後,已經沒有入度0點,則問題解決,一共加了n條邊
若 m > n,則還有m-n個入度0點,則從這些點以外任取一點,和這些點都連上邊,即可,這還需加m-n條邊。
所以,max(m,n)就是第二個問題的解


此外:當只有一個強連通分支的時候,就是縮點後只有一個點,雖然入度出度為0的都有一個,但是實際上不需要增加清單的項了,所以答案是1,0;

#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#define maxn 100 + 100#define maxm 110 * 100using namespace std;int n, m;struct node{    int u, v, next;};node edge[maxm];int head[maxn], cnt;int low[maxn], dfn[maxn];int dfs_clock;int Stack[maxn], top;bool Instack[maxn];int Belong[maxn];int scc_clock;int in[maxn], out[maxn];int num[maxn];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void addedge(int u, int v){    edge[cnt] = {u, v, head[u]};    head[u] = cnt++;}void getmap(){    for(int i = 1; i <= n; ++i){        int v;        while(scanf("%d", &v),v){            addedge(i, v);        }    }}void Tarjan(int u, int per){    int v;    low[u] = dfn[u] = ++dfs_clock;    Stack[top++] = u;    Instack[u] = true;    for(int i = head[u]; i != -1; i = edge[i].next){        int v = edge[i].v;        if(!dfn[v]){            Tarjan(v, u);            low[u] = min(low[u], low[v]);        }        else if(Instack[v])            low[u] = min(low[u], dfn[v]);    }    if(dfn[u] == low[u]){        scc_clock++;        do{            v = Stack[--top];            Instack[v] = false;            Belong[v] = scc_clock;        }        while( v != u);    }}void suodian(){    for(int i = 1; i <= scc_clock; ++i){        out[i] = 0;        in[i] = 0;    }    for(int i = 0; i < cnt; ++i){        int u = Belong[edge[i].u];        int v = Belong[edge[i].v];        if(u != v){            out[u]++;            in[v]++;        }    }}void find(){    memset(low, 0, sizeof(low));    memset(dfn, 0, sizeof(dfn));    memset(Belong, 0, sizeof(Belong));    memset(Stack, 0, sizeof(Stack));    memset(Instack, false, sizeof(false));    dfs_clock = scc_clock = top = 0;    for(int i = 1; i <= n ; ++i){        if(!dfn[i])            Tarjan(i, i);    }}void solve(){    if(scc_clock == 1){        printf("1\n0\n");        return ;    }    int numin, numout;    numin = numout = 0;    for(int i = 1; i <= scc_clock; ++i){        if(in[i] == 0) numin++;        if(out[i] == 0) numout++;    }    printf("%d\n%d\n", numin, max(numout, numin));}int main (){    while(scanf("%d", &n) != EOF){        init();        getmap();        find();        suodian();        solve();    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

POJ 1236--Network of Schools【scc縮點構圖 && 求scc入度為0的個數 && 求最少加幾條邊使圖變成強聯通】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.