본문 바로가기

Algorithm

강한 연결 요소 java

반응형

업데이트 예정

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package algo;
 
 
public class B2150 {
    static int V, E;
    static ArrayList<Integer> graph[];
    static ArrayList<Integer> reGraph[];
    static Stack<Integer> stack;
    static boolean visit[];
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        V = Integer.parseInt(st.nextToken());
        E = Integer.parseInt(st.nextToken());
 
        graph = new ArrayList[V + 1];
        reGraph = new ArrayList[V + 1];
        visit = new boolean[V + 1];
        for (int i = 1; i <= V; i++) {
            graph[i] = new ArrayList<Integer>();
            reGraph[i] = new ArrayList<Integer>();
        }
 
        for (int e = 0; e < E; e++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
 
            graph[u].add(v);
            reGraph[v].add(u);
        }
 
        // 첫번째 dfs로 스택을 구한다.
        stack = new Stack<Integer>();
        for(int i=1;i<=V;i++) {
            if(!visit[i])
                dfs(i);
        }
 
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        Arrays.fill(visit, false);
 
        while (!stack.isEmpty()) {
            int node = stack.pop();
            if (!visit[node]) {
                list.add(new ArrayList<Integer>());
                SCC(node,list.get(list.size()-1));
            }
        }
        for(int i=0;i<list.size();i++) {
            Collections.sort(list.get(i));
        }
        
        Collections.sort(list,new NC());
        System.out.println(list.size());
        for(int i=0;i<list.size();i++) {
            for(int j=0;j<list.get(i).size();j++)
                System.out.print(list.get(i).get(j)+" ");
            System.out.println(-1);
        }
    }
 
    // stack 을 구하는 dfs
    public static void dfs(int node) {
        visit[node] = true;
        for (int next : graph[node]) {
            if (!visit[next]) {
                dfs(next);
            }
        }
        stack.add(node);
    }
 
    // stack에 나온걸 가지고 찾는 dfs
    public static void SCC(int node, ArrayList<Integer> list) {
        visit[node] = true;
        list.add(node);
        for (int next : reGraph[node]) {
            if (!visit[next]) {
                SCC(next,list);
            }
        }
    }
    
    //Comporator
    static class NC implements Comparator<ArrayList<Integer>>{
        @Override
        public int compare(ArrayList<Integer> o1,ArrayList<Integer> o2) {
            return o1.get(0- o2.get(0);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
반응형

'Algorithm' 카테고리의 다른 글

레드 블랙트리  (0) 2020.10.15
CCW  (0) 2020.05.03
트라이(Trie) : JAVA / 백준 5052  (0) 2020.04.26
이분 매칭 알고리즘 ( JAVA),열혈강호1,2,3 문제  (0) 2020.04.25
포드-폴커슨 인접리스트 구현하기 JAVA  (0) 2020.04.19