This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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){
    //cout << curl << " " << curr << endl;
    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){
    //cout << a << " " << b << endl;
    
    int subLCA = LCA(reach[b], reach[neighbourhoods[bIndex + 1]]);
    while(leftReach[subLCA] < leftReach[abLCA]){
        int nextIndex = unite(neighbourhoods, b, neighbourhoods[bIndex + 1], bIndex + 1, subLCA);
        b = subLCA;
        bIndex = nextIndex;
        subLCA = LCA(reach[b], reach[neighbourhoods[bIndex + 1]]);
    }
    nodeValues[a]++;
    nodeValues[b]++;
    nodeValues[abLCA] -= 2;
    return bIndex;
}
void addNumbers(vector<int> & neighbourhoods){
    sort(neighbourhoods.begin(), neighbourhoods.end(), cmp);
    //for (int x : neighbourhoods) cout << x << " ";
    //cout << endl;
    int curLCA = neighbourhoods[0];
    for (int i = 1; i < neighbourhoods.size(); i++){
        int subLCA = LCA(curLCA, neighbourhoods[i]);
        //cout << subLCA << endl;
        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);
    // for (int x : order) cout << x << " ";
    // cout << endl;
    // for (int x : firstReach) cout << x << " ";
    // cout << endl;
    // for (int x : reach) cout << x << " ";
    // cout << endl;
    // for (int x : reachToPos) cout << x << " ";
    // cout << endl;
    segmentTree.assign(order.size() * 4 + 1, 0);
    buildSegmentTree(0, 0, order.size() - 1);
    // for (int x : segmentTree) cout << x << " ";
    // cout << endl;
    // cout << LCA(1, 4) << endl;
    // for (int x : leftReach) cout << x << " ";
    // cout << endl;
    for (int i = 0; i < m; i++){
        int cnt;
        cin >> cnt;
        vector<int> neighbourhoods(cnt);
        for (int & x : neighbourhoods) cin >> x, x--;
        addNumbers(neighbourhoods);
        // for (int x : nodeValues) cout << x << " ";
        // cout << endl;
        // cout << endl;
    }
    sumDfs(0, -1);
    cout << solution.size() << endl;
    for (int x : solution) cout << x + 1 << " ";
    cout << endl;
}
Compilation message (stderr)
railway.cpp: In function 'void addNumbers(std::vector<int>&)':
railway.cpp:97:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for (int i = 1; i < neighbourhoods.size(); i++){
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |