Skip to content

Commit 1c50a06

Browse files
authored
Merge pull request #785 from AlgorithmWithGod/khj20006
[20250831] BOJ / D5 / 그래프와 쿼리 / 권혁준
2 parents 996cba6 + 195e83e commit 1c50a06

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
class DisjointSet {
51+
int size;
52+
int[] root, rank;
53+
Stack<int[]> works;
54+
55+
DisjointSet(int size) {
56+
this.size = size;
57+
root = new int[size+1];
58+
rank = new int[size+1];
59+
for(int i=1;i<=size;i++) {
60+
root[i] = i;
61+
rank[i] = 1;
62+
}
63+
works = new Stack<>();
64+
}
65+
66+
int find(int x) { return x == root[x] ? x : find(root[x]); }
67+
68+
void union(int a, int b) {
69+
int x = find(a), y = find(b);
70+
if(x == y) {
71+
works.push(new int[]{-1,-1,-1});
72+
return;
73+
}
74+
if(rank[x] < rank[y]) {
75+
rank[y] += rank[x];
76+
root[x] = y;
77+
works.add(new int[]{x, y, rank[x]});
78+
}
79+
else {
80+
rank[x] += rank[y];
81+
root[y] = x;
82+
works.add(new int[]{y, x, rank[y]});
83+
}
84+
}
85+
86+
void rollback() {
87+
if (!works.isEmpty()) {
88+
int[] last = works.pop();
89+
int x = last[0], y = last[1], r = last[2];
90+
if(x == -1) return;
91+
rank[y] -= r;
92+
root[x] = x;
93+
}
94+
}
95+
96+
}
97+
98+
public class Main {
99+
100+
static IOController io;
101+
102+
//
103+
104+
static int N, M;
105+
static List<int[]>[] lists;
106+
static DisjointSet ds;
107+
static List<int[]>[] needAnswer;
108+
109+
static void update(int s, int e, int l, int r, int n, int a, int b) {
110+
if(l>r || l>e || r<s) return;
111+
if(l<=s && e<=r) {
112+
lists[n].add(new int[]{a,b});
113+
return;
114+
}
115+
int m = (s+e)>>1;
116+
update(s,m,l,r,n*2,a,b);
117+
update(m+1,e,l,r,n*2+1,a,b);
118+
}
119+
120+
static void clear(int s, int e, int n) throws Exception {
121+
for(int[] edge : lists[n]) {
122+
int a = edge[0], b = edge[1];
123+
ds.union(a,b);
124+
}
125+
if(s == e) {
126+
for(int[] info : needAnswer[s]) {
127+
int a = info[0], b = info[1];
128+
if(ds.find(a) == ds.find(b)) io.write("1\n");
129+
else io.write("0\n");
130+
}
131+
for(int i=0;i<lists[n].size();i++) ds.rollback();
132+
return;
133+
}
134+
int m = (s+e)>>1;
135+
clear(s,m,n*2);
136+
clear(m+1,e,n*2+1);
137+
for(int i=0;i<lists[n].size();i++) ds.rollback();
138+
}
139+
140+
public static void main(String[] args) throws Exception {
141+
142+
io = new IOController();
143+
144+
N = io.nextInt();
145+
M = io.nextInt();
146+
lists = new List[M*4];
147+
for(int i=0;i<M*4;i++) lists[i] = new ArrayList<>();
148+
ds = new DisjointSet(N);
149+
needAnswer = new List[M];
150+
for(int i=0;i<M;i++) needAnswer[i] = new ArrayList<>();
151+
Map<String, Integer> map = new TreeMap<>();
152+
for(int i=0;i<M;i++) {
153+
int op = io.nextInt();
154+
int a = io.nextInt();
155+
int b = io.nextInt();
156+
if(a > b) {
157+
int t = a;
158+
a = b;
159+
b = t;
160+
}
161+
if(op == 1) map.put(a+","+b, i);
162+
else if(op == 2) {
163+
update(0,M-1,map.get(a+","+b),i,1,a,b);
164+
map.remove(a+","+b);
165+
}
166+
else {
167+
needAnswer[i].add(new int[]{a,b});
168+
}
169+
}
170+
for(String key : map.keySet()) {
171+
String[] words = key.split(",");
172+
int a = Integer.parseInt(words[0]);
173+
int b = Integer.parseInt(words[1]);
174+
update(0,M-1,map.get(key),M,1,a,b);
175+
}
176+
177+
clear(0,M-1,1);
178+
179+
io.close();
180+
181+
}
182+
183+
}
184+
```

0 commit comments

Comments
 (0)