Skip to content

Commit e2d3adc

Browse files
authored
[20250211] BOJ / 플래5 / 선분 그룹 / 권혁준
1 parent b39a0ea commit e2d3adc

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Point{
7+
int x, y;
8+
Point(int x, int y){
9+
this.x = x;
10+
this.y = y;
11+
}
12+
13+
public int ccw(Point a, Point b) {
14+
int x1 = this.x, y1 = this.y;
15+
int x2 = a.x, y2 = a.y;
16+
int x3 = b.x, y3 = b.y;
17+
int res = (x1*y2 + x2*y3 + x3*y1) - (x1*y3 + x2*y1 + x3*y2);
18+
return res > 0 ? 1 : (res < 0 ? -1 : 0);
19+
}
20+
21+
}
22+
23+
class Line{
24+
Point a, b;
25+
Line(int x1, int y1, int x2, int y2){
26+
if(x1 < x2 || (x1 == x2 && y1 < y2)) {
27+
a = new Point(x1,y1);
28+
b = new Point(x2,y2);
29+
}
30+
else {
31+
a = new Point(x2,y2);
32+
b = new Point(x1,y1);
33+
}
34+
}
35+
36+
public boolean intersect(Line o) {
37+
int cond1 = this.a.ccw(this.b, o.a);
38+
int cond2 = this.b.ccw(this.a, o.b);
39+
int cond3 = o.a.ccw(o.b, this.a);
40+
int cond4 = o.b.ccw(o.a, this.b);
41+
42+
43+
if(cond1*cond2 < 0 || cond3*cond4 < 0) return false;
44+
if(cond1*cond2 > 0 && cond3*cond4 > 0) return true;
45+
if(cond1 == 0) {
46+
if(this.between(o.a)) return true;
47+
}
48+
if(cond2 == 0) {
49+
if(this.between(o.b)) return true;
50+
}
51+
if(cond3 == 0) {
52+
if(o.between(this.a)) return true;
53+
}
54+
if(cond4 == 0) {
55+
if(o.between(this.b)) return true;
56+
}
57+
return false;
58+
59+
}
60+
61+
public boolean between(Point p) {
62+
return this.a.x<=p.x&&p.x<=this.b.x && (this.a.y<=p.y&&p.y<=this.b.y || this.b.y<=p.y&&p.y<=this.a.y);
63+
}
64+
65+
}
66+
67+
class Main {
68+
69+
// IO field
70+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
71+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
72+
static StringTokenizer st;
73+
74+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
75+
static int nextInt() {return Integer.parseInt(st.nextToken());}
76+
static long nextLong() {return Long.parseLong(st.nextToken());}
77+
static void bwEnd() throws Exception {bw.flush();bw.close();}
78+
79+
// Additional field
80+
static Line[] arr;
81+
static int N;
82+
static int[] root, cnt;
83+
84+
public static void main(String[] args) throws Exception {
85+
86+
ready();
87+
solve();
88+
89+
bwEnd();
90+
}
91+
92+
static void ready() throws Exception{
93+
94+
N = Integer.parseInt(br.readLine());
95+
arr = new Line[N];
96+
root = new int[N];
97+
cnt = new int[N];
98+
for(int i=0;i<N;i++) {
99+
root[i] = i;
100+
cnt[i] = 1;
101+
}
102+
103+
for(int i=0;i<N;i++) {
104+
nextLine();
105+
int x1 = nextInt(), y1 = nextInt(), x2 = nextInt(), y2 = nextInt();
106+
arr[i] = new Line(x1,y1,x2,y2);
107+
}
108+
109+
}
110+
111+
static void solve() throws Exception{
112+
113+
for(int i=1;i<N;i++) {
114+
for(int j=0;j<i;j++) {
115+
if(arr[i].intersect(arr[j])) {
116+
int x = f(i), y = f(j);
117+
if(x != y) {
118+
cnt[y] += cnt[x];
119+
root[x] = y;
120+
}
121+
}
122+
}
123+
}
124+
125+
int ans = 0, max = 0;
126+
boolean[] vis = new boolean[N];
127+
for(int i=0;i<N;i++) {
128+
int x = f(i);
129+
if(vis[x]) continue;
130+
vis[x] = true;
131+
ans++;
132+
133+
if(cnt[x] > max) max = cnt[x];
134+
}
135+
bw.write(ans+"\n"+max);
136+
137+
}
138+
139+
static int f(int x) {return x==root[x] ? x : (root[x]=f(root[x]));}
140+
141+
}
142+
143+
```

0 commit comments

Comments
 (0)