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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
vector<pair<int,int>>adj[100005];
int d[100005], p[100005][20], tin[100005], tout[100005], dp[100005], pos[100005], x[100005];
int timer = 0, n = 0, m = 0, k = 0;
void dfs(int s, int e) {
	tin[s] = ++timer;
	for (int i = 1; i <= 18; i++) {
		p[s][i] = p[p[s][i - 1]][i - 1];
	}
	for (auto u : adj[s]) {
		if (u.first == e) continue;
		d[u.first] = d[s] + 1;
		p[u.first][0] = s;
		pos[u.first] = u.second;
		dfs(u.first, s);
	}
	tout[s] = timer;
}
int lca(int a, int b) {
	if (d[a] > d[b]) swap(a, b);
	int dis = d[b] - d[a];
	for (int i = 0; i <= 18; i++) {
		if (dis & (1 << i)) b = p[b][i];
	}
	if (a == b) return a;
	for (int i = 18; i >= 0; i--) {
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	}
	return p[a][0];
}
bool comp(int x, int y) {
	return tin[x] < tin[y];
}
void dfs1(int s, int e) {
	for (auto u : adj[s]) {
		if (u.first == e) continue;
		dfs1(u.first, s);
		dp[s] += dp[u.first];
	}
	if (dp[s] >= 2 * k) {
		x[pos[s]] = true;
		//cout << pos[s] << "\n";
	}
}
void solve()
{
	cin >> n >> m >> k;
	for (int i = 1; i <= n - 1; i++) {
		int u, v; cin >> u >> v;
		adj[u].push_back({v, i });
		adj[v].push_back({u, i});
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		int s; cin >> s;
		vector<int>v(s);
		for (int i = 0; i < s; i++) cin >> v[i];
		sort(v.begin(), v.end(), comp);
		for (int i = 0; i < s; i++) {
			dp[v[i]]++;
			dp[v[(i + 1) % s]]++;
			dp[lca(v[i], v[(i + 1) % s])] -= 2;
		}
	}
	dfs1(1, 0);
	int ans = 0;
	for (int i = 1; i <= n - 1; i++) { if (x[i]) ++ans; }
	cout << ans << "\n";
	for (int i = 1; i <= n - 1; i++) {
		if(x[i])cout << i << " ";
	}
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;// cin>>t;
	while (t--) 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... |