Skip to content

Commit c0414d9

Browse files
authored
Merge pull request #1747 from AlgorithmWithGod/khj20006
[20251228] BOJ / P5 / 인경호의 나무 / 권혁준
2 parents 940948a + 5385703 commit c0414d9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
using ll = long long;
5+
6+
const ll MOD = 1e9 + 7;
7+
8+
int N, M;
9+
int a[1001]{}, l[1001]{}, r[1001]{};
10+
ll c[2001][1001]{};
11+
12+
vector<int> res;
13+
14+
void inorder(int n) {
15+
if(l[n] != -1) inorder(l[n]);
16+
res.push_back(a[n]);
17+
if(r[n] != -1) inorder(r[n]);
18+
}
19+
20+
int main() {
21+
cin.tie(0)->sync_with_stdio(0);
22+
23+
for(int k=1;k<=2000;k++) {
24+
c[k][1] = k;
25+
for(int i=2;i<=min(k,1000);i++) c[k][i] = (c[k-1][i-1] + c[k-1][i]) % MOD;
26+
}
27+
28+
cin>>N>>M;
29+
for(int i=1;i<=N;i++) cin>>a[i]>>l[i]>>r[i];
30+
31+
inorder(1);
32+
res.push_back(M+1);
33+
ll ans = 1, prev = 0;
34+
for(int i=0;i<res.size();i++) {
35+
if(res[i] == -1) {
36+
int cnt = 0;
37+
while(i<res.size() && res[i] == -1) i++, cnt++;
38+
if(res[i] <= prev) return cout<<0,0;
39+
if(cnt > res[i] - prev - 1) return cout<<0,0;
40+
ans = (ans * c[res[i]-prev-1][cnt]) % MOD;
41+
}
42+
prev = res[i];
43+
}
44+
cout<<ans;
45+
46+
}
47+
```

0 commit comments

Comments
 (0)