๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

N๊ฐœ์˜ ์„ ๋ถ„๋“ค์ด 2์ฐจ์› ํ‰๋ฉด์ƒ์— ์ฃผ์–ด์ ธ ์žˆ๋‹ค. ์„ ๋ถ„์€ ์–‘ ๋์ ์˜ x, y ์ขŒํ‘œ๋กœ ํ‘œํ˜„์ด ๋œ๋‹ค.

๋‘ ์„ ๋ถ„์ด ์„œ๋กœ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ์—, ๋‘ ์„ ๋ถ„์€ ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•œ๋‹ค๊ณ  ์ •์˜ํ•˜๋ฉฐ, ๊ทธ๋ฃน์˜ ํฌ๊ธฐ๋Š” ๊ทธ ๊ทธ๋ฃน์— ์†ํ•œ ์„ ๋ถ„์˜ ๊ฐœ์ˆ˜๋กœ ์ •์˜ํ•œ๋‹ค. ๋‘ ์„ ๋ถ„์ด ๋งŒ๋‚œ๋‹ค๋Š” ๊ฒƒ์€ ์„ ๋ถ„์˜ ๋์ ์„ ์Šค์น˜๋“ฏ์ด ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•œ๋‹ค.

N๊ฐœ์˜ ์„ ๋ถ„๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์„ ๋ถ„๋“ค์€ ์ด ๋ช‡ ๊ฐœ์˜ ๊ทธ๋ฃน์œผ๋กœ ๋˜์–ด ์žˆ์„๊นŒ? ๋˜, ๊ฐ€์žฅ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ทธ๋ฃน์— ์†ํ•œ ์„ ๋ถ„์˜ ๊ฐœ์ˆ˜๋Š” ๋ช‡ ๊ฐœ์ผ๊นŒ? ์ด ๋‘ ๊ฐ€์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์ž.

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— N(1 โ‰ค N โ‰ค 3,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N+1๋ฒˆ์งธ ์ค„์—๋Š” ์–‘ ๋์ ์˜ ์ขŒํ‘œ๊ฐ€ x1, y1, x2, y2์˜ ์ˆœ์„œ๋กœ ์ฃผ์–ด์ง„๋‹ค.
  • ๊ฐ ์ขŒํ‘œ์˜ ์ ˆ๋Œ“๊ฐ’์€ 5,000์„ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์ž…๋ ฅ๋˜๋Š” ์ขŒํ‘œ ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์ด ํ•˜๋‚˜ ์žˆ๋‹ค.
# 1
3
1 1 2 3
2 1 0 0
1 0 1 1
# 2
3
-1 -1 1 1
-2 -2 2 2
0 1 -1 0

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ๊ทธ๋ฃน์˜ ์ˆ˜๋ฅผ, ๋‘˜์งธ ์ค„์— ๊ฐ€์žฅ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ทธ๋ฃน์— ์†ํ•œ ์„ ๋ถ„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
# 1
1
3
# 2
2
2


๋ฌธ์ œ ์ดํ•ด


  • ์„ ๋ถ„์˜ ๊ต์ฐจ ์—ฌ๋ถ€ ํ™•์ธ -> ์ด๊ฒƒ์œผ๋กœ ๊ทธ๋ฃนํ™” ๋ฐ ๊ทธ๋ฃน๋ณ„๋กœ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ


์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐ ์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 1์‹œ๊ฐ„ 20๋ถ„(์ฒซ ํ’€์ด ์‹œ๊ฐ„ ํฌํ•จ)

  • ์ž…๋ ฅ
    • ์„ ๋ถ„๋“ค์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ
  • ๊ณ„์‚ฐ
    • Union-Find ์ˆ˜ํ–‰
      • ์„ ๋ถ„๋“ค์˜ ๊ต์ฐจ ์—ฌ๋ถ€ ํ™•์ธ
        • ๊ต์ฐจํ•˜๋Š” ์„ ๋ถ„์€ Union ์—ฐ์‚ฐ์œผ๋กœ ๊ทธ๋ฃน ํ•ฉ์น˜๊ธฐ
    • ๊ต์ฐจ ์—ฌ๋ถ€ ํ™•์ธ
      • ccw
        • ์„ธ ์ ์ด ์ด๋ฃจ๋Š” ๋ฐฉํ–ฅ ๊ณ„์‚ฐ
      • meet
        • ๋‘ ์„ ๋ถ„์˜ ๊ต์ฐจ ์—ฌ๋ถ€ ํ™•์ธ -> ๊ต์ฐจํ•˜๋ฉด Union ์—ฐ์‚ฐ ์ˆ˜ํ–‰
    • ๊ทธ๋ฃน ๊ณ„์‚ฐ
      • ๊ทธ๋ฃน ๋‚ด ์†ํ•˜๋Š” ์„ ๋ถ„ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ(๊ฐ€์žฅ ํฐ ๊ฐ’)
      • ๊ทธ ๊ทธ๋ฃน ์ €์žฅ
  • ์ถœ๋ ฅ
    • ๊ทธ๋ฃน ๊ฐœ์ˆ˜, ๊ฐ€์žฅ ํฐ ๊ทธ๋ฃน ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅ
import java.util.*;

public class Main {

    static int[] parent;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Line[] lines = new Line[n];
        parent = new int[n];

        for (int i = 0; i < n; i++) {
            lines[i] = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
            parent[i] = i;
        }

        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (meet(lines[i], lines[j])) {
                    union(i, j);
                }
            }
        }

        Map<Integer, Integer> groupCount = new HashMap<>();
        int maxSize = 1;

        for (int i = 0; i < n; i++) {
            int groupParent = getParent(i);
            groupCount.put(groupParent, groupCount.getOrDefault(groupParent, 0) + 1);
            maxSize = Math.max(maxSize, groupCount.get(groupParent));
        }

        System.out.println(groupCount.size());
        System.out.println(maxSize);
    }

    static int getParent(int idx) {
        if (parent[idx] != idx) {
            parent[idx] = getParent(parent[idx]);
        }
        return parent[idx];
    }

    static boolean meet(Line l1, Line l2) {
        int res1 = ccw(l1, l2.x1, l2.y1) * ccw(l1, l2.x2, l2.y2);
        int res2 = ccw(l2, l1.x1, l1.y1) * ccw(l2, l1.x2, l1.y2);

        if (res1 == 0 && res2 == 0) {
            return Math.min(l1.x1, l1.x2) <= Math.max(l2.x1, l2.x2) &&
                   Math.min(l2.x1, l2.x2) <= Math.max(l1.x1, l1.x2) &&
                   Math.min(l1.y1, l1.y2) <= Math.max(l2.y1, l2.y2) &&
                   Math.min(l2.y1, l2.y2) <= Math.max(l1.y1, l1.y2);
        } else {
            return res1 <= 0 && res2 <= 0;
        }
    }

    static int ccw(Line l1, int x3, int y3) {
        int x1 = l1.x1, y1 = l1.y1, x2 = l1.x2, y2 = l1.y2;
        long result = (long) (x2 - x1) * (y3 - y1) - (long) (y2 - y1) * (x3 - x1);
        if (result == 0) return 0;
        return result > 0 ? 1 : -1;
    }

    static void union(int i, int j) {
        int rootI = getParent(i);
        int rootJ = getParent(j);
        if (rootI != rootJ) {
            parent[rootI] = rootJ;
        }
    }

    static class Line {
        int x1, y1, x2, y2;

        public Line(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€