From f618a0c040eae08ded3b33a23df912109f7f70d0 Mon Sep 17 00:00:00 2001 From: oncsr Date: Wed, 13 Aug 2025 23:31:00 +0900 Subject: [PATCH] =?UTF-8?q?[20250813]=20BOJ=20/=20D5=20/=20Cat=20Exercise?= =?UTF-8?q?=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- khj20006/202508/13 BOJ D5 Cat Exercise.md | 144 ++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 khj20006/202508/13 BOJ D5 Cat Exercise.md diff --git a/khj20006/202508/13 BOJ D5 Cat Exercise.md b/khj20006/202508/13 BOJ D5 Cat Exercise.md new file mode 100644 index 00000000..1da02e9d --- /dev/null +++ b/khj20006/202508/13 BOJ D5 Cat Exercise.md @@ -0,0 +1,144 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static int N; + static int[] p; + static long[] s; + static int[] r; + static int[][] par; + static int[] dep; + static List[] graph; + static List edges; + + static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));} + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + static void init() throws Exception { + + N = io.nextInt(); + p = new int[N+1]; + s = new long[N+1]; + r = new int[N+1]; + par = new int[N+1][18]; + graph = new List[N+1]; + dep = new int[N+1]; + for(int i=1;i<=N;i++) { + p[i] = io.nextInt(); + r[i] = i; + graph[i] = new ArrayList<>(); + } + edges = new ArrayList<>(); + for(int i=1;i a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]); + for(int[] edge : edges) { + int a = edge[0], b = edge[1]; + int x = f(a), y = f(b); + s[y] = Math.max(s[y], s[x] + dist(x, y)); + r[x] = y; + } + io.write(s[N] + "\n"); + + } + + static void dfs(int n, int p, int d) { + par[n][0] = p; + dep[n] = d; + for(int i:graph[n]) if(i != p) dfs(i,n,d+1); + } + + static int dist(int a, int b) { + int diff = Math.abs(dep[a]-dep[b]); + int res = 0; + for(int k=0;k<18;k++) if((diff & (1< dep[b]) a = par[a][k]; + else b = par[b][k]; + res += (1<=0;k--) if(par[a][k] != par[b][k]) { + res += (1<<(k+1)); + a = par[a][k]; + b = par[b][k]; + } + if(a != b) res += 2; + return res; + } + +} +```