Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27d707e

Browse files
authored
Merge pull request #339 from yeongleej/main
[25μ£Όμ°¨] μ΄μ§€μ˜
2 parents bb0a1ae + b069acb commit 27d707e

File tree

6 files changed

+571
-0
lines changed

6 files changed

+571
-0
lines changed

β€ŽBOJ/10001-15000번/JY_12784.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.io.*;
2+
import java.util.*;
3+
public class JY_12784 {
4+
5+
static int N, M;
6+
static List<Node>[] g;
7+
static boolean[] visited;
8+
static class Node {
9+
int num, cost;
10+
public Node(int num, int cost) {
11+
this.num = num;
12+
this.cost = cost;
13+
}
14+
@Override
15+
public String toString() {
16+
return "Node [num=" + num + ", cost=" + cost + "]";
17+
}
18+
19+
20+
}
21+
22+
public static void main(String[] args) throws IOException {
23+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
26+
int T = Integer.parseInt(st.nextToken());
27+
StringBuilder sb = new StringBuilder();
28+
for(int t=0; t<T; t++) {
29+
st = new StringTokenizer(br.readLine());
30+
N = Integer.parseInt(st.nextToken());
31+
M = Integer.parseInt(st.nextToken());
32+
33+
g = new ArrayList[N+1];
34+
for(int i=1; i<N+1; i++) {
35+
g[i] = new ArrayList<>();
36+
}
37+
38+
for(int i=0; i<M; i++) {
39+
st = new StringTokenizer(br.readLine());
40+
int a = Integer.parseInt(st.nextToken());
41+
int b = Integer.parseInt(st.nextToken());
42+
int c = Integer.parseInt(st.nextToken());
43+
g[a].add(new Node(b, c));
44+
g[b].add(new Node(a, c));
45+
}
46+
47+
visited = new boolean[N+1];
48+
visited[1] = true;
49+
int ans = dfs(1);
50+
sb.append(ans+"\n");
51+
}
52+
53+
System.out.println(sb.toString());
54+
}
55+
public static int dfs(int now) {
56+
visited[now] = true;
57+
58+
// λ¦¬ν”„λ…Έλ“œλΌλ©΄ μžμ‹ μ˜ λΉ„μš© λ°˜ν™˜
59+
if(now !=1 && g[now].size() == 1) return g[now].get(0).cost;
60+
61+
// λ¦¬ν”„λ…Έλ“œ μ•„λ‹˜ => μžμ‹ 순회
62+
int minCost = 0;
63+
for(Node next: g[now]) {
64+
if(visited[next.num]) continue;
65+
minCost += Math.min(next.cost, dfs(next.num));
66+
}
67+
68+
return minCost;
69+
}
70+
71+
}

