Skip to content

[권혁준-17주차 알고리즘 스터디] #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions 권혁준_17주차/[BOJ-11085] 군사 이동.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M, S, E;
static int[][] G;
static int[] r;

static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
S = nextInt();
E = nextInt();
r = new int[N];
for(int i=0;i<N;i++) r[i] = i;
G = new int[M][];
for(int i=0;i<M;i++) G[i] = new int[] {nextInt(), nextInt(), nextInt()};

}

static void solve() throws Exception {

Arrays.sort(G, (a,b) -> b[2]-a[2]);
for(int[] e:G) {
int a = e[0], b = e[1], c = e[2];
int x = f(a), y = f(b);
if(x == y) continue;
r[x] = y;
if(f(S) == f(E)) {
bw.write(c + "\n");
return;
}
}

}

}
```
66 changes: 66 additions & 0 deletions 권혁준_17주차/[BOJ-12784] 인하니카 공화국.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static List<int[]>[] V;

public static void main(String[] args) throws Exception {

for(int T=nextInt();T-->0;solve()) ready();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
V = new List[N+1];
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
for(int i=1;i<N;i++) {
int a = nextInt(), b = nextInt(), c = nextInt();
V[a].add(new int[] {b,c});
V[b].add(new int[] {a,c});
}

}

static void solve() throws Exception {

bw.write((N == 1 ? 0 : dfs(1,0)) + "\n");

}

static int dfs(int n, int p) {
int res = 0, child = 0;
for(int[] e:V[n]) if(e[0] != p) {
int i = e[0], c = e[1];
res += Math.min(dfs(i,n), c);
child++;
}
return child>0 ? res : (int)1e7;
}

}
```
61 changes: 61 additions & 0 deletions 권혁준_17주차/[BOJ-1701] Cubeditor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static char[] B;
static int[] X;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

B = br.readLine().toCharArray();

}

static void solve() throws Exception {

int max = 0;
for(int l=0;l<B.length-1;l++) {
char[] A = new char[B.length-l];
for(int i=0;i<A.length;i++) A[i] = B[i+l];
X = new int[A.length];
for(int i=1;i<A.length;i++) {
X[i] = X[i-1];
while(X[i]>0 && A[X[i]] != A[i]) X[i] = X[X[i]-1];
if(A[X[i]] == A[i]) X[i]++;
max = Math.max(X[i], max);
}
}
bw.write(max + "\n");

}

}
```
105 changes: 105 additions & 0 deletions 권혁준_17주차/[BOJ-2842] 집배원 한상덕.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
```java
import java.util.*;
import java.io.*;

public class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static final int[] dx = {-1,-1,-1,0,0,1,1,1};
static final int[] dy = {-1,0,1,-1,1,-1,0,1};

static int N;
static char[][] A;
static int[][] H;
static int[] L;
static int sx, sy, cnt = 0;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
A = new char[N][];
H = new int[N][N];
L = new int[N*N];
for(int i=0;i<N;i++) {
A[i] = br.readLine().toCharArray();
for(int j=0;j<N;j++) {
if(A[i][j] == 'P') {
sx = i;
sy = j;
}
if(A[i][j] == 'K') cnt++;
}
}
for(int i=0;i<N;i++) for(int j=0;j<N;j++) L[i*N+j] = H[i][j] = nextInt();

}

static void solve() throws Exception {

Arrays.sort(L);
int ans = Integer.MAX_VALUE;
for(int min:L) if(min <= H[sx][sy]) {
int s = min, e = L[N*N-1]+1, m = (s+e)>>1;
while(s<e) {
if(bfs(min,m)) e = m;
else s = m+1;
m = (s+e)>>1;
}
if(m == L[N*N-1]+1) continue;
ans = Math.min(m-min, ans);
}
bw.write(ans + "\n");

}

static boolean bfs(int l, int r) {
int fd = 0;
if(H[sx][sy] < l || H[sx][sy] > r) return false;
Queue<int[]> Q = new LinkedList<>();
Q.offer(new int[] {sx,sy});
boolean[][] vis = new boolean[N][N];
vis[sx][sy] = true;
while(!Q.isEmpty()) {
int[] now = Q.poll();
int x = now[0], y = now[1];
if(A[x][y] == 'K') {
fd++;
if(fd == cnt) return true;
}
for(int i=0;i<8;i++) {
int xx = x+dx[i], yy = y+dy[i];
if(xx<0 || xx>=N || yy<0 || yy>=N || H[xx][yy] < l || H[xx][yy] > r || vis[xx][yy]) continue;
vis[xx][yy] = true;
Q.offer(new int[] {xx,yy});
}
}
return false;
}

}
```