개발일지

[백준]단절선(11400) 본문

알고리즘

[백준]단절선(11400)

devbh 2020. 3. 26. 18:38


import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main_11400 {
    static int count = 1;
    static int order[];

    static class Node implements Comparable<Node>{
        int a;
        int b;
        Node(int a,int b){
            this.a = a;
            this.b = b;
        }
        @Override
        public int compareTo(Node o) {
            if(this.a==o.a) {
                return this.b - o.b;
            }
            return this.a-o.a;
        }
    }
    static ArrayList<Integer> list[];
    static PriorityQueue<Node> pq = new PriorityQueue<>(); 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int v = sc.nextInt();    // 정점
        int e = sc.nextInt();    // 간선

        list = new ArrayList[v+1];
        order = new int[v+1];

        for(int i=0;i<=v;i++) {
            list[i] = new ArrayList<>();
        }

        // 인접 리스트
        for(int i=0;i<e;i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();

            list[a].add(b);
            list[b].add(a);
        }

        for(int i=1;i<=v;i++) {
            if(order[i]==0)
                dfs(i,0);
        }
        System.out.println(pq.size());
        while(!pq.isEmpty()) {
            Node aa = pq.poll();
            System.out.println(aa.a+" "+aa.b);
        }
        sc.close();

    }
    static int dfs(int vertax,int perent) {
        order[vertax] = count++;
        int ret = order[vertax];

        for(int now:list[vertax]) {
            if(now == perent) continue;
            if(order[now]==0) {
                int low = dfs(now,vertax);
                if(low > order[vertax]) {
                    if(now>vertax)
                        pq.add(new Node(vertax,now));
                    else
                        pq.add(new Node(now,vertax));
                }

                ret = Math.min(ret, low);
            }else {
                ret = Math.min(ret, order[now]);
            }
        }

        return ret;
    }
}

'알고리즘' 카테고리의 다른 글

[백준]플로이드(11404)  (0) 2020.03.26
[백준]타임머신(11657)  (0) 2020.03.25
[백준]최단경로(1753)  (0) 2020.03.25
[백준]단절점(11266)  (0) 2020.03.25
[백준] 교수님은 기다리지 않는다(3830)  (0) 2020.03.19
Comments