# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
643581 | Vu_CG_Coder | Cat in a tree (BOI17_catinatree) | C++14 | 26 ms | 39396 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* [Author : Hoang Duy Vu] */
#include <bits/stdc++.h>
#define All(x) (x).begin(),(x).end()
#define ll long long
#define C make_pair
#define fi first
#define se second
#define two second.first
#define thr second.second
#define TASK "txt"
using namespace std;
template<typename T> bool maximize(T &res, const T &val) {
if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) {
if (res > val) { res = val; return true; } return false; }
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int LOG = 20;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
const int mod = 1e9 + 7;
const int N = 200100;
vector <int> a[N];
vector <int> dp[N][3];
int t[N] = {0};
int high[N] = {0};
int n , k;
void input()
{
cin >> n >> k;
for (int i = 1 ; i < n ; i++)
{
int x;
cin >> x;
a[x].push_back(i);
a[i].push_back(x);
}
}
void dfs(int u ,int par)
{
int cnt = 0;
dp[u][0].assign(10,0);
for (int v : a[u])
if (v != par)
{
dfs(v,u);
int New = (cnt + 1)&1;
maximize(high[u],high[v] + 1);
dp[u][New].assign(high[u] + 10,0);
for (int i = 0 ; i <= high[u] ; i++)
{
if (i < dp[u][cnt].size());
dp[u][New][i] = dp[u][cnt][i];
for (int j = max(i - 1,0) ; j <= high[v] ; j++)
{
int x = j + 1;
// if (u == 7) cout << u << " " << high[v] << " " << v << " " << i << " " << x << " " << k - x << " " << dp[v][t[v]][j] <<"\n";
if (max(i,k - x) < dp[u][cnt].size())
maximize(dp[u][New][i],dp[u][cnt][max(i,k - x)] + dp[v][t[v]][j]);
}
}
// if (u == 7) cout << v << " " << dp[u][New][1] << "\n";
cnt = New;
}
t[u] = cnt;
maximize(dp[u][t[u]][0],dp[u][t[u]][k] + 1);
// cout << u << ":";
// for (int i = 0 ; i <= high[u] ; i++) cout << dp[u][t[u]][i] << " ";
// cout << "\n\n";
}
void solve()
{
dfs(0,0);
int res = 0;
for (int i = 0 ; i <= high[0] ; i++) maximize(res,dp[0][t[0]][i]);
cout << res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
if(fopen(TASK".inp", "r")){
freopen(TASK".inp","r",stdin);
freopen(TASK".out","w",stdout);
}
int tk;
tk = 1;
//cin >> tk;
while (tk--)
{
input();
solve();
}
return 0;
}
Compilation message (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... |