计蒜客 Islands Tarjan强连通

On the mysterious continent of Tamriel, there is a great empire founded by human.

To develope the trade, the East Empire Company is set up to transport goods from place to place.

Recently, the company wants to start their business in Solstheim, which is consists of NN islands.

Luckily, there are already MM sea routes.

All routes are one-way, and the ii-th route can transport person and goods from island u_iui to v_ivi.

Now, the company nominates you a particular job to plan some new routes to make sure that person and goods can be transported between any two islands.

Furthermore, because the neighboring regions are under attack by an increasing number of dragons, limited resources can be used to set up new routes.

So you should plan to build new routes as few as possible.

Input Format

The first line contains an integer TT, indicating that there are TT test cases.

For each test case, the first line includes two integers N~(N \leq 10000)N (N10000) and M~(M \leq 100000)M (M100000), as described above.

After that there are MM lines. Each line contains two integers u_iui and v_ivi.

Output Format

For each test case output one integer, represent the least number of routes required to new.

样例输入
2
4 3
1 2
2 3
3 4
4 4
1 2
1 4
3 2
3 4
样例输出
1
2
题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

缩点后,统计入度为0的个数A,出度为0的个数B。

1、出度(入读)为0的点都需要加边。

2、 贪心先两两配对;

       多的出度(入度)为0的点再和一般的点来做边

所以结果为max(A , B)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;


public class Main {
    
    public static void main(String[] args) {
        new Task().solve();
    }
}

class Task {
    InputReader in = new InputReader(System.in) ;
    PrintWriter out = new PrintWriter(System.out) ;
    
    final int N = 10008 ;  
	final int M = 100008 ;  
	  
    class Edge {  
        int u, v, next;  
  
        Edge(int u, int v , int next) {  
            this.u = u;  
            this.v = v;   
            this.next = next;  
        }  
    }  
  
    Edge[] e = new Edge[M];  
    int[] head = new int[N];  
    int eid;  
  
    void add(int u, int v) {  
        e[eid] = new Edge(u, v , head[u]);  
        head[u] = eid++;  
    }  
  
    int[] dfn = new int[N];  
    int[] low = new int[N];  
    int[] stack = new int[N];  
    boolean[] inStack = new boolean[N];  
    int[] belong = new int[N];  
    int[] bccSize = new int[N];  
    int top, dfnTime, bccId;  
  
    void tarjan(int u, int father) {  
        dfn[u] = low[u] = ++dfnTime;  
        stack[top++] = u;  
        inStack[u] = true;  
        for (int i = head[u]; i != -1; i = e[i].next) {  
            int v = e[i].v;  
            if (dfn[v] == -1) {  
                tarjan(v, u);  
                low[u] = Math.min(low[u], low[v]);   
            } else if (inStack[v])  
                low[u] = Math.min(low[u], dfn[v]);  
        }  
        if (low[u] == dfn[u]) {  
            int now;  
            bccId++;  
            do {  
                now = stack[--top];  
                inStack[now] = false;  
                belong[now] = bccId;  
                bccSize[bccId]++;  
            } while (now != u);  
        }  
    }  
  
    void clear() {  
        Arrays.fill(dfn, -1);  
        Arrays.fill(head, -1);  
        Arrays.fill(inStack, false);  
        Arrays.fill(bccSize, 0);  
        eid = top = dfnTime = bccId = 0;  
    }  
      
    int doTarjan(int n){  
        for (int i = 1; i <= n; i++) {  
            if (dfn[i] == -1)  
                tarjan(i, i);  
        }  
  
        if(bccId == 1){
        	return 0 ; 
        }
        int[] inCnt = new int[bccId+1] ;
        int[] outCnt = new int[bccId+1] ;
        Arrays.fill(inCnt, 0) ;
        Arrays.fill(outCnt, 0) ;
        
        for (int u = 1; u <= n; u++) {  
            for (int i = head[u]; i != -1; i = e[i].next) {  
                if (belong[u] != belong[e[i].v]){
                	outCnt[belong[e[i].u]]++ ;
                	inCnt[belong[e[i].v]]++ ;
                } 
            }  
        }  
  
        int inSum = 0 , outSum = 0 ;  
        for (int i = 1; i <= bccId; i++) {  
           if(inCnt[i] == 0){
        	   inSum++ ;
           }
           if(outCnt[i] == 0){
        	   outSum++ ;
           }
        }  
        return Math.max(inSum, outSum) ;
    }  
    
    void solve(){
    	int t = in.nextInt() ;
    	while(t-- > 0){
    		int n = in.nextInt() ;
    		int m = in.nextInt() ;
    		clear() ;
    		for(int i = 1 ; i <= m ; i++){
    			add(in.nextInt() , in.nextInt()) ;
    		}
    		out.println(doTarjan(n)) ;
    	}
    	out.flush() ; 
    }
    
}
 

class InputReader {    
    public BufferedReader reader;    
    public StringTokenizer tokenizer;    
    
    public InputReader(InputStream stream) {    
        reader = new BufferedReader(new InputStreamReader(stream), 32768);    
        tokenizer = new StringTokenizer("");    
    }    
    private void eat(String s) {    
        tokenizer = new StringTokenizer(s);    
    }    
    
    public String nextLine() {     
        try {    
            return reader.readLine();    
        } catch (Exception e) {    
            return null;    
        }    
    }    
    
    public boolean hasNext() {    
        while (!tokenizer.hasMoreTokens()) {    
            String s = nextLine();    
            if (s == null)    
                return false;    
            eat(s);    
        }    
        return true;    
    }    
    
    public String next() {    
        hasNext();    
        return tokenizer.nextToken();    
    }    
    
    public int nextInt() {    
        return Integer.parseInt(next());    
    }    
    
    public int[] nextInts(int n) {    
        int[] nums = new int[n];    
        for (int i = 0; i < n; i++) {    
            nums[i] = nextInt();    
        }    
        return nums;    
    }    
    
    public long nextLong() {    
        return Long.parseLong(next());    
    }    
    
    public double nextDouble() {    
        return Double.parseDouble(next());    
    }    
    
    public BigInteger nextBigInteger() {    
        return new BigInteger(next());    
    }    
    
}    






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值