Skip to content

Commit e9febbc

Browse files
authored
[20251108] BOJ / P5 / 나는 뱀파이어 / 권혁준
1 parent d8ac572 commit e9febbc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N, M, R;
6+
vector<vector<int>> v(100001);
7+
8+
pair<int, int> f(int n, int p, int t) {
9+
int d = t, c = 0, l = 0;
10+
for(int i:v[n]) if(i != p) {
11+
auto [dd, cc] = f(i, n, t);
12+
d = min(d, dd+1);
13+
c += cc;
14+
l++;
15+
}
16+
if(!l) return {0,1};
17+
if(d == t && n != R) c++, d = 0;
18+
return {d,c};
19+
}
20+
21+
int main() {
22+
cin.tie(0)->sync_with_stdio(0);
23+
24+
cin>>N>>M>>R;
25+
for(int i=1,a,b;i<N;i++) {
26+
cin>>a>>b;
27+
v[a].push_back(b);
28+
v[b].push_back(a);
29+
}
30+
31+
int s = 1, e = N, m = (s+e)>>1;
32+
while(s<e) {
33+
if(f(R, 0, m).second > M) s = m+1;
34+
else e = m;
35+
m = (s+e)>>1;
36+
}
37+
cout<<(m == N ? -1 : m);
38+
39+
}
40+
```

0 commit comments

Comments
 (0)