Skip to content

Commit 6a163ef

Browse files
committed
BOJ_15270 : 친구 팰린드롬
1 parent cdc5985 commit 6a163ef

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
private static int N, M;
7+
private static boolean[][] friend;
8+
private static int[][] relation;
9+
private static boolean[] visited;
10+
private static int answer = 0;
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
N = Integer.parseInt(st.nextToken());
16+
M = Integer.parseInt(st.nextToken());
17+
friend = new boolean[N+1][N+1];
18+
relation = new int[M][2];
19+
visited = new boolean[N+1];
20+
for(int i=0; i<M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int u = Integer.parseInt(st.nextToken());
23+
int v = Integer.parseInt(st.nextToken());
24+
friend[u][v] = true;
25+
friend[v][u] = true;
26+
relation[i][0] = u;
27+
relation[i][1] = v;
28+
}
29+
dfs(0, 0);
30+
System.out.println((answer == N) ? answer : answer + 1);
31+
}
32+
33+
public static void dfs(int start, int count) {
34+
answer = Math.max(answer, count);
35+
36+
for(int i=start; i<M; i++) {
37+
int friend1 = relation[i][0];
38+
int friend2 = relation[i][1];
39+
if(!visited[friend1] && !visited[friend2]) {
40+
visited[friend1] = true;
41+
visited[friend2] = true;
42+
dfs(i, count+2);
43+
visited[friend1] = false;
44+
visited[friend2] = false;
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)