poj1236 Network of Schools

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

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Source

 
题意:给出学校间的网络,网络是单向的。某个学校得到消息之后,他指向的学校也能得到消息。问至少要发放多少消息,才能让所有学校都得到消息。二问,要求最少加几条边,才能使在任意点发放一个消息,所有学校都能拿到。
思路: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 }

 

posted @ 2014-10-01 17:26  一个大叔  阅读(430)  评论(0编辑  收藏  举报