Skip to content

Commit 6104f47

Browse files
authored
[20250806] BOJ / P3 / 사탕 배달 / 권혁준
1 parent 1a63db6 commit 6104f47

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, M;
57+
static int[] c;
58+
static List<Integer>[] graph;
59+
static int[] dep;
60+
static int[][] par, bit;
61+
static int[] king;
62+
63+
public static void main(String[] args) throws Exception {
64+
65+
io = new IOController();
66+
67+
init();
68+
solve();
69+
70+
io.close();
71+
72+
}
73+
74+
static void init() throws Exception {
75+
76+
N = io.nextInt();
77+
c = new int[N+1];
78+
graph = new List[N+1];
79+
king = new int[6];
80+
for(int i=1;i<=N;i++) {
81+
graph[i] = new ArrayList<>();
82+
c[i] = io.nextInt();
83+
king[c[i]] = i;
84+
}
85+
86+
for(int i=1;i<N;i++) {
87+
int a = io.nextInt();
88+
int b = io.nextInt();
89+
graph[a].add(b);
90+
graph[b].add(a);
91+
}
92+
93+
dep = new int[N+1];
94+
par = new int[N+1][17];
95+
bit = new int[N+1][17];
96+
97+
}
98+
99+
static void solve() throws Exception {
100+
101+
dfs(1,0,0);
102+
103+
for(int k=1;k<17;k++) for(int i=1;i<=N;i++) {
104+
par[i][k] = par[par[i][k-1]][k-1];
105+
bit[i][k] = bit[i][k-1] | bit[par[i][k-1]][k-1];
106+
}
107+
108+
M = io.nextInt()-1;
109+
int prev = io.nextInt();
110+
int temp = io.nextInt();
111+
io.write(king[temp] == 0 ? "CRY\n" : "PLAY\n");
112+
113+
while(M-->0) {
114+
int cur = io.nextInt();
115+
int tar = io.nextInt();
116+
117+
int a = prev, b = cur;
118+
prev = cur;
119+
120+
int mask = 0;
121+
int diff = Math.abs(dep[a] - dep[b]);
122+
for(int k=0;k<17;k++) if((diff & (1<<k)) != 0) {
123+
if(dep[a] > dep[b]) {
124+
mask |= bit[a][k];
125+
a = par[a][k];
126+
}
127+
else {
128+
mask |= bit[b][k];
129+
b = par[b][k];
130+
}
131+
}
132+
133+
for(int k=16;k>=0;k--) if(par[a][k] != par[b][k]) {
134+
mask |= bit[a][k] | bit[b][k];
135+
a = par[a][k];
136+
b = par[b][k];
137+
}
138+
if(a != b) {
139+
mask |= bit[a][0] | bit[b][0];
140+
a = par[a][0];
141+
b = par[b][0];
142+
}
143+
mask |= (1<<c[a]);
144+
145+
if((mask & (1<<tar)) != 0) io.write("PLAY\n");
146+
else io.write("CRY\n");
147+
148+
}
149+
150+
}
151+
152+
static void dfs(int n, int p, int d) {
153+
dep[n] = d;
154+
par[n][0] = p;
155+
bit[n][0] = (1<<c[n]);
156+
for(int i:graph[n]) if(i != p) dfs(i,n,d+1);
157+
}
158+
159+
}
160+
```

0 commit comments

Comments
 (0)