File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int [] parent;
7+
8+ static int find (int x ) {
9+ if (parent[x] == x) return x;
10+ return parent[x] = find(parent[x]);
11+ }
12+
13+ static void union (int a , int b ) {
14+ a = find(a);
15+ b = find(b);
16+ if (a != b) parent[b] = a;
17+ }
18+
19+ public static void main (String [] args ) throws Exception {
20+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
21+ int n = Integer . parseInt(br. readLine());
22+
23+ parent = new int [n + 1 ];
24+ for (int i = 1 ; i <= n; i++ ) parent[i] = i;
25+
26+ for (int i = 0 ; i < n - 2 ; i++ ) {
27+ StringTokenizer st = new StringTokenizer (br. readLine());
28+ int a = Integer . parseInt(st. nextToken());
29+ int b = Integer . parseInt(st. nextToken());
30+ union(a, b);
31+ }
32+
33+ int root = find(1 );
34+ for (int i = 2 ; i <= n; i++ ) {
35+ if (find(i) != root) {
36+ System . out. println(1 + " " + i);
37+ return ;
38+ }
39+ }
40+ }
41+ }
42+ ```
You can’t perform that action at this time.
0 commit comments