# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1174756 | Tien_Noob | Cat in a tree (BOI17_catinatree) | C++20 | 67 ms | 139584 KiB |
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
using namespace std;
const int N = 2e5;
vector<int> adj[N + 1];
int n, d;
deque<int> dp[N + 1];
void read()
{
cin >> n >> d;
for (int i = 1; i < n; ++ i)
{
int u;
cin >> u;
adj[u].push_back(i);
}
}
void DFS(int u)
{
dp[u].push_back(1); //dp[u][0] = 1
for (int v : adj[u])
{
DFS(v);
dp[v].push_front(dp[v].front());
if (dp[u].size() < dp[v].size())
{
swap(dp[u], dp[v]);
}
for (int j = 0; j < dp[v].size(); ++ j)
{
int k = d - j;
int t1 = dp[u][j] + (k >= dp[v].size() ? 0 : dp[v][max(0, k)]);
int t2 = dp[v][j] + (k >= dp[u].size() ? 0 : dp[u][max(0, k)]);
dp[u][j] = max({dp[u][j], t1, t2});
}
for (int j = (int)dp[v].size() - 2; j >= 0; -- j)
{
dp[u][j] = max(dp[u][j], dp[u][j + 1]);
}
}
}
void solve()
{
DFS(0);
cout << dp[0][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |