天天看點

A - Network of Schools POJ - 1236(強連通分量 縮點)

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

1 0

Sample Output

1

2

給定一個有向圖,n個點,求

(1)至少要選幾個頂點,才能從這些頂點出發,到達所有頂點。

(2)至少要加幾個頂點,才能從任意一個頂點出發,到達所有頂點。

對于第一個問題,隻要求縮點之後,入度為0的點數就是答案。

第二個問題,要加最少的邊,那麼把出度為0的點和入度為0的點相連,加的邊就是最少的。也就是求max(出度為0的點的數量,入度為0的點的數量)。

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int prime=2333317;
const int INF=0x3f3f3f3f3f;
int dfn[N],low[N],head[N],dfs_clock,sccnow[N],k,scc_clock,n,in[N],out[N];
stack<int>s;
struct node
{
    int v;
    int next;
} edge[N*100];
void add(int u,int v)
{
    edge[k].v=v;
    edge[k].next=head[u];
    head[u]=k++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    k=scc_clock=dfs_clock=0;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
}
void tarjan(int u)
{
    low[u]=dfn[u]=++dfs_clock;
    s.push(u);
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!sccnow[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        scc_clock++;
        while(1)
        {
            int x=s.top();
            s.pop();
            sccnow[x]=scc_clock;
            if(u==x)
                break;
        }
    }
}
int main()
{
    scanf("%d",&n);
        init();
        for(int i=1; i<=n; i++)
        {
            int x;
            while(scanf("%d",&x)&&x)
            {
                add(i,x);
            }
        }

        for(int i=1; i<=n; i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=head[i]; j!=-1; j=edge[j].next)
            {
                if(j==-1)
                    break;
                int v=edge[j].v;
                if(sccnow[i]!=sccnow[v])
                {
                    in[sccnow[v]]++;
                    out[sccnow[i]]++;
                }
            }
        }
        int ans1=0,ans2=0;
        for(int i=1; i<=scc_clock; i++)
        {
            if(!in[i])
                ans1++;
            if(!out[i])
                ans2++;
        }
        if(scc_clock==1)
            printf("1\n0\n");
        else
            printf("%d\n%d\n",ans1,max(ans1,ans2));
    return 0;
}

           

繼續閱讀