Submission #464482

#TimeUsernameProblemLanguageResultExecution timeMemory
464482lordlorincRailway (BOI17_railway)C++17
100 / 100
226 ms22464 KiB
#include <bits/stdc++.h>

using namespace std;

int n, m, k, curTime = 0, leftTime = 0;
vector<vector<pair<int, int> > > graph;

vector<int> reach, segmentTree, order, reachToPos, firstReach, leftReach, nodeValues, solution;

void dfs(int pos, int curParent){
    reach[pos] = curTime++;
    reachToPos[reach[pos]] = pos;
    order.push_back(reach[pos]);
    firstReach[pos] = order.size() - 1;

    for (pair<int, int> x : graph[pos]){
        if (x.first == curParent) continue;

        dfs(x.first, pos);
        order.push_back(reach[pos]);
    }

    leftReach[pos] = leftTime++;
}

void buildSegmentTree(int pos, int l, int r){
    if (l == r) {
        segmentTree[pos] = order[l];
        return;
    }

    int mid = (l + r) / 2;
    buildSegmentTree(pos * 2 + 1, l, mid);
    buildSegmentTree(pos * 2 + 2, mid + 1, r);

    segmentTree[pos] = min(segmentTree[pos * 2 + 1], segmentTree[pos * 2 + 2]);
}

int querySegmentTree(int pos, int curl, int curr, int tarl, int tarr){

    if (curl >= tarl && curr <= tarr){
        return segmentTree[pos];
    }

    int mid = (curl + curr) / 2, response = INT_MAX;

    if (mid >= tarl) {
        response = min(response, querySegmentTree(pos * 2 + 1, curl, mid, tarl, tarr));
    }
    if (mid + 1 <= tarr){
        response = min(response, querySegmentTree(pos * 2 + 2, mid + 1, curr, tarl, tarr));
    }

    return response;
}

int LCA(int a, int b){
    if (firstReach[a] > firstReach[b]) swap(a, b);
    return reachToPos[querySegmentTree(0, 0, order.size() - 1, firstReach[a], firstReach[b])];
}

bool cmp (int a, int b){
    return leftReach[a] < leftReach[b];
}

int unite(vector<int> & neighbourhoods, int a, int b, int bIndex, int abLCA){

    if (bIndex + 1 < neighbourhoods.size()){
        int subLCA = LCA(b, neighbourhoods[bIndex + 1]);

        while(bIndex + 1 < neighbourhoods.size() && leftReach[subLCA] < leftReach[abLCA]){
            int nextIndex = unite(neighbourhoods, b, neighbourhoods[bIndex + 1], bIndex + 1, subLCA);
            b = subLCA;
            bIndex = nextIndex;
            subLCA = LCA(b, neighbourhoods[bIndex + 1]);
        }
    }

    nodeValues[a]++;
    nodeValues[b]++;
    nodeValues[abLCA] -= 2;

    return bIndex;
}

void addNumbers(vector<int> & neighbourhoods){

    sort(neighbourhoods.begin(), neighbourhoods.end(), cmp);

    int curLCA = neighbourhoods[0];

    for (int i = 1; i < neighbourhoods.size(); i++){
        int subLCA = LCA(curLCA, neighbourhoods[i]);

        i = unite(neighbourhoods, curLCA, neighbourhoods[i], i, subLCA);
        curLCA = subLCA;
    }
}

int sumDfs(int pos, int parent){
    int sum = nodeValues[pos];
    for (pair<int, int> x : graph[pos]){
        if (x.first == parent) continue;

        int subSum = sumDfs(x.first, pos);

        if (subSum >= k) solution.push_back(x.second);

        sum += subSum;
    }

    return sum;
}

int main(){
    cin >> n >> m >> k;

    graph.resize(n);
    reach.resize(n);
    reachToPos.resize(n);
    firstReach.resize(n);
    leftReach.resize(n);
    nodeValues.assign(n, 0);
    for (int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;

        graph[a].push_back(make_pair(b, i));
        graph[b].push_back(make_pair(a, i));
    }

    dfs(0, -1);

    segmentTree.assign(order.size() * 4 + 1, 0);
    buildSegmentTree(0, 0, order.size() - 1);

    for (int i = 0; i < m; i++){
        int cnt;
        cin >> cnt;

        vector<int> neighbourhoods(cnt);
        for (int & x : neighbourhoods) cin >> x, x--;

        addNumbers(neighbourhoods);
    }

    sumDfs(0, -1);

    cout << solution.size() << endl;

    sort(solution.begin(), solution.end());

    for (int x : solution) cout << x + 1 << " ";
    cout << endl;
}

Compilation message (stderr)

railway.cpp: In function 'int unite(std::vector<int>&, int, int, int, int)':
railway.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     if (bIndex + 1 < neighbourhoods.size()){
      |         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:71:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         while(bIndex + 1 < neighbourhoods.size() && leftReach[subLCA] < leftReach[abLCA]){
      |               ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway.cpp: In function 'void addNumbers(std::vector<int>&)':
railway.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for (int i = 1; i < neighbourhoods.size(); i++){
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...