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>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG=18;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int par[MAXN][LOG], h[MAXN];
int stt[MAXN], fnt[MAXN], timer=1;
int sum[MAXN];
int parid[MAXN];
vector<pii> G[MAXN];
void dfs1(int node){
stt[node]=timer++;
for (int i=1; i<LOG; i++) par[node][i]=par[par[node][i-1]][i-1];
for (pii p:G[node]) if (p.first!=par[node][0]){
par[p.first][0]=node;
parid[p.first]=p.second;
dfs1(p.first);
}
fnt[node]=timer;
}
inline bool is_par(int u, int v){
return stt[u]<=stt[v] && fnt[v]<=fnt[u];
}
int Lca(int x, int y){
if (stt[x]>stt[y]) swap(x, y);
if (fnt[y]<=fnt[x]) return x;
for (int i=LOG-1; i>=0; i--) if (!is_par(par[x][i], y)) x=par[x][i];
return par[x][0];
}
int dfs2(int node){
for (pii p:G[node]) if (p.first!=par[node][0]) sum[node]+=dfs2(p.first);
return sum[node];
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m>>k;
for (int i=1; i<n; i++){
cin>>u>>v;
G[u].pb({v, i});
G[v].pb({u, i});
}
par[1][0]=1;
dfs1(1);
while (m--){
cin>>t;
for (int i=0; i<t; i++) cin>>A[i];
sort(A, A+t, [](int i, int j){
return stt[i]<stt[j];
});
for (int i=0; i<t; i++){
int x=A[i], y=A[(i+1)%t], lca=Lca(x, y);
//cerr<<"lca : "<<x<<' '<<y<<' '<<lca<<endl;
sum[x]++;
sum[y]++;
sum[lca]-=2;
}
}
dfs2(1);
//for (int i=2; i<=n; i++) debug2(i, sum[i]/2)
vector<int> out;
for (int i=2; i<=n; i++) if (sum[i]>=2*k) out.pb(parid[i]);
sort(all(out));
cout<<out.size()<<'\n';
for (int i:out) cout<<i<<' ';
return 0;
}
/*
14 8 0
1 2
2 3
2 7
3 4
3 5
3 6
1 8
8 9
9 11
11 12
11 13
9 10
9 14
5 3 13 9 1 4
5 7 6 9 14 5
2 12 13
2 7 9
3 10 12 13
3 6 5 4
3 7 3 9
3 10 13 5
*/
# | 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... |