zoukankan      html  css  js  c++  java
  • J

    来源poj1236

    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

    找入度为0的强通量,和出度为0 的强通量,特例,如果只有一个强通量的时候为1 0;

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<queue>
    #include<map>
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+100;
    const double eps=1e-8;
    using namespace std;
    const double pi=acos(-1.0);
    const int inf=0xfffffff;
    const int N=105;
    struct Edge {
         int v,next;
    }edge[N*N];
    int dfn[N],low[N];
    int stack[N],node[N],visit[N],cnt,tot,index;
    int belong[N],bcnt;
    void add_edge(int x,int y)
    {
         edge[cnt].next=node[x];
         edge[cnt].v = y;
         node[x]=cnt++;
        return ;
     }
     void tarjan(int x)//代表第几个点在处理。递归的是点。
     {
         dfn[x]=low[x]=++tot;// 新进点的初始化。
         stack[++index]=x;//进站
         visit[x]=1;//表示在栈里
        for(int i=node[x];i!=-1;i=edge[i].next)
         {
             if(!dfn[edge[i].v]) {//如果没访问过
                tarjan(edge[i].v);//往下进行延伸,开始递归
                 low[x]=min(low[x],low[edge[i].v]);//递归出来,比较谁是谁的儿子/父亲,就是树的对应关系,涉及到强连通分量子树最小根的事情。
            }
            else if(visit[edge[i].v ]){  //如果访问过,并且还在栈里。
                 low[x]=min(low[x],dfn[edge[i].v]);//比较谁是谁的儿子/父亲。就是链接对应关系
             }
         }
         if(low[x]==dfn[x]) //发现是整个强连通分量子树里的最小根。
        {
        	bcnt++;
             do{
             	belong[stack[index]]=bcnt;
                 visit[stack[index]]=0;
                 index--;
                 
             }while(x!=stack[index+1]);//出栈,并且输出。
         }
         return ;
     }
    int in[N],out[N];
    void solve(int n)
    {
    	tot=index=bcnt=0;
    	mm(dfn,0);
    	mm(low,0);
    	mm(in,0);
    	mm(belong,0);
    	mm(out,0);
    	mm(visit,0);
    	rep(i,1,n+1)
    	if(!dfn[i])
    	tarjan(i);
    	int ans1=0,ans2=0;
    	rep(i,1,n+1)
    	{
    		for(int j=node[i];j!=-1;j=edge[j].next)
    		{
    			if(belong[i]!=belong[edge[j].v])
    			{
    				in[belong[edge[j].v]]++;
    				out[belong[i]]++;
    			}
    		}
    	}
    	rep(i,1,bcnt+1)
    	{
    		if(in[i]==0) ans1++;
    		if(out[i]==0) ans2++;
    	}
    	if(bcnt==1)
    	pf("1
    0
    ");
    	else
    	pf("%d
    %d
    ",ans1,max(ans2,ans1));
    }
    int main()
    {
    	int n;
    	while(~sf("%d",&n))
    	{
    		rep(i,1,n+1)
    		node[i]=-1;
    		cnt=1;
    		rep(i,1,n+1)
    		{
    			int x;
    			while(sf("%d",&x)&&x)
    			add_edge(i,x);
    		}
    		solve(n);
    	}
    	return 0;
    }
    
  • 相关阅读:
    【CUDA开发】CUDA面内存拷贝用法总结
    【CUDA开发】CUDA编程接口(一)------一十八般武器
    【CUDA开发】CUDA编程接口(一)------一十八般武器
    【计算机视觉】OPENCV对于有alpha通道的透明背景图片的读取和图片叠加
    【计算机视觉】OPENCV对于有alpha通道的透明背景图片的读取和图片叠加
    【CUDA开发】论CUDA和LAV解码器是否真的实用
    【CUDA开发】论CUDA和LAV解码器是否真的实用
    【VS开发】网络SOCKET编程INADDR_ANY选项
    【VS开发】网络SOCKET编程INADDR_ANY选项
    【FFMPEG】I,P,B帧和PTS,DTS时间戳的关系
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9454229.html
Copyright © 2011-2022 走看看