CCF之高速公路(java)

试题编号: 201509-4
试题名称: 高速公路
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  某国有n个城市,为了使得城市间的交通更便利,该国国王打算在城市之间修一些高速公路,由于经费限制,国王打算第一阶段先在部分城市之间修一些单向的高速公路。
  现在,大臣们帮国王拟了一个修高速公路的计划。看了计划后,国王发现,有些城市之间可以通过高速公路直接(不经过其他城市)或间接(经过一个或多个其他城市)到达,而有的却不能。如果城市A可以通过高速公路到达城市B,而且城市B也可以通过高速公路到达城市A,则这两个城市被称为便利城市对。
  国王想知道,在大臣们给他的计划中,有多少个便利城市对。
输入格式
  输入的第一行包含两个整数n, m,分别表示城市和单向高速公路的数量。
  接下来m行,每行两个整数a, b,表示城市a有一条单向的高速公路连向城市b。
输出格式
  输出一行,包含一个整数,表示便利城市对的数量。
样例输入
5 5
1 2
2 3
3 4
4 2
3 5
样例输出
3
样例说明

  城市间的连接如图所示。有3个便利城市对,它们分别是(2, 3), (2, 4), (3, 4),请注意(2, 3)和(3, 2)看成同一个便利城市对。
评测用例规模与约定
  前30%的评测用例满足1 ≤ n ≤ 100, 1 ≤ m ≤ 1000;
  前60%的评测用例满足1 ≤ n ≤ 1000, 1 ≤ m ≤ 10000;
  所有评测用例满足1 ≤ n ≤ 10000, 1 ≤ m ≤ 100000。

解题代码(java):

import java.util.Scanner;
import java.util.Vector;
import java.lang.Math;
public class Main {		
	@SuppressWarnings("unchecked")
	public static Vector<Integer> []g = new Vector[10005];	
	@SuppressWarnings("unchecked")
	public static Vector<Integer> component[] = new Vector[10005];			
	public static int []STACK = new int[10005];	
	static int TOP=0;        
	static boolean [] instack = new boolean[10005];    	
	static int []dfn = new int[10005];    
	static int []low = new int[10005];    	
	static int index=1;    	
	static int cnt=0; 	
	public static void main(String[] args)	{		
		int n =0,m=0,x=0,y=0;    	    
		int ans=0;  	    
		Scanner in = new Scanner(System.in);	   
		n = in.nextInt();	   
		m = in.nextInt();	
		for(int i = 0;i<n+1;i++){	    	
			 g[i] = new Vector<Integer>();	    		   
		}
	    for(int i = 0;i<5000;i++) {
	    	component[i] = new Vector<Integer>();
        }
	    for(int i = 0;i<m;i++){	
	    	x= in.nextInt();	    	
	    	y=in.nextInt();	    	
	    	g[x].add(y);
	    	
	    }
        in.close();	
        for(int i=1;i<=n;i++){    	        
        	if(dfn[i]==0)	        	
        		tarjan(i);    	    
        }  	    	    
        for(int i=1;i<=cnt;i++){
             Vector<Integer> vec=component[i];    	       
             int sz=vec.size();    	       	        
             if(sz==1)	        	
            	 continue;    	        
             else	        	
            	 ans+=sz*(sz-1)/2;    	    
                 	   
        System.out.println(ans);	    	     	
        }
    }
	static void tarjan(int x){    	    
		dfn[x]=low[x]=index++;    	    
		instack[x]=true;    	    
		STACK[++TOP]=x;    	   
		int j; 	    	    
		for(int i=0;i<g[x].size();i++){   	       
			j=g[x].get(i);    	       
			if(dfn[j]==0){    	            
				tarjan(j);    	            
				low[x]=Math.min(low[x],low[j]);    	        
			} else if(instack[j]){                    
				low[x]=Math.min(low[x],dfn[j]);    	       
			}    	   
		}    	    
		if(dfn[x]==low[x]){    	            
			cnt++;    	            
			do{    	                
				j=STACK[TOP--];    	                
				instack[j]=false;    	                
				component[cnt].add(j);   	            
			}while(x!=j);
		}	
	}
}		

得分70分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值