Skip to content

Commit 2a5dab5

Browse files
authored
Merge pull request #900 from AlgorithmWithGod/khj20006
[20250915] BOJ / G3 / 그래프 리뷰 유튜버 / 권혁준
2 parents 4cea76b + 3dc20df commit 2a5dab5

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
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())
24+
nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static int N;
58+
static int[] c;
59+
static List<Integer>[] graph;
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
N = io.nextInt();
66+
c = new int[N+1];
67+
graph = new List[N+1];
68+
for(int i=1;i<=N;i++) graph[i] = new ArrayList<>();
69+
70+
for(int i=1;i<N;i++) {
71+
int a = io.nextInt();
72+
int b = io.nextInt();
73+
c[a]++;
74+
c[b]++;
75+
graph[a].add(b);
76+
graph[b].add(a);
77+
}
78+
79+
for(int i=1;i<=N;i++) if(c[i] >= 3) {
80+
int a = graph[i].get(0);
81+
int b = graph[i].get(1);
82+
int c = graph[i].get(2);
83+
io.write("3\n");
84+
io.write(a + " " + b + "\n");
85+
io.write(b + " " + c + "\n");
86+
io.write(a + " " + c + "\n");
87+
io.close();
88+
return;
89+
}
90+
91+
for(int i=1;i<=N;i++) for(int j:graph[i]) {
92+
if(c[i] == 2 && c[j] == 2) {
93+
int a = graph[i].get(0);
94+
if(a == j) a = graph[i].get(1);
95+
int b = graph[j].get(0);
96+
if(b == i) b = graph[j].get(1);
97+
io.write("3\n");
98+
io.write(a + " " + b + "\n");
99+
io.write(a + " " + j + "\n");
100+
io.write(b + " " + i + "\n");
101+
io.close();
102+
return;
103+
}
104+
}
105+
106+
}
107+
108+
}
109+
```

0 commit comments

Comments
 (0)