Implementing the Risk Coefficient of the 4th Blue Bridge Cup with java

Posted by Davidc316 on Mon, 29 Jul 2019 14:12:45 +0200

Risk factor

During the Anti-Japanese War, the tunnel warfare in the central Hebei Plain played an important role.
There are channel connections between several sites in the tunnel, forming a huge network. But there are also hidden dangers, when the enemy found a site, other sites may lose contact.
Let's define a risk factor DF(x,y):
For two sites X and Y (x!= y), if we can find a site z, when Z is destroyed by the enemy, X and y are disconnected, then we call z the key point about X and y. Correspondingly, for any pair of sites X and y, the risk factor DF(x,y) is expressed as the number of key points between the two points.
The task of this topic is to find out the risk coefficient between two stations by knowing the network structure.
The first line of input data contains two integers n (2 <= n <== 1000) and m (0 <= m <== 2000), representing the number of sites and channels respectively.
Next, in line m, two integers u and V (1 <= u, V <= n; u!= v) represent a channel.
The last line, two numbers u and v, represents the risk factor DF (u,v) between the two points.
Output: An integer that outputs - 1 if the two points asked are disconnected.
For example:
User input:
7 6
1 3
2 3
3 4
3 5
4 5
5 6
1 6
Then the program should output:
2

Resource agreements:
Peak memory consumption (including virtual machines) < 64M
CPU consumption < 2000 ms
Please output strictly according to the requirements, and do not print something like "Please input..." Excess content.
All the code is placed in the same source file. After debugging is passed, the source code is copied and submitted.
Note: Do not use package statements. Do not use jdk1.6 or above features.
Note: The name of the main class must be Main, otherwise it will be treated as invalid code.

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static int n, m, start, end;
    public static ArrayList<Integer>[] map;
    public static int count, root;
    public static int[] DFN;
    public static int[] Low;
    public static int[] Parent;
    public ArrayList<Integer> point;
    
    public void init() {
        count = 0;
        root = 1;
        DFN = new int[n + 1];
        Low = new int[n + 1];
        Parent = new int[n + 1];
        point = new ArrayList<Integer>();
        for(int i = 1;i <= n;i++) {
            DFN[i] = -1;
            Low[i] = -1;
            Parent[i] = -1;
        }
    }
    
    public void TarJan(int begin, int parent) {
        DFN[begin] = ++count;
        Low[begin] = DFN[begin];
        Parent[begin] = parent;
        int Childern = 0;
        for(int i = 0;i < map[begin].size();i++) {
            int j = map[begin].get(i);
            if(DFN[j] == -1) {
                Childern++;
                TarJan(j, begin);
                Low[begin] = Math.min(Low[begin], Low[j]);
                if(begin == root && Childern > 1) {
                    if(!point.contains(begin))
                        point.add(begin);
                } else if(begin != root && Low[j] >= DFN[begin]) {
                    if(!point.contains(begin))
                        point.add(begin);
                }
            } else if(j != Parent[begin]) {
                Low[begin] = Math.min(Low[begin], DFN[j]);
            }
        }
    } 
    
    public void dfs(int begin, boolean[] visited) {
        visited[begin] = true;
        for(int i = 0;i < map[begin].size();i++) {
            int j = map[begin].get(i);
            if(visited[j] == false)
                dfs(j, visited);
        }
    }
    
    public void getResult() {
        boolean[] visited = new boolean[n + 1];
        dfs(start, visited);
        if(visited[end] == false) {
            System.out.println("-1");
            return;
        }
        init();
        TarJan(1, 0);
        int count = 0;
        for(int i = 0;i < point.size();i++) {
            int j = point.get(i);
            if(j != start && j != end) {
                visited = new boolean[n + 1];
                visited[j] = true;
                dfs(start, visited);
                if(visited[end] == false)
                    count++;
            }
        }
        System.out.println(count);
    }
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        map = new ArrayList[n + 1];
        for(int i = 1;i <= n;i++)
            map[i] = new ArrayList<Integer>();
        for(int i = 1;i <= m;i++) {
            int u = in.nextInt();
            int v = in.nextInt();
            map[u].add(v);
            map[v].add(u);
        }
        start = in.nextInt();
        end = in.nextInt();
        test.getResult();
    }
}

Topics: network Java