Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26-yuyu0830 #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
| 19์ฐจ์‹œ | 2024.07.12 | ์žฌ๊ท€ | [์šฐ์ˆ˜๋งˆ์„](https://www.acmicpc.net/problem/1949) | - |
| 20์ฐจ์‹œ | 2024.07.22 | LIS | [๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 5](https://www.acmicpc.net/problem/14003) | - |
| 21์ฐจ์‹œ | 2024.07.23 | ์ˆ˜ํ•™ | [1์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ](https://www.acmicpc.net/problem/9527) | - |
| 26์ฐจ์‹œ | 2024.08.20 | ๊ธฐํ•˜ | [์„ ๋ถ„ ๊ทธ๋ฃน](https://www.acmicpc.net/problem/2162) | - |
---
123 changes: 123 additions & 0 deletions yuyu0830/์ˆ˜ํ•™/2162.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Point {
int x, y;
void set(int X, int Y) {
x = X;
y = Y;
}

bool operator== (const Point t) {
return x == t.x && y == t.y;
}
};

struct Line {
Point a, b;

void set(int x1, int y1, int x2, int y2) {
bool e;

// x๊ฐ€ ์ž‘์€ ์ˆœ, ๊ฐ™๋‹ค๋ฉด y๊ฐ€ ํฐ ์ˆœ
// ์‚ฌ๋ถ„๋ฉด์—์„œ ์™ผ์ชฝ ์œ„ ์ ์„ ์šฐ์„ 
if (x1 != x2) e = x1 < x2;
else e = y1 > y2;

a.x = e ? x1 : x2;
a.y = e ? y1 : y2;
b.x = e ? x2 : x1;
b.y = e ? y2 : y1;
}
};


// CCW
Line arr[3001];
bool visited[3001] = {0, };

int CCW(Point a, Point b, Point c) {
int t1 = a.x * b.y + b.x * c.y + c.x * a.y;
int t2 = a.y * b.x + b.y * c.x + c.y * a.x;

int s = t1 - t2;

if (s > 0) return 1;
else if (!s) return 0;
else return -1;
}

bool isIntersect(Line l1, Line l2) {
Point p1 = l1.a;
Point p2 = l1.b;
Point p3 = l2.a;
Point p4 = l2.b;

if (p1 == p3 || p1 == p4 || p2 == p3 || p2 == p4) return true;

int t1 = CCW(p1, p2, p3) * CCW(p1, p2, p4); // l1 ๊ธฐ์ค€
int t2 = CCW(p3, p4, p1) * CCW(p3, p4, p2); // l2 ๊ธฐ์ค€

// ๋‘ ์ง์„ ์ด ์ผ์ง์„  ์ƒ์— ์กด์žฌํ•˜๋ฉด
// ๊ธฐ์šธ๊ธฐ๊ฐ€ 0 ํ˜น์€ ๋ฌดํ•œ์ธ ๊ฒฝ์šฐ || ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ
if (!t1 && !t2) {
bool x = (p1.x == p3.x && p2.x == p4.x) || (p1.x <= p4.x != p2.x <= p3.x);
bool y = (p1.y == p3.y && p2.y == p4.y) || (p1.y >= p4.y != p2.y >= p3.y);

return x && y;
}

return t1 <= 0 && t2 <= 0;
}

// union find
int parent[3001] = {0, };
int count[3001] = {0, };

int find(int v) {
if (parent[v] == v) return v;
return parent[v] = find(parent[v]);
}

void merge(int x, int y) {
parent[find(min(x, y))] = find(max(x, y));
}

int main() {
int n; cin >> n;

for (int i = 0; i < n; i++) {
int a, b, c, d; cin >> a >> b >> c >> d;
arr[i].set(a, b, c, d);

parent[i] = i;
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// i๋ฒˆ์งธ Line๋Š” 0 ~ i - 1๋ฒˆ์งธ Line๊นŒ์ง€ ๊ฒน์น˜๋Š”์ง€ ํ™•์ธ
// ๊ฒน์น˜๋ฉด Union ํ•ด๋ฒ„๋ฆฌ๊ธฐ
if (isIntersect(arr[i], arr[j]))
merge(i, j);
}
}

// ๊ฒน์นจ ์—ฌ๋ถ€๊ฐ€ ๋’ค์— ๊ฒฐ์ •๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ๋ชฐ์•„์„œ ๊ฐœ์ˆ˜ ์—ฐ์‚ฐ
for (int i = 0; i < n; i++)
::count[find(i)]++;

int cnt = 0, maxCnt = 0;

// ๊ฐœ์ˆ˜ ๊ฐฑ์‹ 
for (int i = 0; i < n; i++) {
if (::count[i]) {
cnt++;
maxCnt = max(maxCnt, ::count[i]);
}
}

printf("%d\n%d\n", cnt, maxCnt);
}