β€ŽBOJ/30000-35000번/JY_32070.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class JY_32070 {
5+
static int N;
6+
// cnt[i] 그룹의 곡의 개수 == 그룹의 크기
7+
// rank[i] 트리의 깊이
8+
// toParis[] ν•œ κ·Έλ£Ήμ—μ„œ 상단에 μœ„μΉ˜ν•œ 곡의 개수
9+
static int[] parent, cnt, topPairs;
10+
static boolean[] solved;
11+
static List<Integer>[] ball;
12+
13+
static int find(int u) {
14+
if (parent[u] == u) return u;
15+
return parent[u] = find(parent[u]);
16+
}
17+
18+
static void union(int a, int b) {
19+
int pa = find(a);
20+
int pb = find(b);
21+
22+
if(pa == pb) return;
23+
if(pa < pb) {
24+
parent[pb] = pa;
25+
cnt[pa] += cnt[pb];
26+
} else {
27+
parent[pa] = pb;
28+
cnt[pb] += cnt[pa];
29+
}
30+
31+
}
32+
33+
// ν•΄λ‹Ή μ§‘ν•©μ˜ 크기 λ°˜ν™˜
34+
static int getCnt(int u) {
35+
return cnt[find(u)];
36+
}
37+
38+
public static void main(String[] args) throws IOException {
39+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
40+
N = Integer.parseInt(br.readLine());
41+
42+
ball = new ArrayList[N + 1];
43+
for (int i = 1; i <= N; i++) {
44+
ball[i] = new ArrayList<>();
45+
}
46+
47+
parent = new int[N + 1];
48+
// rank = new int[N + 1];
49+
cnt = new int[N + 1];
50+
topPairs = new int[N + 1];
51+
solved = new boolean[N + 1];
52+
53+
// μ΄ˆκΈ°ν™” == 각 μƒμžκ°€ κ·Έλ£Ήμž₯
54+
for (int i = 1; i <= N; i++) {
55+
parent[i] = i;
56+
cnt[i] = 1;
57+
}
58+
59+
for (int i = 1; i <= N; i++) {
60+
StringTokenizer st = new StringTokenizer(br.readLine());
61+
int a = Integer.parseInt(st.nextToken());
62+
int b = Integer.parseInt(st.nextToken());
63+
64+
// 색깔이 a인 곡의 μ ˆλŒ€μ μΈ μƒμž μœ„μΉ˜
65+
ball[a].add(i * 2);
66+
// 색깔이 b인 곡의 μ ˆλŒ€μ μΈ μƒμž μœ„μΉ˜
67+
ball[b].add(i * 2 + 1);
68+
}
69+
70+
System.out.println(Arrays.toString(ball));
71+
72+
// 같은 색 곡이 μžˆλŠ” μƒμžλ“€μ„ 병합
73+
for (int i = 1; i <= N; i++) {
74+
union(ball[i].get(0) / 2, ball[i].get(1) / 2);
75+
}
76+
77+
System.out.println(">> parent: "+Arrays.toString(parent));
78+
System.out.println(">> cnt: "+Arrays.toString(cnt));
79+
// cnt[i] : i번째 μƒμžκ°€ μ†ν•œ μ‚¬μ΄ν΄μ˜ μƒμžμ˜ 개수
80+
81+
// 루트 λ…Έλ“œμ˜ topPairs 개수 증가
82+
// 짝수 : μƒμžμ˜ 상단에 있음
83+
// ν™€μˆ˜ : μƒμžμ˜ ν•˜λ‹¨μ— 있음
84+
for (int i = 1; i <= N; i++) {
85+
// 색깔이 i인 곡이 λͺ¨λ‘ 각 μƒμžμ˜ 상단에 있음
86+
if (ball[i].get(0) % 2 == 0 && ball[i].get(1) % 2 == 0) {
87+
int rootIdx = find(ball[i].get(0) / 2);
88+
System.out.println("i:"+i+" root:"+rootIdx);
89+
topPairs[rootIdx]++;
90+
}
91+
}
92+
93+
System.out.println("top: "+Arrays.toString(topPairs));
94+
95+
// μƒμž 그룹이 2개 이상이면 λΆˆκ°€λŠ₯ (-1 좜λ ₯)
96+
for (int i = 1; i <= N; i++) {
97+
int rootIdx = find(ball[i].get(0) / 2);
98+
if (topPairs[rootIdx] >= 2) {
99+
System.out.println("-1");
100+
return;
101+
}
102+
}
103+
104+
int ans = 0;
105+
106+
// μ΅œμ†Œ 이동 횟수 계산
107+
for (int i = 1; i <= N; i++) {
108+
int rootIdx = find(ball[i].get(0) / 2);
109+
110+
if (solved[rootIdx]) continue;
111+
112+
solved[rootIdx] = true;
113+
int x = cnt[rootIdx];
114+
115+
if (x >= 2) ans += x + 1;
116+
}
117+
118+
System.out.println(ans);
119+
}
120+
}

