QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414647#33. Cat in a treex-camp0 2ms10716kbC++171.2kb2024-05-19 13:31:372024-05-19 13:31:37

Judging History

你现在查看的是最新测评结果

  • [2024-05-19 13:31:37]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:10716kb
  • [2024-05-19 13:31:37]
  • 提交

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%