POJ 1236(强连通分量+Kosaraju)

Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19625 Accepted: 7732
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

IOI 1996

题目描述:给一个有向图,问至少选几个点才能遍历全图,添加几条边才能让它变成强连通的

做法:先找出强连通分量,将强连通分量缩点,并找出有p个入度为0的点,就是A了
然后找出有q个出度为0的点,B就是max(p,q)了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 110
using namespace std;
vector<int> g[maxn];//图
vector<int> ng[maxn];//反图
vector<int> s;
int vis[maxn];
int scc[maxn];//记录点所在的强连通分量
int cnt;
struct point
{
    int in;
    int out;
}p[maxn];//记录缩点后的出度和入度
void dfs1(int x)
{
    if(vis[x])return;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++)
    {
        dfs1(g[x][i]);
    }
    s.push_back(x);
}
void dfs2(int x)
{
    if(scc[x])return;
    scc[x]=cnt;
    for(int i=0;i<ng[x].size();i++)
    {
        dfs2(ng[x][i]);
    }
}
int kosaraju(int n)
{
    memset(vis,0,sizeof(vis));
    memset(scc,0,sizeof(scc));
    for(int i=1;i<=n;i++)
    {
        dfs1(i);
    }
    cnt=0;
    for(int i=n-1;i>=0;i--)
    {
        if(!scc[s[i]])
        {
            cnt++;
            dfs2(s[i]);
        }
    }
    return cnt;
}
void init(int n)//注意初始化的项目一个也不能少
{
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
        ng[i].clear();
        p[i].in=0;
        p[i].out=0;
    }
        s.clear();
}
int main()
{
    int n,m;
    int a;
    while(scanf("%d",&n)==1)
    {
        init(n);
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&a)&&a)
            {
                g[i].push_back(a);
                ng[a].push_back(i);
            }
        }
        kosaraju(n);
        int fr;
        int to;
        //当只有一个连通分量时就不需要再加边了
        if(cnt==1)
        {
            printf("1\n0\n");
            continue;
        }
        //缩点操作
        for(int i=1;i<=n;i++)
        {
            fr=scc[i];
            //cout<<fr<<endl;
            for(int j=0;j<g[i].size();j++)
            {
                to=scc[g[i][j]];
                if(to!=fr)//注意当强连通分量内部有边时不计(即缩点的自环)多重边不影响
                {
                    p[to].in++;
                    p[fr].out++;
                }
            }
        }
        int ans1=0,ans2=0;
        for(int i=1;i<=cnt;i++)
        {
            if(p[i].in==0)ans1++;
            if(p[i].out==0)ans2++;
        }
        printf("%d\n%d\n",ans1,max(ans1,ans2));

    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值