Submission #644773

#TimeUsernameProblemLanguageResultExecution timeMemory
644773Vu_CG_CoderCat in a tree (BOI17_catinatree)C++14
51 / 100
404 ms26976 KiB
/* [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 = 2000; vector <int> a[N]; int dp[N][4500] = {0}; int t[N] = {0}; int f[1100] = {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) { memset(dp[u],0,sizeof(dp[u])); int cnt = 0; for (int v : a[u]) if (v != par) { dfs(v,u); maximize(high[u],high[v] + 1); for (int i = 0 ; i <= k ; i++) f[i] = dp[u][i]; for (int i = 0 ; i <= min(high[u],k) ; i++) { for (int j = max(i - 1,0) ; j <= min(high[v],k) ; j++) { int x = j + 1; maximize(dp[u][i],f[max(i,k - x)] + dp[v][j]); } } } maximize(dp[u][0],dp[u][k] + 1); } void solve() { dfs(0,0); int res = 0; for (int i = 0 ; i <= high[0] ; i++) maximize(res,dp[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)

catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:47:9: warning: unused variable 'cnt' [-Wunused-variable]
   47 |     int cnt = 0;
      |         ^~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:85:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     freopen(TASK".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:86:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     freopen(TASK".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...