QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#414647 | #33. Cat in a tree | x-camp | 0 | 2ms | 10716kb | C++17 | 1.2kb | 2024-05-19 13:31:37 | 2024-05-19 13:31:37 |
Judging History
answer
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
#include <cstring>
#include <queue>
#define MX 200002
using namespace std;
vector<int> adj[MX];
int N,D;
int dep[MX];
set<pair<int,int>> nodes;
set<pair<int,int>>::iterator ptr[MX];
bool visited[MX];
void dfs(int u, int par, int depth) {
dep[u] = depth;
auto it = nodes.insert({depth,u}).first;
ptr[u] = it;
for (auto v: adj[u]) {
if (v != par) {
dfs(v, u, depth+1);
}
}
}
void dfs_maxD(int u, int par, int depth) {
if (depth >= D ) {
return;
}
visited[u] = true;
for (auto v: adj[u]) {
if (v != par && !visited[v]) {
dfs_maxD(v, u, depth+1);
}
}
nodes.erase(ptr[u]);
}
int main() {
cin >> N >> D;
for (int i=1; i<N; i++) {
int a;
cin >> a;
adj[i].push_back(a);
adj[a].push_back(i);
}
dfs(0,-1, 0);
int ans = 0;
while (!nodes.empty()) {
auto &[d, node] = *nodes.rbegin();
dfs_maxD(node, -1, 0);
ans++;
}
cout << ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 0ms
memory: 10704kb
input:
10 2 0 0 2 2 0 3 4 7 2
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 2ms
memory: 10716kb
input:
10 3 0 0 2 1 4 4 3 1 7
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 10632kb
input:
12 1 0 0 1 3 1 0 2 1 3 2 2
output:
12
result:
ok single line: '12'
Test #4:
score: -11
Wrong Answer
time: 2ms
memory: 10640kb
input:
12 4 0 0 1 2 0 0 4 7 7 9 10
output:
4
result:
wrong answer 1st lines differ - expected: '3', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%