[usaco]5.3.4强联通分支,双联通分支 Network of Schools

简介: <p> <br> Network of Schools<br> IOI '96 Day 1 Problem 3 <br> A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a

 
Network of Schools
IOI '96 Day 1 Problem 3
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.

PROGRAM NAME: schlnet
INPUT FORMAT
The first line of the input file 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.

SAMPLE INPUT (file schlnet.in)
5
2 4 3 0
4 5 0
0
0
1 0

OUTPUT FORMAT
Your program should write two lines to the output file. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.


SAMPLE OUTPUT (file schlnet.out)
1
2

 

--------------------------------------------------------------------------------
算法:强联通分支,双联通分支
首先使用强联通分子算法,把同一个分支变成一个点,然后把有向图当成无向图,求得双联通分支。
求出每一个联通分量的入度为0的点和出度为0的点。
task A很好求,只需要把所有的入度为0的点加起来就是了;如果某个联通分量没有入度为0 的点,那么就加1.
task B要求把这些联通分量连接成一个连通图,最简单的办法是把这些联通分量连成环。
 假第i个联通分量出度为0的点有out个,第i+1个联通分量入度为0的点有in个,那么最少需要max(out,in)个遍才能把他们连接起来。
 由于要求最少的遍,那么当out和in最接近的时候,能节省很多边,
 考虑这个例子(in,out):(1,2),(1,1),(2,1);如果这样按顺序连接起来的话,需要2+1+2+1条边,但如果2和3颠倒一下顺序,则需要1+2+1+1条边
 因此,每次选取还未匹配的分量 出度最大的一个分量和入度最大的另一个分量,连接起来。
这题好麻烦,很多需要注意的问题。

这题是用java写的,运行速度比c++慢多了


Executing...
   Test 1: TEST OK [0.090 secs, 254972 KB]
   Test 2: TEST OK [0.090 secs, 254972 KB]
   Test 3: TEST OK [0.108 secs, 254972 KB]
   Test 4: TEST OK [0.126 secs, 254972 KB]
   Test 5: TEST OK [0.126 secs, 254972 KB]
   Test 6: TEST OK [0.126 secs, 255384 KB]
   Test 7: TEST OK [0.144 secs, 255248 KB]
   Test 8: TEST OK [0.162 secs, 255420 KB]
   Test 9: TEST OK [0.270 secs, 255792 KB]
   Test 10: TEST OK [0.144 secs, 255556 KB]
   Test 11: TEST OK [0.162 secs, 255240 KB]    

]-----------------------------------------------------------------------------------

 

/*
 ID:yunleis3
 PROG:schlnet
 LANG:JAVA
 */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
 