β€ŽBOJ/5001-10000번/JY_9944.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import java.util.*;
2+
import java.io.*;
3+
public class JY_9944 {
4+
5+
static final int INF = 1000001;
6+
static int N, M;
7+
static char[][] g;
8+
// 상 ν•˜ 쒌 우
9+
static int[] dx = {-1, 1, 0, 0};
10+
static int[] dy = {0, 0, -1, 1};
11+
static boolean[][] visited;
12+
static int eCnt = 0;
13+
static int ans;
14+
15+
public static void main(String[] args) throws IOException {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st;
18+
String line;
19+
20+
int t = 1;
21+
while((line = br.readLine()) != null && !line.isEmpty()) {
22+
st = new StringTokenizer(line);
23+
24+
N = Integer.parseInt(st.nextToken());
25+
M = Integer.parseInt(st.nextToken());
26+
27+
g = new char[N][M];
28+
eCnt = 0;
29+
for(int i=0; i<N; i++) {
30+
String str = br.readLine();
31+
for(int j=0; j<M; j++) {
32+
g[i][j] = str.charAt(j);
33+
if(g[i][j] == '.') eCnt++;
34+
}
35+
}
36+
37+
ans = INF;
38+
39+
// μ‹œμž‘μ 
40+
for(int i=0; i<N; i++) {
41+
for(int j=0; j<M; j++) {
42+
if(g[i][j] == '*') continue;
43+
visited = new boolean[N][M];
44+
for(int d=0; d<4; d++) {
45+
int nx = i + dx[d];
46+
int ny = j + dy[d];
47+
// dλ°©ν–₯으둜 이동 κ°€λŠ₯ν•˜λ©΄ 단계 1λΆ€ν„° μ‹œμž‘
48+
if(canGo(nx, ny)) {
49+
dfs(i, j, d, 1, 1);
50+
}
51+
// λΆˆκ°€λŠ₯ν•˜λ©΄ 단계 2λΆ€ν„° μ‹œμž‘
52+
else {
53+
dfs(i, j, d, 1, 0);
54+
}
55+
56+
}
57+
}
58+
}
59+
System.out.print("Case "+t+": ");
60+
if(ans == INF) System.out.println(-1);
61+
else System.out.println(ans);
62+
t++;
63+
}
64+
65+
66+
}
67+
public static void printG() {
68+
for(int i=0; i<N; i++) {
69+
System.out.println(Arrays.toString(g[i]));
70+
}
71+
System.out.println();
72+
}
73+
public static boolean inRange(int x, int y) {
74+
return x>=0 && x<N && y>=0 && y<M;
75+
}
76+
public static boolean canGo(int x, int y) {
77+
return inRange(x, y) && !visited[x][y] && g[x][y] != '*';
78+
}
79+
public static void dfs(int x, int y, int dir, int mCnt, int cnt) {
80+
// λͺ¨λ“  μΉΈ 탐색함
81+
if(mCnt == eCnt) {
82+
ans = Math.min(ans, cnt);
83+
return;
84+
}
85+
// ν˜„μž¬ 이동 경둜의 μˆ˜κ°€ μ €μž₯된 정닡보닀 크닀면 μ§„ν–‰X
86+
if(cnt >= ans || cnt >= INF) return;
87+
88+
visited[x][y] = true;
89+
int nx = x + dx[dir];
90+
int ny = y + dy[dir];
91+
92+
// ν˜„μž¬ λ°©ν–₯으둜 갈 수 있음
93+
if(canGo(nx, ny)) {
94+
dfs(nx, ny, dir, mCnt+1, cnt);
95+
}
96+
// 갈 수 μ—†μŒ -> λ°©ν–” μ „ν™˜ ν•„μš”(단계 증가)
97+
else {
98+
boolean isMove = false;
99+
for(int i=0; i<4; i++) {
100+
if(i == dir) continue;
101+
nx = x + dx[i];
102+
ny = y + dy[i];
103+
if(!canGo(nx, ny)) continue;
104+
105+
isMove = true;
106+
dfs(nx, ny, i, mCnt+1, cnt+1);
107+
}
108+
109+
}
110+
visited[x][y] = false;
111+
}
112+
113+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /