Hard deck/리포트

044 : Cocktail

서버관리자 페페 2022. 8. 3. 15:28
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    // setup
    static ArrayList<cNode>[] F;
    static long LCM;
    static boolean[] visited;
    static long[] D;

    public static void main(String[] args) throws Exception {

        // ISC - L1
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        // L2~
        F = new ArrayList[N];
        visited = new boolean[N];
        D = new long[N];
        LCM = 1;
        for (int i = 0; i < N; i++) {
            F[i] = new ArrayList<cNode>();
        }
        for (int i = 0; i < N; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int p = sc.nextInt();
            int q = sc.nextInt();
            F[a].add(new cNode(b, p, q));
            F[b].add(new cNode(a, q, p));
            LCM *= (p * q / GCD(p, q));
        }


        // Operating DFS + plate
        D[0] = LCM;
        DFS(0);
        long mgcd = D[0];
        for (int i = 1; i < N; i++) {
            mgcd = GCD(mgcd, D[i]);
        }

        // OEC
        for (int i = 0; i < N; i++) {
            System.out.print(D[i] / mgcd + " ");
        }
    }

    // External Module : DFS
    public static void DFS(int Node) {
        visited[Node] = true;
        for (cNode i : F[Node]) {
            int next = i.getB();
            if (!visited[next]) {
                D[next] = D[Node] * i.getQ() / i.getP();
                DFS(next);
            }
        }
    }
    // External Module : GCD
    public static long GCD(long a, long b) {
        if (b == 0)
            return a;
        else
            return GCD(b, a % b);
    }
}

// External Class
class cNode {
    int b;
    int p;
    int q;

    public cNode(int b, int p, int q) {
        super();
        this.b = b;
        this.p = p;
        this.q = q;
    }

    public int getB() {
        return b;
    }
    public int getP() {
        return p;
    }
    public int getQ() {
        return q;
    }
}

'Hard deck > 리포트' 카테고리의 다른 글

046 : 특정 거리의 도시 찾기  (0) 2022.08.08
045 : Extended Euclidean algorithm  (3) 2022.08.03
041 : Euler phi  (2) 2022.08.03
040 : 제곱이 아닌 수 찾기  (3) 2022.07.30
037 : 소수 구하기  (0) 2022.07.30