poj1236——Network of Schools(加最少边组成强连通分量)

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

题目有两个要求。第一个是求最少可以给软件的学校,明显只要求入度为0的强连通分量就行,设答案为ans1
第二个是求增加最少的边,使得任意给两个学校软件都行,即所有学校要组成一个强连通分量。思路是算出出度为0的点ans2,在加上上一问算出的ans1,答案就是max(ans1,ans2)。假设出度为0的点比较多,把这些点与入度为0的点连接就能组成强连通分量

#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=105;
const int maxe=105*105;
struct Node
{
    int x,y,next;
} edge[maxe];
int dfn[MAXN],low[MAXN],vis[MAXN],s[MAXN],belong[MAXN],head[MAXN];
int in[MAXN],out[MAXN]; //入度,出度
int n,tot=0,ssum=0,ans=0,times=0,t=0,ans2;
void add(int x,int y)
{
    edge[++tot].x=x;
    edge[tot].y=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void tarjan(int x)
{
    int y,i;
    times++;
    t++;
    dfn[x]=low[x]=times;
    vis[x]=1;
    s[t]=x;
    for (i=head[x]; i; i=edge[i].next)
    {
        y=edge[i].y;
        if (vis[y]==0)
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        if (vis[y]==1)
            low[x]=min(low[x],dfn[y]);
    }
    if (dfn[x]==low[x])
    {
        ssum++;
        do
        {
            y=s[t--];
            belong[y]=ssum;
            vis[y]=2;
        }
        while (y!=x);
    }
}
int main()
{
    int i,j;
    while(cin >> n)
    {
        for (i=1; i<=n; i++)
        {
            cin >> j;
            while (j)
            {
                add(i,j);
                cin >> j;
            }
        }
        for (i=1; i<=n; i++)
            if (vis[i]==0)
                tarjan(i);
        if (ssum==1)  //只有一个连通分量时要特判
        {
            cout << 1 << endl << 0 << endl;
            return 0;
        }
        for (i=1; i<=tot; i++)
            if (belong[edge[i].x]!=belong[edge[i].y])
            {
                out[belong[edge[i].x]]++;
                in[belong[edge[i].y]]++;
            }
        ans=0;
        for (i=1; i<=ssum; i++)
            if (in[i]==0) ans++;
        cout << ans << endl;
        ans2=0;
        for (i=1; i<=ssum; i++)
            if (out[i]==0) ans2++;
        cout << max(ans,ans2)<< endl;
    }
}

你可能感兴趣的:(c,网络,it,ACM,poj)