QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415079#33. Cat in a treex-campCompile Error//C++173.6kb2024-05-20 09:15:352024-05-20 09:15:36

Judging History

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

  • [2024-05-20 09:15:36]
  • 评测
  • [2024-05-20 09:15:35]
  • 提交

answer

#include <iostream>
#include <vector>
#include <cstring>
#include <set>
/*
 centroid decomposition to solve Cat in Tree problem
 */

using namespace std;
#define INF (1e9)
const int MX = 200001;  // Adjust the maximum number of nodes as needed

vector<int> adj[MX];
bool visited[MX];
int subtreeSize[MX];
int min_dist[MX];
vector<vector<pair<int, int>>> ancestors;
int N, D;
int dfs_depth;
vector<pair<int,int>> nodes;
int ans;

int dfs(int node, int parent, int depth = 1) {
    subtreeSize[node] = 1;
    nodes.push_back({-depth,node});
    for (int child : adj[node]) {
        if (child != parent && !visited[child]) {
            subtreeSize[node] += dfs(child, node, depth+1);
        }
    }
    return subtreeSize[node];
}

int findCentroid(int node, int parent, int treeSize) {
    for (int child : adj[node]) {
        if (child != parent && !visited[child]) {
            if (subtreeSize[child] *2 >= treeSize)
                return findCentroid(child, node, treeSize);
        }
    }
    return node;
}

void get_dists(int node, int centroid, int parent = -1, int cur_dist = 1) {
    for (int child : adj[node]) {
        if (child == parent || visited[child]) { continue; }
      //  cur_dist++;
        get_dists(child, centroid, node, cur_dist+1);
      //  cur_dist--;
    }
    ancestors[node].push_back({centroid, cur_dist});
}
int cnt = 0;
void decompose(int centroid) {
    cnt ++;
    for (int child : adj[centroid]) {
        if (visited[child]) { continue; }
        get_dists(child, centroid, centroid);
    }
    
    // Mark the centroid and its subtree as visited
    visited[centroid] = true;
    for (int child : adj[centroid]) {
        if (!visited[child]) {
            // Recursively decompose the subtrees
            int subtreeSize = dfs(child, centroid,1);
            int childCentroid = findCentroid(child, centroid, subtreeSize);
            if (subtreeSize > 1)
                decompose(childCentroid);
        }
    }
}

int centroidDecomposition(int root) {
    int treeSize = dfs(root, -1);
    int centroid = findCentroid(root, -1, treeSize);
    
    // Process the centroid and its subtree
    decompose(centroid);
    return centroid;
}

void paintRed (int node) {
    for (auto &[ancestor, dist] : ancestors[node]) {
        if (dist < min_dist[ancestor])
            min_dist[ancestor]  = dist;
    }
    min_dist[node] = 0;
}


/* min distance from a node to any red note*/
int query(int node) {
    int ans = min_dist[node];
    for (auto &[ancestor, dist]  : ancestors[node]) {
        if (!dist) { continue; }
        /*
         * The distance between `node` and a red painted node is the sum of
         * the distance from `node` to one of its ancestors (`dist`) and the
         * distance from this ancestor to the nearest red node
         * (`min_dist[ancestor]`).
         */
        ans = min(ans, dist + min_dist[ancestor]);
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> D;
    for (int i = 1; i <= N - 1; ++i) {
        int u;
        cin >> u;
        adj[i].push_back(u);
        adj[u].push_back(i);
    }
    memset(min_dist, 0x3f, sizeof min_dist);
    ancestors.assign(N+1, vector<pair<int, int>>());
    centroidDecomposition(1);
    sort(nodes.begin(), nodes.end());
    for (int i=0; i<nodes.size(); i++) {
        auto next = nodes[i];
        if (query(next.second) >= D) {
            paintRed(next.second);
            ans++;
        }
    }
    cout << ans ;
    return 0;
}



Details

answer.code: In function ‘int main()’:
answer.code:121:5: error: ‘sort’ was not declared in this scope; did you mean ‘short’?
  121 |     sort(nodes.begin(), nodes.end());
      |     ^~~~
      |     short