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;
#define endl '\n'
#define ll long long
const ll sz=1e5+5;
ll p[sz], lvl[sz], dp[sz][100], in[sz], ln, n, m, k, t, c[sz];
vector<ll>g[sz];
set<ll>s;
map<pair<ll, ll>, ll>m1;
/*
{} []
*/
void dfs1(ll u, ll par)
{
in[u]=++t;
lvl[u]=lvl[par]+1;
dp[u][0]=par;
for(int i=1; i<ln; i++){
dp[u][i]=dp[dp[u][i-1]][i-1];
}
for(auto v:g[u]){
if(v!=par){
p[v]=u;
dfs1(v, u);
}
}
}
ll lca(ll u, ll v)
{
if(lvl[u]<lvl[v]) swap(u, v);
ll dif=lvl[u]-lvl[v];
for(int i=0; i<ln; i++){
if(dif&(1<<i)){
u=dp[u][i];
}
}
if(u==v) return u;
for(int i=ln-1; i>=0; i--){
if(dp[u][i]!=dp[v][i]){
u=dp[u][i];
v=dp[v][i];
}
}
return dp[u][0];
}
void dfs2(ll u, ll par)
{
for(auto v:g[u]){
if(v!=par){
dfs2(v, u);
c[u]+=c[v];
}
}
if(c[u]>=2*k && u!=1){
s.insert(m1[{u, par}]);
}
}
bool cmp(ll u, ll v)
{
return in[u]<in[v];
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
ln=(ceil)(log2(n));
for(int i=1; i<n; i++){
ll a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
m1[{a, b}]=i;
m1[{b, a}]=i;
}
dfs1(1, 0);
for(int i=1; i<=m; i++){
ll num;
cin>>num;
ll k[num+2];
for(int j=1; j<=num; j++){
cin>>k[j];
}
sort(k+1, k+num+1, cmp);
k[num+1]=k[1];
set<pair<ll, ll>>cur;
for(int j=2; j<=num+1; j++){
ll x=k[j-1], y=k[j];
ll l=lca(x, y);
c[x]++; c[y]++; c[l]-=2;
}
}
dfs2(1, 0);
cout<<s.size()<<endl;
for(auto i:s) cout<<i<<' ';
}
/*
20 13 2
1 2
2 3
2 15
2 17
3 4
4 5
5 6
6 7
6 11
6 18
7 8
8 9
8 12
8 19
9 10
11 13
11 16
12 14
19 20
4 7 17 19 20
4 4 11 12 13
4 4 8 15 19
4 8 10 15 17
4 1 12 18 19
4 1 2 7 12
4 2 12 14 15
4 12 14 15 17
4 2 12 14 15
4 9 11 19 20
4 6 7 8 12
4 5 6 14 18
4 6 7 8 11
*/
# | 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... |