zoukankan      html  css  js  c++  java
  • POJ

    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个学校之间有单向的网络,每个学校可以通过单向网络向周边的学校传输信息。

    问题1:初始至少需要向多少个学校发放信息,使得网络内所有的学校最终都能收到信息。

    问题2:至少需要添加几条边,使得任意向一个学校发送信息后,经过若干次传送,所有的学校都能收到信息。

    题解:

    分析题意发现其实就是问对于一个有向无环图:
    1.至少要选几个点,才能从这些点出发到达所有点 。

    2.至少加入几条边,就能从图中任何一个点出发到达所有点。

    当然图不一定是无环图,那么就用到了Tarjan算法来找到强连通分量,然后再缩点就变成了有向无环图。

    然后不难想出对于第一个问题答案就是有向无环图中入度为0的点的个数。对于第二个问题仔细想一下就发现了如果要达到题意肯定每个点都有出有入,所以要消灭所有入度为零和出度为零的点,所以如果入度为0的个数是n,出度为0的个数是m,至少添加边的条数就是max(n,m)。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    
    #define MAXN 205
    
    bool is_instack[MAXN];//记录节点是否在栈中 
    int stack[MAXN],top;
    bool map[MAXN][MAXN];//存图,这里用的矩阵仅供理解,具体做题自己调整。
    int DFN[MAXN];//记录节点第一次被访问时的时间
    int LOW[MAXN];//记录节点与节点的子树节点中最早的步数 
    int time; 
    int Belong[MAXN];//记录每个节点属于的强连通分量编号 
    int N,cnt;//N是点数,cnt是强连通分量编号。
    
    void Tarjan(int x){
        DFN[x] = LOW[x] = ++time;
        is_instack[x] = true;
        stack[++top] = x;
        for(int i=1 ; i<=N ; ++i){
            if(map[x][i] == false)continue;
            if(!DFN[i]){
                Tarjan(i);
                if(LOW[i]<LOW[x]){
                    LOW[x] = LOW[i];
                }
            }
            else if(is_instack[i] && DFN[i]<DFN[x]){ //这里注意不能直接else,因为DFN[i]!=0还有可能是横叉边。
                LOW[x] = min(LOW[x],DFN[i]);
            }
        }
        if(DFN[x] == LOW[x]){
            ++cnt;
            int mid;
            do{
                mid = stack[top--];
                is_instack[mid] = false;
                Belong[mid] = cnt;
            }while(mid != x);
        }
    }
    
    int in[MAXN];//记录缩点后点的入度 
    int out[MAXN];//记录缩点后点的出度 
    
    void init(){
    	memset(map,false,sizeof(map));
        memset(DFN,0,sizeof(DFN));
        memset(LOW,0,sizeof(LOW));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(is_instack,false,sizeof(is_instack));
        time = 0;
        cnt = 0;
        top = 0;
    }
    
    int main(){
        while(scanf("%d",&N)!=EOF){
        	init();
            for(int i=1 ; i<=N ; ++i){
            	int t;
            	while(scanf("%d",&t) && t)map[i][t] = 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(map[i][j] && Belong[i] != Belong[j]){
            			++out[Belong[i]];
            			++in[Belong[j]];
    				}
    			}
    		}
    		if(cnt==1){
    			printf("1
    0
    ");
    			continue;
    		}
    		int inzero = 0,outzero = 0;
    		for(int i=1 ; i<=cnt ; ++i){
    			if(!in[i])++inzero;
    			if(!out[i])++outzero;
    		}
            printf("%d
    %d
    ",inzero,max(inzero,outzero));
        }
    
        return 0;
    }


  • 相关阅读:
    C#往SQLServer中插入大数据
    (5.1.2)引擎管理——启动参数与单用户模式启动
    (5.1.1)引擎管理——包含数据库解决迁移造成孤立用户问题
    【转】mysql的group_concat函数,默认最大长度是1024
    【转】【mysql面试】https://blog.csdn.net/hanfazy/article/details/14520437
    【基本优化实践】【1.2】索引优化——重建索引、查看堆表、查看索引使用情况、查看索引碎片率
    【基本优化实践】【1.1】IO优化——把文件迁移到不同物理磁盘
    sql server优化方向?
    mysql中的事务隔离级别与锁
    Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514080.html
Copyright © 2011-2022 走看看