public class schlnet {
	private static final ArrayList<Integer> outdgree0 = null;
	static ArrayList<Integer>[] list;
	static ArrayList<Integer>[] reverse_list;
	static int current_cpnt=0;
	static int []dfs_nmbr;
	static int []cpnt_nmbr;
	static int []dfs_high;
	static int dfs_n;
	static LinkedList<Integer> dfs_stack=new LinkedList<Integer>();
	public static void main(String [] arg) throws IOException{
		File file=new File("schlnet.in");
		long begin=System.currentTimeMillis();
		Scanner scan=new Scanner(file);
		FileWriter w=new FileWriter(new File("schlnet.out"));
		int total =scan.nextInt();
		list=new ArrayList [total+1];
		reverse_list=new ArrayList [total+1];
		int out=0;
		int in=0;
		int[] indegree=new int [total+1];
		for(int i=1;i<total+1;i++){reverse_list[i]=new ArrayList<Integer>();}
		for(int i=1;i<total+1;i++){
			list[i]=new ArrayList<Integer>();
			while(true){
				int t=scan.nextInt();
				if(t==0) break;
				list[i].add(t);
				//indegree[t]++;
				reverse_list[t].add(i);
			}
			if(list[i].size()==0) out++;
		}
		dfs_nmbr=new int[total+1];
		cpnt_nmbr=new int[total+1];
		dfs_high=new int[total+1];
		dfs_n=total+1;
		for(int i=1;i<=total;++i){
			if(dfs_nmbr[i]==0)
				scc(i);
		}
		ArrayList<Integer>[] list1=new ArrayList[current_cpnt+1];
		ArrayList<Integer>[] reverse_list1=new ArrayList[current_cpnt+1];
		for(int i=1;i<current_cpnt+1;++i){
			list1[i]=new ArrayList<Integer>();
			reverse_list1[i]=new ArrayList<Integer>();
		}
		for(int i=1;i<=total;i++){
			int first=cpnt_nmbr[i];
			for(int j=0;j<list[i].size();++j){
				int next=cpnt_nmbr[list[i].get(j)];
				if(next==first)
					continue;
				boolean flag=false;
				for(int s=0;s<list1[first].size();++s){
					if(list1[first].get(s)==next)
					{
						flag=true;
						break;
					}
				}
				if(!flag)
				{
					list1[first].add(next);
					reverse_list1[next].add(first);
					indegree[next]++;
				}
			}
		}
		list=list1;
		reverse_list=reverse_list1;
		
		total=current_cpnt;
		
		
		for(int i=1;i<=total;i++){
			if(indegree[i]==0) in++;
		}
		
		
		
		boolean[] visited=new boolean[total+1];
		int [] componentnum=new int[total+1];
		for(int i=0;i<total+1;++i)
		{
			visited[i]=false;
			componentnum[i]=-1;
			componentnum[i]=-1;
		}
		LinkedList<Integer> stack=new LinkedList<Integer>();
		int cpnt_g=0;
		ArrayList<Integer> indgrees0=new ArrayList<Integer>();
		ArrayList<Integer> outdgrees0=new ArrayList<Integer>();
		while(true){
			boolean find0indgree=false;
			int root=0;
			boolean finished=true;
			ArrayList<Integer> nums=new ArrayList<Integer>();
			for(int i=1;i<total+1;++i){
				if(!visited[i]&&indegree[i]==0){
					root=i;
					find0indgree=true;
				}
				if(!visited[i])
					finished=false;
			}
			if(finished)
				break;
			if(!find0indgree){
				for(int i=1;i<total+1;++i)
					if(!visited[i])
						root=i;
			}
			stack.addFirst(root);
			visited[root]=true;
			int cmpnt_l=cpnt_g++;
			nums.add(root);
			componentnum[root]=cmpnt_l;
			while(!stack.isEmpty()){
				int r=stack.removeFirst();				
				for(int i=0,size=list[r].size();i<size;++i){
					int next=list[r].get(i);
					if(!visited[next])
					{
						visited[next]=true;
						stack.addFirst(next);
						componentnum[next]=cmpnt_l;
						nums.add(next);
					}
				}
				for(int i=0,size=reverse_list[r].size();i<size;++i){
					int next=reverse_list[r].get(i);
					if(!visited[next])
					{
						visited[next]=true;
						stack.addFirst(next);
						componentnum[next]=cmpnt_l;
						nums.add(next);
					}
				}
			}
			int ins0=0,outs0=0;
			for(int i=0;i<nums.size();++i){
				if(indegree[nums.get(i)]==0)
					++ins0;
				if(list[nums.get(i)].size()==0)
					++outs0;
			}
			indgrees0.add(ins0);
			outdgrees0.add(outs0);
			
		}
		int taska=0,taskb=0;
		if(total==1){
			taska=1;
			taskb=0;
		}
		else if(cpnt_g==1&&indgrees0.get(0)==0&&outdgrees0.get(0)==0){
			taska=1;
			taskb=0;
		}else if(cpnt_g==1){
			taska=indgrees0.get(0);
			if(taska==0)
				taska=1;
			taskb=indgrees0.get(0)>outdgrees0.get(0)?indgrees0.get(0):outdgrees0.get(0);
		}
		else{
			in=0;out=0;
			boolean[] outvisited=new boolean[cpnt_g];
			boolean[] invisited=new boolean[cpnt_g];
			for(int i=0;i<cpnt_g;++i){
				//pick the max out;
				int ptr=-1;
				for(int j=0;j<cpnt_g;++j){
					if(!outvisited[j]&&(ptr==-1||outdgrees0.get(ptr)>outdgrees0.get(ptr)))
						ptr=j;
				}
				int ptrin=-1;
				for(int j=0;j<cpnt_g;++j){
					if(!invisited[j]&&(ptrin==-1||indgrees0.get(ptrin)>indgrees0.get(ptrin)))
						ptrin=j;
				}
				outvisited[ptr]=true;
				invisited[ptrin]=true;
				in=indgrees0.get(ptrin);
				out=outdgrees0.get(ptr);
				if(in==0)
					in=1;
				if(out==0)
					out=1;
				taska+=in;
				taskb+=max(out,in);
			}
//			for(int i=0;i<cpnt_g;++i){
//				int next=(i+1)%cpnt_g;
//				out=outdgrees0.get(i);
//				if(out==0)
//					out=1;
//				in=indgrees0.get(next);
//				if(in==0)
//					in=1;
//				taska+=in;
//				taskb+=max(out,in);
//			}
			
		}
		
		
		FileWriter wr=new FileWriter(new File("schlnet.out"));
		wr.write(taska+"\n"+taskb+"\n");
		wr.flush();
		wr.close();
		System.out.print(taska+"\n"+taskb+"\n");
	}
	static void scc(int v){
		dfs_nmbr[v]=dfs_n--;
		dfs_stack.addFirst(v);
		dfs_high[v]=dfs_nmbr[v];
		for(int i=0;i<list[v].size();++i){
			int w=list[v].get(i);
			if(dfs_nmbr[w]==0){
				scc(w);
				dfs_high[v]=dfs_high[v]>dfs_high[w]?dfs_high[v]:dfs_high[w];				
			}
			else
				if(dfs_nmbr[w]>dfs_nmbr[v]&&cpnt_nmbr[w]==0)
					dfs_high[v]=max(dfs_high[v],dfs_nmbr[w]);
		}
		if(dfs_high[v]==dfs_nmbr[v]){
			++current_cpnt;
			while(!dfs_stack.isEmpty()){
				int x=dfs_stack.removeFirst();
				cpnt_nmbr[x]=current_cpnt;
				if(x==v){
					break;
				}
			}
		}
	}
	static int max(int x,int y){
		return x>y?x:y;
	}
}


 

目录
相关文章
|
1天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
11天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
449 192
|
3天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
9天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
5天前
|
消息中间件 安全 NoSQL
阿里云通过中国信通院首批安全可信中间件评估
近日,由中国信通院主办的 2025(第五届)数字化转型发展大会在京举行。会上,“阿里云应用服务器软件 AliEE”、“消息队列软件 RocketMQ”、“云数据库 Tair”三款产品成功通过中国信通院“安全可信中间件”系列评估,成为首批获此认证的中间件产品。此次评估覆盖安全可信要求、功能完备性、安全防护能力、性能表现、可靠性与可维护性等核心指标,标志着阿里云中间件产品在多架构适配与安全能力上达到行业领先水平。
315 195

热门文章

最新文章