이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (ll)(1000000007)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
const int nx[8] = {0, 0, 1, -1,1,1,-1,-1}, ny[8] = {1, -1, 0, 0,1,-1,1,-1}; //East, West, South, North+
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}
////////////******SOLUTION******\\\\\\\\\\\
const int MAX_N = 1504;
int n, d;
int par[MAX_N];
vi adj[MAX_N];
int depth[MAX_N];
int dist[MAX_N][MAX_N];
void f(int i, int d)
{
depth[i] = d;
for(auto e: adj[i])
{
if(e == par[i]) continue;
f(e,d+1);
}
}
void solve()
{
cin >> n >> d;
for(int i = 1; i < n; i ++)
{
cin >> par[i];
adj[i].pb(par[i]);
adj[par[i]].pb(i);
}
f(0,0);
int maxt = 0;
int ans = 1;
map<int,vi> m;
vi inc;
for(int i = 0; i <n; i++)
{
m[depth[i]].pb(i);
maxt = max(maxt,depth[i]);
}
for(int i =0; i <n; i ++)
{
queue<pii> q;
q.push({i,0});
bitset<MAX_N> bs;
while(!q.empty())
{
pii u = q.front();
q.pop();
bs[u.ff] = 1;
dist[i][u.ff] = dist[u.ff][i] = u.ss;
for(auto e: adj[u.ff])
{
if(bs[e]) continue;
q.push({e,u.ss+1});
}
}
}
for(int i = maxt; i >= 0; i --)
{
for(auto u: m[i])
{
bool flag = true;
for(auto e: inc)
if(dist[u][e] < d)
flag = false;
if(flag)
inc.pb(u);
}
}
cout << inc.size();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tt =1;
while(tt--)
{
solve();
}
}
/*
*/
컴파일 시 표준 에러 (stderr) 메시지
catinatree.cpp:24:1: warning: multi-line comment [-Wcomment]
24 | ////////////******SOLUTION******\\\\\\\\\\\
| ^
catinatree.cpp: In function 'void solve()':
catinatree.cpp:53:9: warning: unused variable 'ans' [-Wunused-variable]
53 | int ans = 1;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |