-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from seomiii/master
김미서 / 10월 4주차 / 월
- Loading branch information
Showing
4 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
// 무게가 서로 다른 n개의 물건 , 1~n | ||
// 각 물건에 대해서 그 물건과의 비교 결과를 알 수 없는 물건의 개수 | ||
|
||
// 5 <= 물건의 개수 n <= 100 | ||
// 0<= 물건 쌍의 개수 m <= 2000 | ||
public class BOJ10159_저울 { | ||
// 큰 애들로 이어진 그래프, 작은 애들로 이어진 그래프 2개로 운영하기 | ||
// visited 배열은 같은 그래프 사용하기 | ||
// 이어진 개수를 구해서 최종 개수에서 빼주기!! | ||
static ArrayList<ArrayList<Integer>> biggerList, smallerList; | ||
static boolean[] visited; | ||
static int N,M; | ||
static int cnt; | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
N = Integer.parseInt(br.readLine()); | ||
M = Integer.parseInt(br.readLine()); | ||
|
||
biggerList = new ArrayList<>(); | ||
smallerList = new ArrayList<>(); | ||
|
||
for (int i=0; i<=N; i++){ | ||
biggerList.add(new ArrayList<>()); | ||
smallerList.add(new ArrayList<>()); | ||
} | ||
|
||
for (int i=0; i<M; i++){ | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int big = Integer.parseInt(st.nextToken()); | ||
int small = Integer.parseInt(st.nextToken()); | ||
// System.out.println(big+" "+small); | ||
biggerList.get(big).add(small); | ||
smallerList.get(small).add(big); | ||
} | ||
|
||
for (int i=1; i<=N; i++){ // 각각 정점 | ||
visited = new boolean[N+1]; | ||
visited[i] = true; | ||
cnt = 0; | ||
dfs(i, biggerList); | ||
dfs(i, smallerList); | ||
System.out.println(N - cnt -1); | ||
} | ||
} | ||
|
||
private static void dfs(int cur, ArrayList<ArrayList<Integer>> list){ | ||
ArrayList<Integer> links = list.get(cur); | ||
int len = links.size(); | ||
|
||
for (int i=0; i<len; i++){ | ||
int link = links.get(i); | ||
if (!visited[link]){ // 방문하지 않았으면 | ||
visited[link] = true; | ||
cnt++; | ||
dfs(link, list); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
public class BOJ12919_A와B2 { | ||
static String S,T; | ||
static boolean result; | ||
static StringBuilder sb; | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
sb = new StringBuilder(); | ||
S = br.readLine(); | ||
T = br.readLine(); | ||
result = false; | ||
solve(T); | ||
if (result){ | ||
System.out.println(1); | ||
}else{ | ||
System.out.println(0); | ||
} | ||
} | ||
|
||
private static void solve(String t){ | ||
// System.out.println(t); | ||
if (t.length() == S.length()){ | ||
if (t.equals(S)){ | ||
result=true; | ||
} | ||
return; | ||
} | ||
|
||
int len = t.length(); | ||
// A를 붙였다면 | ||
if ( t.charAt(len-1) == 'A'){ | ||
solve(t.substring(0, len-1)); | ||
} | ||
|
||
// B를 붙였다면 | ||
if ( t.charAt(0) == 'B' ){ | ||
sb.append(t); | ||
String temp = ""; | ||
temp = sb.reverse().toString(); | ||
temp = temp.substring(0,len-1); | ||
solve(temp); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import java.io.*; | ||
import java.sql.SQLOutput; | ||
import java.util.*; | ||
public class BOJ17297_MessiGimossi { | ||
static int M, messiLen,index,cnt; | ||
static String result,temp; | ||
static ArrayList<Integer> list = new ArrayList<>(); | ||
public static void main(String[] args) throws Exception { | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
M = Integer.parseInt(br.readLine()) - 1; // 인덱스 | ||
messiLen = 0; | ||
temp = "Messi Gimossi"; | ||
list.add(5); | ||
list.add(13); | ||
|
||
index = 0; | ||
|
||
if (M <= 13){ | ||
result = String.valueOf(temp.charAt(M)); | ||
if (result.equals(" ")){ | ||
result = "Messi Messi Gimossi"; | ||
} | ||
} else { | ||
getMessiLen(); | ||
// System.out.println(list); | ||
messi(list.size()-1); | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
private static void messi(int index){ | ||
// 0 ~ memo[0]-1 | ||
// memo[0] | ||
// memo[0]+1 ~ memo[0]+memo[1] | ||
//System.out.println(Arrays.toString(memo)+" dddddd"+ messiLen); | ||
|
||
messiLen = list.get(index); | ||
// System.out.println(messiLen+" messilen "+ index+ " "+M); | ||
|
||
if (messiLen <= 13){ | ||
// System.out.println(M); | ||
result = String.valueOf(temp.charAt(M)); | ||
if (result.equals(" ")){ | ||
result = "Messi Messi Gimossi"; | ||
} | ||
return; | ||
} | ||
|
||
int left = list.get(index - 1); | ||
int right = list.get(index - 2); | ||
// System.out.println(left +" left "+ right + " right "+ messiLen); | ||
|
||
if (M == left){ // 가운데 | ||
result = "Messi Messi Gimossi"; | ||
return; | ||
} | ||
|
||
if ( M <= left - 1 ){ // 왼쪽이면 | ||
messi(index - 1); | ||
} | ||
// System.out.println(left+1 +" "+ (left+right)+" " + M); | ||
if (M>= left + 1 && M <= left+right ){ // 오른쪽이면 | ||
M -= ( left + 1 ); | ||
// System.out.println(M); | ||
messi(index - 2); | ||
} | ||
} | ||
|
||
private static void getMessiLen(){ | ||
while(list.get(list.size()-1) <= M){ | ||
int first = list.get(list.size()-1); | ||
int second = list.get(list.size()-2); | ||
list.add(first + second + 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
/** | ||
* 얼리 아답터가 아닌 사람들은 자신의 모든 친구들이 얼리 아답터일 때만 이 아이디어를 받아들인다. | ||
* 라는 구문에서 dp를 떠올릴 수 있는 훈련이 필요하다고 생각됩니다! | ||
*/ | ||
|
||
public class BOJ2533_사회망서비스 { | ||
static int N; | ||
static int[][] dp; | ||
static boolean[] visited; | ||
static List<ArrayList> list = new ArrayList<>(); | ||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
N = Integer.parseInt(br.readLine()); | ||
|
||
for (int i=0; i<N+1; i++){ | ||
list.add(new ArrayList<>()); | ||
} | ||
|
||
for (int i=0; i<N-1; i++){ | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int from = Integer.parseInt(st.nextToken()); | ||
int to = Integer.parseInt(st.nextToken()); | ||
|
||
list.get(from).add(to); | ||
list.get(to).add(from); | ||
} | ||
|
||
dp= new int[N+1][2]; | ||
visited = new boolean[N+1]; | ||
visited[1] = true; | ||
dfs(1); | ||
System.out.println(Math.min(dp[1][0], dp[1][1])); | ||
} | ||
|
||
private static void dfs(int cur){ | ||
dp[cur][0] = 0; // 얼리어답터가 아님 | ||
dp[cur][1] = 1; // 얼리어답터가 맞음 | ||
|
||
ArrayList friends = list.get(cur); | ||
for (int i=0; i<friends.size(); i++){ | ||
int friend = (Integer) friends.get(i); | ||
if (!visited[friend]){ | ||
visited[friend] = true; | ||
dfs(friend); | ||
dp[cur][0] += dp[friend][1]; | ||
dp[cur][1] += Math.min(dp[friend][0] , dp[friend][1]); | ||
} | ||
} | ||
} | ||
} |