QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414745#33. Cat in a treex-camp0 2ms7196kbC++171.7kb2024-05-19 16:21:472024-05-19 16:21:47

Judging History

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

  • [2024-05-19 16:21:47]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:7196kb
  • [2024-05-19 16:21:47]
  • 提交

answer

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
#include <cstring>
#include <queue>
#define MX 100002
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 removed[MX];
int parent[MX];
bool visited[MX];
bool leaf[MX];
void dfs(int u, int par, int depth) {
    parent[u] = par;
    dep[u] = depth;
    if (adj[u].size() == 1) leaf[u] = true;
    auto it = nodes.insert({depth,u}).first;
    ptr[u] = it;
    for (auto v: adj[u]) {
        if (v != par) {
            dfs(v, u, depth+1);
        }
    }
}
bool dfs_maxD(int u, int par, int depth, bool mark) {
    if (depth >= D ) {
        return leaf[u];
    }
    bool isleaf = true;
    for (auto v: adj[u]) {
        if (v != par && !visited[v]) {
            isleaf = isleaf && dfs_maxD(v, u, depth+1, mark);
        }
    }
    if (!removed[u]) {
        nodes.erase(ptr[u]);
        removed[u] = true;
    }
    if (mark || leaf[u]) //mark the subtree as visited
        visited[u] = true;
    return isleaf;
}


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();
        int cur = node;
        for (int i=1; i<=(D-1)/2; i++) {
            if (parent[cur] != -1)
                cur = parent[cur];
            else
                break;
        }
        dfs_maxD(cur, parent[cur], 0, true); //take care of subtree
        dfs_maxD(cur, -1, (D-1)/2, false);
        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: 2ms
memory: 7132kb

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: 0ms
memory: 7088kb

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: 7196kb

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: 0ms
memory: 7132kb

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%