Poj 1236 (scc- Kosaraju算法实现)

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9649 Accepted: 3834

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

类似训练指南上的那道‘等价性证明’。一个有向图,求从最少的点出发遍历整个图所需点的个数,和使这个图变成强连通图所需添加的最少边数。先说第二问,先求scc缩点,找到所有入度为0的点的个数a,出度为0的点的个数b。则答案就是max(a,b),这个结论在此不给出证明。而对于第一问,可以看出就是入度为0的点的个数a。
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
const int maxn = 100 + 5;
const int INF = 1000000000;

int n; // 顶点数
vector<int> G[maxn]; // 图的邻接表表示
vector<int> rG[maxn]; // 把边反向后的图
vector<int> vs; // 后续遍历顺序的顶点列表
bool used[maxn]; // 访问标记
int cmp[maxn]; // 所属强连通分量的拓扑序

void add_edge(int from,int to){
    G[from].push_back(to);
    rG[to].push_back(from);
}

void dfs(int x){
    used[x] = true;
    for(int i = 0;i < G[x].size();i++){
        if(!used[G[x][i]]) dfs(G[x][i]);
    }
    vs.push_back(x);
}

void rdfs(int x,int k){
    used[x] = true;
    cmp[x] = k;
    for(int i = 0;i < rG[x].size();i++){
        if(!used[rG[x][i]]) rdfs(rG[x][i],k);
    }
}

int scc(){
    memset(used,0,sizeof(used));
    vs.clear();
    for(int i = 0;i < n;i++){
        if(!used[i]) dfs(i);
    }
    memset(used,0,sizeof(used));
    int k = 0;
    for(int i = vs.size()-1;i >= 0;i--){
        if(!used[vs[i]]) rdfs(vs[i],k++);
    }
    return k;
}

int in[maxn],out[maxn];

int main(){
    while(scanf("%d",&n) != EOF){
        for(int i = 0;i < maxn;i++){
            G[i].clear();
            rG[i].clear();
        }
        for(int i = 0;i < n;i++){
            int x;
            while(scanf("%d",&x)){
                if(x == 0) break;
                x--;
                add_edge(i,x);
            }
        }
        int ans1,ans2 = scc();
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(int i = 0;i < n;i++){
            for(int j = 0;j < G[i].size();j++){
                int to = G[i][j];
                if(cmp[i] != cmp[to]){
                    in[cmp[to]]++;
                    out[cmp[i]]++;
                }
            }
        }
        int a1 = 0,a2 = 0;
        for(int i = 0;i < ans2;i++){
            if(in[i] == 0) a1++;
            if(out[i] == 0) a2++;
        }
        if(ans2 != 1) ans2 = max(a1,a2);
        else ans2 = 0;
        ans1 = a1;
        printf("%d\n",ans1);
        printf("%d\n",ans2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值