#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int inf = 1e17,N = 3e5+1,MOD = 998244353,BL = 1000;
int n,m,threshold;
vi edges[N],tin(N),tout(N),dp(N);
int up[N][20];
int timer = 1;
void dfs(int node,int p) {
tin[node] = timer++;
up[node][0] = p;
for (int i=1;i<20;i++) up[node][i] = up[up[node][i-1]][i-1];
for (auto it : edges[node]) {
if (it == p) continue;
dfs(it,node);
}
tout[node] = timer-1;
}
bool anc(int a,int b) {
return tin[a] <= tin[b] && tin[b] <= tout[a];
}
int lca(int a,int b) {
if (anc(a,b)) return a;
if (anc(b,a)) return b;
for (int i = 19;i>=0;i--) {
if (!anc(up[a][i],b)) a = up[a][i];
}
return up[a][0];
}
void dfs2(int node,int p) {
for (auto it : edges[node]) {
if (it == p) continue;
dfs2(it,node);
dp[node]+=dp[it];
}
}
void solve() {
cin >> n >> m >> threshold;
vector<pii> edg(n);
for (int i=1;i<n;i++) {
int a,b;
cin >> a >> b;
edg[i] = {a,b};
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(1,1);
vi nds;
while (m--) {
int k;
cin >> k;
for (int j = 1;j<=k;j++) {
int x;
cin >> x;
nds.push_back(x);
}
sort(all(nds),[&](int x,int y) {return tin[x] < tin[y];});
for (int i=0;i<k;i++) nds.push_back(lca(nds[i],nds[(i+1)%k]));
sort(all(nds),[&](int x,int y) {return tin[x] < tin[y];});
stack<int> stk;
for (auto it : nds) {
while (!stk.empty() && !anc(stk.top(),it)) stk.pop();
if (!stk.empty()) {
//edge between top and this
dp[it]++;
dp[stk.top()]--;
}
stk.push(it);
}
nds.clear();
}
dfs2(1,1);
vi ans;
for (int i=1;i<=n-1;i++) {
if (anc(edg[i].ff,edg[i].ss)) swap(edg[i].ff,edg[i].ss);
if (dp[edg[i].ff] >= threshold) ans.push_back(i);
}
cout << ans.size() << '\n';
for (auto it : ans) cout << it << ' ';
cout << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | 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... |