Submission #639009

#TimeUsernameProblemLanguageResultExecution timeMemory
639009zaneyuRailway (BOI17_railway)C++14
100 / 100
180 ms42144 KiB
/*input
6 3 2
1 3
2 3
3 4
6 4
4 5
4 1 3 2 5
2 6 3
2 3 2
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET(v,x) lower_bound(ALL(v),x)-v.begin()
const int maxn=1e5+3;
const int INF=0x3f3f3f3f;
const int MOD=1e6+3;
const ld PI=acos(-1.0l);
const ld eps=1e-6;
const ll INF64=9e18+1;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f<<' '<<P.s;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
    return out;
}
int mult(int a,int b){
    return 1LL*a*b%MOD;
}
ll mypow(ll a,ll b,ll mod){
    if(b<=0) return 1;
    a%=mod;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int dep[maxn];
int dist[maxn];
int par[maxn][20];
vector<int> v[maxn];
int in[maxn];
int cur=0;
void dfs(int u,int p){
    par[u][0]=p;
    in[u]=cur++;
    for(int x:v[u]){
        if(x==p) continue;
        dep[x]=dep[u]+1;
        dist[x]+=dist[u];
        dfs(x,u);
    }
}
void sparse(int n){
    REP1(j,19){
        REP(i,n){
            if(par[i][j-1]!=-1){
                par[i][j]=par[par[i][j-1]][j-1];
            }
            else par[i][j]=-1;
        }
    }
}
int lca(int a,int b){
    if(dep[a]<dep[b]) swap(a,b);
    int d=dep[a]-dep[b];
    while(d){
        a=par[a][__lg(lowb(d))];
        d-=lowb(d);
    }
    if(a==b) return a;
    for(int j=19;j>=0;j--){
        if(par[a][j]!=par[b][j]){
            a=par[a][j],b=par[b][j];
        }
    }
    return par[a][0];
}
int get(int a,int b){
    int z=lca(a,b);
    return dist[a]+dist[b]-2*dist[z];
}
bool cmp(int a,int b){
    return in[a]<in[b];
}
int tot[maxn],deg[maxn];
map<pii,int> mp;
int k;
vector<int> ans;
void dfs2(int u,int p){
    for(int x:v[u]){
        if(x==p) continue;
        dfs2(x,u);
        tot[u]+=tot[x];
        if(tot[x]>=k) ans.pb(mp[{u,x}]+1);
    }
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,m;
    cin>>n>>m>>k;
    k*=2;
    REP(i,n-1){
        int a,b;
        cin>>a>>b;
        --a,--b;
        v[a].pb(b),v[b].pb(a);
        mp[{a,b}]=mp[{b,a}]=i;
    }
    dfs(0,-1),sparse(n);
    REP(i,m){
        int x;
        cin>>x;
        vector<int> sp;
        REP(j,x){
            int z;
            cin>>z;
            --z;
            sp.pb(z);
        }
        sort(ALL(sp),cmp);
        REP(i,sz(sp)){
            int x=sp[(i+1)%sz(sp)];
            tot[x]++,tot[sp[i]]++;
            tot[lca(x,sp[i])]-=2;
        }
    }
    dfs2(0,-1);
    //REP(i,n) cout<<i<<' '<<tot[i]<<'\n';
    cout<<sz(ans)<<'\n';
    sort(ALL(ans));
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...