diff --git "a/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.md" "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.md" new file mode 100644 index 00000000..4c52c50b --- /dev/null +++ "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.md" @@ -0,0 +1,80 @@ +```java + +import java.util.*; +import java.io.*; + +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; + static int[] A, B; + static int[] X; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + N = nextInt(); + A = new int[2*N-1]; + B = new int[N]; + X = new int[N]; + int[] ta = new int[N]; + int[] tb = new int[N]; + for(int i=0;i0 && B[X[i]] != B[i]) X[i] = X[X[i]-1]; + if(B[X[i]] == B[i]) X[i]++; + } + + for(int i=0,j=0;i0 && B[j] != A[i]) j = X[j-1]; + if(B[j] == A[i]) j++; + if(j == N) { + bw.write("possible"); + return; + } + } + bw.write("impossible"); + + } + +} + +``` diff --git "a/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.md" "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.md" new file mode 100644 index 00000000..a982e9e8 --- /dev/null +++ "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.md" @@ -0,0 +1,106 @@ +```java + +import java.util.*; +import java.io.*; + + +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; + static List E; + static int[] r; + static int[][] V; + + static final int INF = 123456789; + + 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(); + E = new ArrayList<>(); + r = new int[N]; + V = new int[N][N]; + for(int i=0;ii) E.add(new int[] {i,j,a}); + } + } + + } + + static void solve() throws Exception{ + + int ans = 0, cnt = 0; + Collections.sort(E, (a,b) -> a[2]-b[2]); + + for(int[] e:E) { + int a = e[0], b = e[1], c = e[2]; + int x = f(a), y = f(b); + if(x != y) { + V[a][b] = V[b][a] = c; + r[x] = y; + ans += c; + cnt++; + continue; + } + + int dist = shortestPath(a, b); + if(c < dist) { + V[a][b] = V[b][a] = c; + ans += c; + } + if(c > dist) { + bw.write("-1"); + return; + } + } + + if(cnt < N-1) bw.write("-1"); + else bw.write(ans + "\n"); + + } + + static int shortestPath(int start, int end) { + int[][] D = new int[N][N]; + for(int i=0;i D[j][i] + D[i][k]) D[j][k] = D[j][i] + D[i][k]; + + return D[start][end]; + } + +} + +``` diff --git "a/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.md" "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.md" new file mode 100644 index 00000000..696e37e3 --- /dev/null +++ "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.md" @@ -0,0 +1,75 @@ +```java + +import java.util.*; +import java.io.*; + +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 long N; + static int M; + static int[] A; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + N = nextInt(); + M = nextInt(); + A = new int[M]; + for(int i=0;i>1; + while(s>1; + } + + if(m == 0) { + bw.write(N + "\n"); + return; + } + N -= M; + for(int i=0;i[] V; + static final int INF = (int)2e9; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + 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=0;i Q = new PriorityQueue<>((a,b) -> a[0]-b[0]); + Q.offer(new int[]{0,1}); + + while(!Q.isEmpty()){ + int[] now = Q.poll(); + int d = now[0], n = now[1]; + if(d > DA[n]) continue; + for(int[] next:V[n]){ + int i = next[0], c = next[1]; + if(DA[i] > d+c){ + DA[i] = d+c; + Q.offer(new int[]{DA[i],i}); + } + } + } + + int[][] DB = new int[N+1][2]; + for(int i=1;i<=N;i++) Arrays.fill(DB[i], INF); + DB[1][0] = 0; + Q = new PriorityQueue<>((a,b) -> a[0]/2 + a[1]*2 - b[0]/2 - b[1]*2); + Q.offer(new int[]{0,0,1,0}); + while(!Q.isEmpty()){ + int[] now = Q.poll(); + int a = now[0], b = now[1], n = now[2], k = now[3]; + if(a/2 + b*2 > DB[n][k]) continue; + for(int[] next:V[n]){ + int i = next[0], c = next[1]; + int res = a/2 + b*2; + if(k == 0){ + res += c/2; + if(DB[i][k^1] > res){ + DB[i][k^1] = res; + Q.offer(new int[]{a+c,b,i,k^1}); + } + } + else{ + res += c*2; + if(DB[i][k^1] > res){ + DB[i][k^1] = res; + Q.offer(new int[]{a,b+c,i,k^1}); + } + } + } + } + + int ans = 0; + for(int i=2;i<=N;i++){ + int a = DA[i], b = Math.min(DB[i][0], DB[i][1]); + if(a < b) ans++; + } + bw.write(ans + "\n"); + + } + +} +``` \ No newline at end of file diff --git "a/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-16940] BFS \354\212\244\355\216\230\354\205\234 \354\240\200\354\247\200.md" "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-16940] BFS \354\212\244\355\216\230\354\205\234 \354\240\200\354\247\200.md" new file mode 100644 index 00000000..6dfb4729 --- /dev/null +++ "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-16940] BFS \354\212\244\355\216\230\354\205\234 \354\240\200\354\247\200.md" @@ -0,0 +1,81 @@ +```java + +import java.util.*; +import java.io.*; + +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; + static Set[] V; + static int[] R; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + N = nextInt(); + V = new TreeSet[N+1]; + for(int i=1;i<=N;i++) V[i] = new TreeSet<>(); + for(int i=1;i Q = new LinkedList<>(); + boolean[] vis = new boolean[N+1]; + Q.offer(1); + vis[1] = true; + int idx = 2; + if(R[1] != 1) { + bw.write("0"); + return; + } + while(!Q.isEmpty()) { + int n = Q.poll(); + for(int i=0;i N) bw.write("1"); + else bw.write("0"); + + } + +} + +``` diff --git "a/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.md" "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.md" new file mode 100644 index 00000000..8a44f9be --- /dev/null +++ "b/\352\266\214\355\230\201\354\244\200_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.md" @@ -0,0 +1,202 @@ +```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 INF = 1234567; + static final int[] dx = {0,-1,-1,-1,0,1,1,1}; + static final int[] dy = {-1,-1,0,1,1,1,0,-1}; + static final int[] dShark = {2,0,6,4}; + + // shark dir priority : up - left - down - right + + static int M, S; + static List[][] grid; + static int[][] smell; + static int sx, sy; + + static List[][] copyList; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + M = nextInt(); + S = nextInt(); + grid = new List[4][4]; + smell = new int[4][4]; + for(int i=0;i<4;i++) for(int j=0;j<4;j++) { + grid[i][j] = new ArrayList<>(); + smell[i][j] = INF; + } + + for(int i=0;i0) { + + copyStart(); + moveFish(); + moveShark(); + removeSmell(); + copyEnd(); + + timeIncrease(); + + } + + int cnt = 0; + for(int i=0;i<4;i++) for(int j=0;j<4;j++) cnt += grid[i][j].size(); + bw.write(cnt + "\n"); + + } + + static void copyStart() { + copyList = new List[4][4]; + for(int i=0;i<4;i++) for(int j=0;j<4;j++) { + copyList[i][j] = new ArrayList<>(); + for(int d:grid[i][j]) copyList[i][j].add(d); + } + } + + static void moveFish() { + List[][] resultGrid = new List[4][4]; + for(int i=0;i<4;i++) for(int j=0;j<4;j++) resultGrid[i][j] = new ArrayList<>(); + + for(int i=0;i<4;i++) for(int j=0;j<4;j++) for(int d:grid[i][j]) { + boolean canMove = false; + for(int dir=d,k=0;k++<8;dir = (dir+7)%8) { + int x = i+dx[dir], y = j+dy[dir]; + if(OOB(x,y)) continue; + if(smell[x][y] != INF) continue; + if(x==sx && y==sy) continue; + canMove = true; + resultGrid[x][y].add(dir); + break; + } + if(!canMove) resultGrid[i][j].add(d); + } + + grid = resultGrid; + } + + static void moveShark() { + int[][] vis = new int[4][4]; + + int cnt = 0, max = -1; + int resX = -1, resY = -1; + int resXX = -1, resYY = -1; + int resXXX = -1, resYYY = -1; + for(int a=0;a<4;a++) { + int x = sx+dx[dShark[a]], y = sy+dy[dShark[a]]; + if(OOB(x,y)) continue; + + cnt += vis[x][y]>0 ? 0 : grid[x][y].size(); + vis[x][y]++; + + for(int b=0;b<4;b++) { + int xx = x+dx[dShark[b]], yy = y+dy[dShark[b]]; + if(OOB(xx,yy)) continue; + + cnt += vis[xx][yy]>0 ? 0 : grid[xx][yy].size(); + vis[xx][yy]++; + + for(int c=0;c<4;c++) { + int xxx = xx+dx[dShark[c]], yyy = yy+dy[dShark[c]]; + if(OOB(xxx,yyy)) continue; + + cnt += vis[xxx][yyy]>0 ? 0 : grid[xxx][yyy].size(); + + if(cnt > max) { + max = cnt; + resX = x; + resY = y; + resXX = xx; + resYY = yy; + resXXX = xxx; + resYYY = yyy; + } + + cnt -= vis[xxx][yyy]>0 ? 0 : grid[xxx][yyy].size(); + + } + + vis[xx][yy]--; + cnt -= vis[xx][yy]>0 ? 0 : grid[xx][yy].size(); + + } + + vis[x][y]--; + cnt -= vis[x][y]>0 ? 0 : grid[x][y].size(); + } + + if(!grid[resX][resY].isEmpty()) { + grid[resX][resY] = new ArrayList<>(); + smell[resX][resY] = 2; + } + if(!grid[resXX][resYY].isEmpty()) { + grid[resXX][resYY] = new ArrayList<>(); + smell[resXX][resYY] = 2; + } + if(!grid[resXXX][resYYY].isEmpty()) { + grid[resXXX][resYYY] = new ArrayList<>(); + smell[resXXX][resYYY] = 2; + } + + sx = resXXX; + sy = resYYY; + } + + static void removeSmell() { + for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(smell[i][j] == 0) smell[i][j] = INF; + } + + static void copyEnd() { + for(int i=0;i<4;i++) for(int j=0;j<4;j++) for(int d:copyList[i][j]) grid[i][j].add(d); + } + + static void timeIncrease() { + for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(smell[i][j] != INF) smell[i][j]--; + } + + static boolean OOB(int x, int y) { + return x<0 || x>=4 || y<0 || y>=4; + } + +} + +```