Skip to content

Commit 5b1c352

Browse files
authored
[20250221] BOJ / P3 / 전쟁 중의 삶 / 권혁준
1 parent e6497d1 commit 5b1c352

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Node{
7+
int cnt;
8+
Node l, r;
9+
Node(){ cnt = 0; }
10+
}
11+
12+
class Trie{
13+
Node root;
14+
Trie(){ root = new Node(); }
15+
16+
void insert(long x) {
17+
18+
Stack<Integer> S = new Stack<>();
19+
while(x > 0) {
20+
if((x&1) == 0) S.add(0);
21+
else S.add(1);
22+
x >>= 1;
23+
}
24+
25+
Node now = root;
26+
while(!S.isEmpty()) {
27+
int a = S.pop();
28+
if(a == 0) {
29+
if(now.l == null) now.l = new Node();
30+
now = now.l;
31+
now.cnt++;
32+
}
33+
else {
34+
if(now.r == null) now.r = new Node();
35+
now = now.r;
36+
now.cnt++;
37+
}
38+
}
39+
}
40+
41+
int DFS(int v) {
42+
Node now = root;
43+
int ans = 1;
44+
if(now.l != null) ans += dfs(now.l, v);
45+
if(now.r != null) ans += dfs(now.r, v);
46+
return ans;
47+
}
48+
49+
int dfs(Node now, int v) {
50+
int res = now.cnt == v ? 0 : 1;
51+
if(now.l != null) res += dfs(now.l, v);
52+
if(now.r != null) res += dfs(now.r, v);
53+
return res;
54+
}
55+
}
56+
57+
class Main {
58+
59+
// IO field
60+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
61+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
62+
static StringTokenizer st;
63+
64+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
65+
static int nextInt() {return Integer.parseInt(st.nextToken());}
66+
static long nextLong() {return Long.parseLong(st.nextToken());}
67+
static void bwEnd() throws Exception {bw.flush();bw.close();}
68+
69+
// Additional field
70+
71+
static Trie trie;
72+
static int N;
73+
74+
75+
public static void main(String[] args) throws Exception {
76+
77+
ready();
78+
solve();
79+
80+
bwEnd();
81+
82+
}
83+
84+
static void ready() throws Exception{
85+
86+
trie = new Trie();
87+
88+
N = Integer.parseInt(br.readLine());
89+
nextLine();
90+
for(int i=0;i<N;i++) {
91+
long a = nextLong();
92+
trie.insert(a);
93+
}
94+
95+
}
96+
97+
static void solve() throws Exception{
98+
99+
bw.write(trie.DFS(N)+"\n");
100+
101+
}
102+
103+
}
104+
105+
```

0 commit comments

Comments
 (0)