제출 #850203

#제출 시각아이디문제언어결과실행 시간메모리
850203ThMinh_Cat in a tree (BOI17_catinatree)C++14
100 / 100
60 ms24236 KiB
#include<bits/stdc++.h> #define forin(i, a, b) for(int i = a; i <= b; ++i) using namespace std; const int N = 2e5 + 10; int n, d; int g[N], f[N], cnt[N]; vector<int> adj[N]; void dfs(int u = 1, int p = 0) { f[u] = 1e9; for(auto v : adj[u]) if(v != p) { dfs(v, u); cnt[u] += cnt[v]; f[u] = min(f[u], f[v] + 1); if(g[v]) { if(g[v] * 2 >= d) { f[u] = min(f[u], g[v] + 1); cnt[u]++; } else g[u] = max(g[u], g[v] + 1); } } if(g[u] + f[u] - 2 < d) g[u] = 0; if(!g[u] && f[u] - 1 >= d) g[u] = 1; } int main () { cin.tie(0)->sync_with_stdio(0); if(fopen("Task.inp","r")) { freopen("Task.inp","r",stdin); freopen("WA.out","w",stdout); } if(fopen("Durian.inp","r")) { freopen("Durian.inp","r",stdin); freopen("Durian.out","w",stdout); } cin>>n>>d; forin(i, 1, n - 1) { int x; cin>>x; adj[i + 1].push_back(x + 1); adj[x + 1].push_back(i + 1); } // forin(i, 1, n - 1) { // int u, v; cin>>u>>v; // adj[u].push_back(v); // adj[v].push_back(u); // } dfs(); cout<<cnt[1] + bool (g[1]); }

컴파일 시 표준 에러 (stderr) 메시지

catinatree.cpp: In function 'int main()':
catinatree.cpp:28:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |       freopen("Task.inp","r",stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:29:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |       freopen("WA.out","w",stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:32:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |       freopen("Durian.inp","r",stdin);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:33:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |       freopen("Durian.out","w",stdout);
      |       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...