이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* [Author: Nguyen Ngoc Hung] - From THPT Ngo Gia Tu with Love */
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
#include <chrono>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) (((i)>>(j))&1LL)
#define MASK(i) (1LL<<(i))
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define fi first
#define se second
#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define REPDIS(i,be,en,j) for(int i = (be); i<=(en); i+=j)
#define REPD(i,be,en) for(int i = (be); i>=(en); i--)
#define REP(i,be,en) for(int i = (be); i<=(en); i++)
#define endl "\n"
#define MP make_pair
// #define int ll
//-----------------------------------------------------------------------------------------------//
int readInt(){
char c;
do{ c = getchar(); }while(c!='-' && !isdigit(c));
bool neg = (c=='-');
int res = neg?0:c-'0';
while(isdigit(c=getchar())) res = (res<<3)+(res<<1)+(c-'0');
return neg?-res:res;
}
//------------------------------------------------------------------------------------------------//
const ll LINF = 1e18;
const int INF = 1e9;
const int LOG = 20;
const int MAXN = 2e5+7;
const int N = 1e2+3;
const int MOD = 1e9+7;
const int BASE = 1e5;
const ld EPS = 1e-9;
const ld PI = acos(-1);
const int OFFSET = 1e3;
//------------------------------------------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
//------------------------------------------------------------------------------------------------//
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
vi adj[MAXN],dp[MAXN];
int n,d;
namespace sub1{
int h[MAXN],tmp[MAXN];
void dfs(int u, int par){
dp[u].push_back(1);
for(int &v:adj[u]){
if(v==par) continue;
dfs(v,u);
maxi(h[u],h[v]+1);
dp[u].resize(min(d+1,h[u]+1));
REP(i,0,SZ(dp[u])-1){
tmp[i] = dp[u][i];
}
REP(j,0,SZ(dp[v])-1){
maxi(tmp[j+1],dp[v][j]);
}
REP(i,0,SZ(dp[u])-1){
REP(j,max(0,d-i-1),SZ(dp[v])-1){
int MIN = min(i,j+1);
maxi(tmp[MIN],dp[u][i]+dp[v][j]);
}
}
REP(i,0,SZ(dp[u])-1) dp[u][i] = tmp[i];
}
}
void solve(){
dfs(1,0);
int res = 0;
for(int &i:dp[1]) maxi(res,i);
cout << res;
}
}
namespace sub2{
int child[MAXN],tmp[MAXN];
void dfs(int u, int par){
dp[u].resize(2);
dp[u][1] = 0; child[u] = 1;
for(int &v:adj[u]){
if(v==par) continue;
dfs(v,u);
int siz = max(2,min(n/d+1,child[u]+1));
dp[u].resize(siz,-INF);
REP(i,0,SZ(dp[u])-1) tmp[i] = dp[u][i];
REP(i,0,SZ(dp[v])-1){
if(dp[v][i]==-INF) break;
maxi(tmp[i],dp[v][i]+1);
}
REP(i,1,SZ(dp[u])-1){
if(dp[u][i]==-INF) break;
REP(j,1,SZ(dp[v])-1){
if(dp[v][j]==-INF) break;
if(dp[u][i]+dp[v][j]+1<d) continue;
maxi(tmp[i+j],min(dp[u][i],dp[v][j]+1));
}
}
REP(i,0,SZ(dp[u])-1) dp[u][i] = tmp[i];
}
}
void solve(){
dfs(1,0);
REPD(i,SZ(dp[1])-1,1){
if(dp[1][i]!=-INF) continue;
cout << i;
return ;
}
}
}
void solve(){
cin >> n >> d;
REP(u,2,n){
int v; cin >> v; ++v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(n<=20 || d<=n/d){
sub1::solve();
return ;
}
sub2::solve();
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
#define task1 "nnhzzz"
if(fopen(task1".inp","r")){
freopen(task1".inp","r",stdin);
freopen(task1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
/** /\_/\
* (= ._.)
* / >TL \>AC
**/
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp: In function 'int main()':
catinatree.cpp:195:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:196:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
196 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
200 | freopen(task1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
201 | freopen(task1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |