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>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
    if (a < b) {a = b; return true;} return false;
}
 
int n;
int m;
int k;
int ed[mxN];
int dep[mxN];
int par[mxN][LOG + 1];
vector<pair<int, int>> adj[mxN];
vector<int> pos[mxN];
vector<int> root[mxN];
vector<int> lca[mxN];
set<int> s[mxN];
bool ans[mxN];
void dfs(int u, int p)
{
    for(auto it : adj[u]) {
        int v = it.fi;
        int idx = it.se;
        if(v == p) continue;
        par[v][0] = u;
        dep[v] = dep[u] + 1;
        ed[v] = idx;
        dfs(v, u);
    }
}
int getlca(int u, int v)
{
    if(dep[u] < dep[v]) swap(u, v);
    for(int j = LOG; j >= 0; j--) if(dep[par[u][j]] >= dep[v]) u = par[u][j];
    if(u == v) return u;
    for(int j = LOG; j >= 0; j--) {
        if(par[u][j] != par[v][j]) {
            u = par[u][j];
            v = par[v][j];
        }
    }
    return par[u][0];
}
void calc(int u, int p)
{
    for(auto it : pos[u]) s[u].insert(it);
    for(auto it : adj[u]) {
        int v = it.fi;
        if(v == p) continue; 
        calc(v, u); 
        if(sz(s[u]) < sz(s[v])) swap(s[u], s[v]);
        for(auto it : s[v]) s[u].insert(it);
    }
    for(auto it : root[u]) s[u].erase(s[u].find(it));
    if(p != -1 && sz(s[u]) >= k) {
        ans[ed[u]] = true;
    }
}
 
void solve()
{
    cin >> n >> m >> k;
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].pb({v, i});
        adj[v].pb({u, i});
    }
    par[1][0] = 1;
    dfs(1, -1);
    for(int j = 1; j <= LOG; j++)
    for(int i = 1; i <= n; i++) par[i][j] = par[par[i][j - 1]][j - 1];
    for(int i = 1; i <= m; i++) {
        int ss;
        cin >> ss;
        int rr = -1;
        for(int j = 1; j <= ss; j++) {
            int x;
            cin >> x;
            pos[x].pb(i);
            if(rr == -1) rr = x;
            else rr = getlca(rr, x);
        }
        root[rr].pb(i);
    }
    calc(1, -1);
    int res = 0;
    for(int i = 1; i < n; i++) res += ans[i];
    cout << res << endl;
    for(int i = 1; i < n; i++) if(ans[i] == true) cout << i << ' ';
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int tc = 1;
    //cin >> tc;
    while(tc--) {
        solve();
    }
    return 0;
}
| # | 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... |