1. 程式人生 > >POJ 1236 Network of Schools targan+縮點

POJ 1236 Network of Schools targan+縮點

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

題意:
給出一個圖:

1.求至少需要幾個點才能將資訊傳播至各個城市

2.求需要加幾條邊構成強連通

targan+縮點。。 。 縮點後,求入度為0和出度為0的個數。

1問的答案就是入度為0的個數。

2問的答案就是入度為0的個數與出度為0的個數的最大值。 。。

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int maxn=105;
int n;
int low[maxn],dfn[maxn],ccnc[maxn];
int in[maxn],on[maxn];
int tot,ans1,ans2,ccnum,innum,onnum;
vector<int>ve[maxn];
stack<int>s;
void init()
{
    onnum=innum=tot=ans1=ans2=ccnum=0;
    memset (low,0,sizeof(low));
    memset (dfn,0,sizeof(dfn));
    memset (ccnc,0,sizeof(ccnc));
    memset (in,0,sizeof(in));
    memset (on,0,sizeof(on));
    for (int i=1;i<=n;i++)
        ve[i].clear();
    while (!s.empty())
        s.pop();
}
void targan (int x)
{
    dfn[x]=low[x]=++tot;
    s.push(x);
    for (int i=0;i<ve[x].size();i++)
    {
        int v=ve[x][i];
        if(!dfn[v])
        {
            targan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!ccnc[v])
            low[x]=min(low[x],dfn[v]);
    }
    if(low[x]==dfn[x])
    {
        ccnum++;
        while (1)
        {
            int now=s.top();
            s.pop();
            ccnc[now]=ccnum;
            if(now==x)
                break;
        }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for (int i=1;i<=n;i++)
        {

           int x;
           while (scanf("%d",&x)&&x)
              ve[i].push_back(x);
        }
       for (int i=1;i<=n;i++)
         if(!dfn[i])
            targan(i);
       for (int i=1;i<=n;i++)
      {
           for (int j=0;j<ve[i].size();j++)
          {
            int v=ve[i][j];
            if(ccnc[i]!=ccnc[v])
            {
                on[ccnc[i]]++;
                in[ccnc[v]]++;
            }
          }
       }
        for (int i=1;i<=ccnum;i++)
        {
         if(!in[i])
            innum++;
         if(!on[i])
            onnum++;
        }
        if(ccnum==1)
           printf("1\n0\n");
        else
           printf("%d\n%d\n",innum,max(innum,onnum));
    }
    return 0;
}