이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define debug(x) cout << #x << " = " << x << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2", "unroll-loops", "Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define N 500005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
int n, d;
int h[N], p[22][N];
int del[N], par[N], sz[N];
int MinDist[N];
vector<int> g[N];
void dfs(int u){
for(int v:g[u]) if(v != p[0][u]){
h[v] = h[u] + 1;
p[0][v] = u;
FOR(i, 1, 20) p[i][v] = p[i-1][p[i-1][v]];
dfs(v);
}
}
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for(int v:g[u]) if(v != p && !del[v]){
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int n){
for(int v:g[u]) if(v != p && !del[v] && sz[v] > n/2) return centroid(v, u, n);
return u;
}
void cd(int u, int p = -1){
dfs_sz(u);
u = centroid(u, p, sz[u]);
del[u] = 1;
par[u] = p;
for(int v:g[u]) if(!del[v]) cd(v, u);
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
FORD(i, 20, 0) if(k>>i&1) u = p[i][u];
if(u == v) return u;
FORD(i, 20, 0) if(p[i][u] != p[i][v]) u = p[i][u], v = p[i][v];
return p[0][u];
}
int dist(int u, int v){
return h[u] + h[v] - 2 * h[lca(u, v)];
}
void upd(int u){
for(int v = u; v > 0; v = par[v]) minimize(MinDist[v], dist(u, v));
}
int get(int u){
int Ans = INF;
for(int v = u; v > 0; v = par[v]) minimize(Ans, MinDist[v] + dist(u, v));
return Ans;
}
void solve(){
cin >> n >> d;
FOR(i, 1, n-1){
int u = i, v; cin >> v; u++; v++;
g[u].pb(v);
g[v].pb(u);
}
dfs(1);
cd(1);
// tham lay tu la len truoc -> xa goc hon -> phan bo duoc nhieu hon
vector<pii> candidates;
FOR(i, 1, n) candidates.pb({-h[i], i});
sort(all(candidates));
memset(MinDist, 0x3f, sizeof MinDist);
int Ans = 0;
for(auto [depth, u]:candidates){
if(get(u) >= d){
Ans++;
upd(u);
}
}
cout << Ans;
}
int main(){
#define NAME "TASK"
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
fastIO;
if(Kadoc){
int tc; cin >> tc;
while(tc--){
solve();
}
} else solve();
}
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp: In function 'void solve()':
catinatree.cpp:133:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
133 | for(auto [depth, u]:candidates){
| ^
catinatree.cpp: In function 'int main()':
catinatree.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(NAME".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... |