poj1236 Network of Schools

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11433   Accepted: 4551

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

IOI 1996 題意:給出學校間的網路,網路是單向的。某個學校得到訊息之後,他指向的學校也能得到訊息。問至少要發放多少訊息,才能讓所有學校都得到訊息。二問,要求最少加幾條邊,才能使在任意點發放一個訊息,所有學校都能拿到。思路:tarjan練手題,第一問,求完連通塊之後縮點,然後入度為0的點的個數。第二位,max(入度為0的數,出度為0的數);note:如果只有一個連通塊的話,第二問要輸出0
  1 /*  2  * Author:  Joshua  3  * Created Time:  2014年09月29日 星期一 10時32分59秒  4  * File Name: poj1236.cpp  5  */  6 #include<cstdio>  7 #define maxn 101  8 #define maxm 10001  9 int n,top,tot,c,num; 10 int head[maxn],color[maxn],rd[maxn],cd[maxn],s[maxn],dfn[maxn],low[maxn]; 11 bool mark[maxn]; 12 struct node 13 { 14     int d,next; 15 } e[maxm]; 16  17 void init() 18 { 19     int x,y; 20     num=c=tot=top=0; 21     for (int i=1;i<=n;++i) 22     { 23         scanf("%d",&y); 24         while (y) 25         { 26             e[++tot].d=y; 27             e[tot].next=head[i]; 28             head[i]=tot; 29             scanf("%d",&y); 30         } 31     }     32 } 33  34 void tarjan(int x) 35 { 36     int y; 37     dfn[x]=low[x]=++num; 38     s[++top]=x; 39     mark[x]=true; 40     for (int i=head[x];i;i=e[i].next) 41     { 42         y=e[i].d; 43         if (!dfn[y])  44         { 45             tarjan(y); 46             if (low[y]<low[x]) 47                 low[x]=low[y]; 48         } 49         else if (mark[y] && low[x]>low[y]) 50             low[x]=low[y]; 51     } 52     if (low[x]==dfn[x]) 53     { 54         ++c; 55         do  56         { 57             y=s[top--]; 58             color[y]=c; 59             mark[y]=false; 60         } 61         while (y!=x); 62     } 63 } 64              65 void solve() 66 { 67     int x,y,csum,rsum,ans; 68     for (int i=1;i<=n;++i) 69         if (!dfn[i])  70             tarjan(i); 71     if (c==1)  72     { 73         printf("1\n0\n"); 74         return; 75     } 76     for (int i=1;i<=n;++i) 77         for (int j=head[i];j;j=e[j].next) 78         { 79             y=e[j].d; 80             if (color[y]!=color[i]) 81             { 82                 rd[color[y]]++; 83                 cd[color[i]]++; 84             } 85         } 86     csum=rsum=0; 87     for (int i=1;i<=c;++i) 88     { 89         if (!rd[i]) ++rsum; 90         if (!cd[i]) ++csum; 91     } 92     if (rsum>csum) ans=rsum; 93     else ans=csum; 94     printf("%d\n",rsum); 95     printf("%d\n",ans); 96 } 97  98 int main() 99 {100     while (scanf("%d",&n)>0)101     {102         init();103         solve();104     }105     return 0;106 }

 

poj1236 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.