File tree Expand file tree Collapse file tree 2 files changed +56
-57
lines changed Expand file tree Collapse file tree 2 files changed +56
-57
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -158,7 +158,62 @@ int main() {
158158
159159## 其他语言版本
160160
161- ### Java
161+ ### Java
162+
163+ ``` Java
164+
165+ import java.util.* ;
166+
167+ public class Main {
168+ public static void main (String [] args ) {
169+ int N , M ;
170+ Scanner scanner = new Scanner (System . in);
171+ N = scanner. nextInt();
172+ M = scanner. nextInt();
173+ DisJoint disJoint = new DisJoint (N + 1 );
174+ for (int i = 0 ; i < M ; ++ i) {
175+ disJoint. join(scanner. nextInt(), scanner. nextInt());
176+ }
177+ if (disJoint. isSame(scanner. nextInt(), scanner. nextInt())) {
178+ System . out. println(" 1" );
179+ } else {
180+ System . out. println(" 0" );
181+ }
182+ }
183+
184+ }
185+
186+ // 并查集模板
187+ class DisJoint {
188+ private int [] father;
189+
190+ public DisJoint (int N ) {
191+ father = new int [N ];
192+ for (int i = 0 ; i < N ; ++ i){
193+ father[i] = i;
194+ }
195+ }
196+
197+ public int find (int n ) {
198+ return n == father[n] ? n : (father[n] = find(father[n]));
199+ }
200+
201+ public void join (int n , int m ) {
202+ n = find(n);
203+ m = find(m);
204+ if (n == m) return ;
205+ father[m] = n;
206+ }
207+
208+ public boolean isSame (int n , int m ){
209+ n = find(n);
210+ m = find(m);
211+ return n == m;
212+ }
213+
214+ }
215+
216+ ```
162217
163218### Python
164219
You can’t perform that action at this time.
0 commit comments