[POJ 1236][IOI 1996]Network of Schools

來源:互聯網
上載者:User

標籤:define   sts   ret   scribe   style   field   develop   net   第一個   

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

Source

field=source&key=IOI+1996">IOI 1996


題目大意:給定一個有向圖,求至少要有多少個點, 才幹從這些點出發到達全部點;至少要加入多少條邊,才幹從隨意一點出發到達全部點

首先要推出一個定理:在DAG中,對於全部入度不為0的點,一定有入度為0的點可達(由於從入度為0的點倒著走,一定能走到入度不為0的點)

於是此題可用tarjan縮點,求有多少個入度為0的點,這就是第一個問題的答案。

第二個問題的答案為入度為0的點和出度為0的點的最小值。證明比較難。略。

對於這道題,由於僅僅要求入度和出度為0的點,故僅僅需在tarjan過程中記錄每一個點歸屬哪個強連通分量。然後統計輸出就可以

#include <iostream>#include <stdio.h>#include <string.h>#define MAXE 500#define MAXV 3000using namespace std;int N;struct edge{    int u,v,next;}edges[MAXV];int head[MAXE],nCount=0;int dfn[MAXE],low[MAXE],index=0;int belong[MAXE],tot=0; //belong[i]=i點所屬的強連通分量,tot=強連通分量總數bool inStack[MAXE];int stack[MAXE*4],top=0;bool map[MAXE][MAXE];int inDegree[MAXE],outDegree[MAXE],inZero=0,outZero=0; //入度。出度int max(int a,int b){    if(a>b) return a;    return b;}int min(int a,int b){    if(a<b) return a;    return b;}void AddEdge(int U,int V){    edges[++nCount].u=U;    edges[nCount].v=V;    edges[nCount].next=head[U];    head[U]=nCount;}void tarjan(int u){    dfn[u]=low[u]=++index;    stack[++top]=u; //該點入棧    inStack[u]=true;    for(int p=head[u];p!=-1;p=edges[p].next)    {        int v=edges[p].v;        if(!dfn[v])        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(inStack[v])        {            low[u]=min(low[u],dfn[v]);        }    }    int v;    if(dfn[u]==low[u])    {        tot++;        do        {            v=stack[top--];            belong[v]=tot;            inStack[v]=false;        }        while(u!=v);    }}int main(){    int to;    cin>>N;    memset(head,-1,sizeof(head));    for(int i=1;i<=N;i++)    {        while(1)        {            cin>>to;            if(to==0) break;            AddEdge(i,to);            map[i][to]=true;        }    }    for(int i=1;i<=N;i++)        if(!dfn[i]) tarjan(i);    for(int i=1;i<=N;i++)        for(int j=1;j<=N;j++)        {            if(map[i][j]&&belong[i]!=belong[j])            {                inDegree[belong[j]]++;                outDegree[belong[i]]++;            }        }    for(int i=1;i<=tot;i++)    {        if(!inDegree[i]) inZero++;        if(!outDegree[i]) outZero++;    }    if(tot==1) cout<<1<<endl<<0<<endl;    else cout<<inZero<<endl<<max(inZero,outZero)<<endl;    return 0;}



[POJ 1236][IOI 1996]Network of Schools

相關文章

聯繫我們

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