POJ 1236 Network of Schools【強連通縮點】【Tarjan算法】

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18759   Accepted: 7387

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


原題鏈接:http://poj.org/problem?id=1236

題意:

 一些學校連成了網絡, 在學校之間存在某個協議:每個學校都維護一張傳送表,表明他們要負責將收到的軟件傳送到表中的所有學校。如果A在B的表中,那麼B不一定在A的表中。

    現在的任務就是,給出所有學校及他們維護的表,問1、如果所有學校都要被傳送到,那麼需要幾份軟件備份;2、如果只用一份軟件備份,那麼需要添加幾條邊?


PS:第二道Tarjan,並且還用了縮點,搞了一下午,注意,此題中Tarjan用到的棧要定義成全局變量.....

參考博客:

http://blog.csdn.net/jiangzh7/article/details/8639709
http://www.cnblogs.com/ACMERY/p/4681803.html
http://blog.csdn.net/wangjian8006/article/details/7888558
http://blog.csdn.net/huzhengnan/article/details/7787595[不錯]
http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html


AC代碼:

/**
  * 行有餘力,則來刷題!
  * 博客鏈接:http://blog.csdn.net/hurmishine
  * 個人博客網站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1000+5;
bool a[maxn][maxn];
int low[maxn],dfn[maxn];
int belong[maxn];
int in[maxn],out[maxn];
bool vis[maxn];
int index,cnt;
int n;
stack<int>s;
void Init()
{
    for(int i=0;i<=n;i++)
    {
        dfn[i]=low[i]=vis[i]=0;
    }
    memset(a,false,sizeof(a));
    index=cnt=0;
}
void Tarjan(int u)
{
    low[u]=dfn[u]=++index;
    s.push(u);
    vis[u]=true;
    for(int v=1;v<=n;v++)
    {
        if(!a[u][v])
            continue;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        int x;
        cnt++;
        do
        {
            x=s.top();
            s.pop();
            belong[x]=cnt;
            vis[x]=false;
        }while(x!=u);
    }
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n)
    {
        Init();
        int x;
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&x)&& x)
            {
                a[i][x]=true;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
                Tarjan(i);
        }

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]&&belong[i]!=belong[j])
                {
                    out[belong[i]]++;
                    in[belong[j]]++;
                }
            }
        }

        int t1=0,t2=0;
        for(int i=1;i<=cnt;i++)
        {
            if(!in[i])
                t1++;
            if(!out[i])
                t2++;
        }
        if(cnt==1)
            cout<<"1"<<endl<<"0"<<endl;
        else
            cout<<t1<<endl<<max(t1,t2)<<endl;
    }
    return 0;
}

AC代碼2:

/**
  * 行有餘力,則來刷題!
  * 博客鏈接:http://blog.csdn.net/hurmishine
  * 個人博客網站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int maxn=1000+5;
int low[maxn],dfn[maxn];
bool vis[maxn];
int belong[maxn];
int in[maxn],out[maxn];
vector<int>G[maxn];
stack<int>s;
int n;
int index,cnt;
void Init()
{
    cnt=index=0;
    for(int i=0;i<=n;i++)
    {
        G[i].clear();
        low[i]=dfn[i]=vis[i]=0;
        //belong[i]=in[i]=out[i]=
    }
}
void Tarjan(int u)
{
    low[u]=dfn[u]=++index;
    vis[u]=true;
    s.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        int x;
        do
        {
            x=s.top();
            s.pop();
            vis[x]=false;
            belong[x]=cnt;
        }while(x!=u);
    }
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n)
    {
        Init();
        int x;
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&x),x)
            {
                G[i].push_back(x);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                Tarjan(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<G[i].size();j++)
            {
                int v=G[i][j];
                if(belong[i]!=belong[v])
                {
                    //cout<<belong[i]<<","<<belong[v]<<endl;
                    out[belong[i]]++;
                    in[belong[v]]++;
                }
            }
        }
        int t1=0,t2=0;
        for(int i=1;i<=cnt;i++)
        {
            if(!in[i])
                t1++;
            if(!out[i])
                t2++;
        }
        //cout<<cnt<<endl;
        if(cnt==1)
            cout<<"1"<<endl<<"0"<<endl;
        else
            cout<<t1<<endl<<max(t1,t2)<<endl;
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章