zoukankan      html  css  js  c++  java
  • poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 13800   Accepted: 5504

    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

    题意:向学校发软件,1、问最少向多少个学校发软件就可以间接让所有学校都得到软件 2、问最少添加多少条边就可以使 任意向一所学校发软件 就可以让其余所有学校都收到软件

    题解:求出强连通分支后缩点,求出所有入度和出度问0的点的个数insum和outsum,则答案为insum和max(insum,outsum)

    输入:第一行是一个n代表接下来有n行,每行输入小于n个数以0结尾,代表这个点与第i行(当前行数)的值i点相连

    #include<stdio.h>
    #include<string.h>
    #include<stack>
    #include<vector>
    #include<algorithm>
    #define MAX 1100
    #define MAXM 2001000
    #define INF 0x7ffffff
    using namespace std;
    int low[MAX],dfn[MAX];
    int head[MAX],ans;
    int instack[MAX];
    int dfsclock,scccnt;
    int in[MAX],out[MAX];
    int sccno[MAX];
    stack<int>s;
    vector<int>newmap[MAX];
    vector<int>scc[MAX];
    struct node
    {
    	int beg,end,next;
    }edge[MAXM];
    void init()
    {
    	ans=0;
    	memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
    	edge[ans].beg=u;
    	edge[ans].end=v;
    	edge[ans].next=head[u];
    	head[u]=ans++;
    }
    void tarjan(int u)
    {
    	int v,i;
    	s.push(u);
    	instack[u]=1;
    	low[u]=dfn[u]=++dfsclock;
    	for(i=head[u];i!=-1;i=edge[i].next)
    	{
    		v=edge[i].end;
    		if(!dfn[v])
    		{
    			tarjan(v);
    			low[u]=min(low[u],low[v]);
    		}
    		else if(instack[v])
    		    low[u]=min(low[u],dfn[v]);
    	}
    	if(low[u]==dfn[u])
    	{
    		scccnt++;
    		while(1)
    		{
    			v=s.top();
    			s.pop();
    			instack[v]=0;
    			sccno[v]=scccnt;
    			if(v==u)
    			    break;
    		}
    	}
    }
    void find(int l,int r)
    {
    	dfsclock=scccnt=0;
    	memset(low,0,sizeof(low));
    	memset(dfn,0,sizeof(dfn));
    	memset(instack,0,sizeof(instack));
    	memset(sccno,0,sizeof(sccno));
    	for(int i=l;i<=r;i++)
    	{
    		if(!dfn[i])
    		    tarjan(i);
    	}
    }
    void suodian()
    {
    	int u,v;
    	memset(in,0,sizeof(in));
    	memset(out,0,sizeof(out));
    	for(int i=0;i<ans;i++)
    	{
    		u=sccno[edge[i].beg];
    		v=sccno[edge[i].end];
    		if(v!=u)
    		{
    			newmap[u].push_back(v);
    			in[v]++;
    			out[u]++;
    		}
    	}
    }
    void solve()
    {
    	int i,j;
    	int sum,insum,outsum;
    	insum=outsum=0;
    	if(scccnt==1)
    	{
    		printf("1
    0
    ");
    		return ;
    	}
    	for(i=1;i<=scccnt;i++)
    	{
    		if(!in[i])
    		    insum++;
    		if(!out[i])
    		    outsum++;
    	}
    	sum=max(insum,outsum);
    	printf("%d
    %d
    ",insum,sum);
    }
    int main()
    {
    	int n,m,j,i,a;
    	while(scanf("%d",&n)!=EOF)
    	{
    		init();
    		for(i=1;i<=n;i++)
    		{
    			while(scanf("%d",&a),a)
    				add(i,a);
    		}
    		find(1,n);
    		suodian();
    		solve();
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    打包spring项目遇到的坑 Unable to locate Spring NamespaceHandler for XML schema ……shcema/context 产生的原因及解决方法
    Mybatis 从入门到精通一:mybatis的入门
    IO流系列一:输入输出流的转换
    本地模拟 gitlab ci 的 demo 项目
    docker 容器中部署 Go 服务时,请求 https 文件时抛错
    微信支付『支付失败,如果已扣款,资金在0-3个工作日原路返回』踩坑案例及解决方案
    PHP 安装 扩展时 抛出 /usr/local/Cellar/php@7.1/7.1.25/pecl 异常解决
    SpringBoot2 引入 Aop
    Mac 下 IDEA 中 SpringBoot 如何利用 DevTool 开启热部署
    MySql数据库中敏感字段加密处理方案
  • 原文地址:https://www.cnblogs.com/tonghao/p/4904244.html
Copyright © 2011-2022 走看看