# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
974506 | KasymK | Cat in a tree (BOI17_catinatree) | C++17 | 1031 ms | 3788 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 5;
vector<int> adj[N], vis;
int n, d;
int mx = INT_MIN;
int marked[N], dis[N], see[N];
int in_yakyn(int x){
for (int i = 0; i < n; ++i)
see[i] = dis[i] = 0;
queue<int> q;
q.push(x);
see[x] = 1;
while(!q.empty()){
int id = q.front();
q.pop();
for (int i : adj[id])
if (!see[i]){
dis[i] = dis[id] + 1;
see[i] = 1;
if(vis[i])
return dis[i];
q.push(i);
}
}
}
bool barla(){
int cnt = count(vis.begin(), vis.end(), 1);
if(cnt <= 1)
return 1;
for (int i = 0; i < n; ++i)
if(vis[i] and in_yakyn(i) < d)
return 0;
return 1;
}
void f(int x){
if(x == n){
int cnt = count(vis.begin(), vis.end(), 1);
if(barla())
mx = max(mx, cnt);
return;
}
for(int i = 0; i <= 1; ++i){
vis[x] = i;
f(x + 1);
}
}
int main() {
scanf("%d%d", &n, &d);
for(int i = 1; i <= n - 1; ++i) {
int a;
scanf("%d", &a);
adj[a].push_back(i);
adj[i].push_back(a);
}
vis.resize(n);
f(0);
printf("%d\n", mx);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |