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
0
0
1 0
Sample Output
1
2
题意:有n台电脑,现在有些电脑可以传数据到另外的电脑上,即有k条边。问题1:现在问至少需要给多少台电脑传送数据才能到达全部电脑。问题2:问至少添加多少条边才能使得往任意一台电脑传送数据便可以到达所有电脑。

题解:这是一道很好的题目,包含了强连通分量的查找以及将强连通分量缩成DAG的算法。所以做完后,感觉对强连通有了挺深的了解。先构造一个有向图出来,然后求强连通分量,最后找出那些没有入度的强连通,就是问题1的答案。比较出度为0的强连通个数和入度为0的强连通个数,取最大的,即为问题2的答案。(因为要将该DAG变为一个强连通,那么最少连边就考虑吧那些出度零点连一条边到入度零点。所以答案就是在入度零点和出度零点中找最大值)

代码:

#include <iostream>
#include <bitset>
#include <cstdio>
#include <queue>
#include <stack>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5+7;
stack<int> s;
vector<int> g[maxn];
int dfn[maxn],low[maxn],tot,n,ans = maxn,vis[maxn];
int cnt,id[maxn];
int in[maxn],out[maxn];
void tarjan(int a)
{
    low[a] = dfn[a] = ++tot;
    s.push(a),vis[a] = 1;
    for (int i = 0;i < g[a].size();i++)
    {
        int v = g[a][i];
        if(!dfn[v]){
            tarjan(v);
            low[a] = min(low[v],low[a]);
        }else if(vis[v]){
            low[a] = min(low[a],dfn[v]);
        }
    }
    if(dfn[a] == low[a])
    {
        cnt++;
        while(1)
        {
            int now = s.top();
            s.pop();
            vis[now] = 0;
            id[now] = cnt;
            if(now==a) break;
        }
    }
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        while(!s.empty())
            s.pop();
        for (int i = 1;i <= n;i++)
        {
            int a;
            scanf("%d",&a);
            while (a!=0){
                g[i].push_back(a);
                scanf("%d",&a);
            }
        }
        memset(dfn,0,sizeof(dfn));
        memset(vis,0,sizeof(vis));
        tot = 0,cnt = 0;
        for (int i = 1;i <= n;i++)
        {
            if(!dfn[i]) tarjan(i);
        }
        if(cnt==1){
            printf("1\n0\n");
            continue;
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 0;j < g[i].size();j++){
                int u = i,v = g[i][j];
                if(id[u]!=id[v])
                {
                    out[id[u]]++;
                    in[id[v]]++;
                }
            }
        }
        int ans1 = 0,ans2 = 0;
        for (int i = 1;i <= cnt;i++) if(in[i]==0) ans1++;
        printf("%d\n",ans1);
        for (int i = 1;i <= cnt;i++) if(out[i]==0) ans2++;
        printf("%d\n",max(ans1,ans2));
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值