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------------------------------
//******************************************************************
#define IOS ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0)
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
#define endl "\n"
//******************************************************************
//----------------------------FUNCTION------------------------------
//******************************************************************
ll gcd(ll a,ll b){
if(a>b) swap(a,b);
if(a==0) return a+b;
return gcd(b%a,a);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
bool is_prime(ll n){
ll k=sqrt(n);
if(n==2) return true;
if(n<2||n%2==0||k*k==n) return false;
for(int i=3;i<=k;i+=2){
if(n%i==0){
return false;
}
}
return true;
}
//*****************************************************************
//--------------------------MAIN-CODE------------------------------
const int mxn=1e5+5,lg=30;
int t=1,n,m,k,x,y,up[mxn][lg],tin[mxn],tout[mxn],timer,node[mxn],color[mxn],say[mxn],sum[mxn];
vector<int>vt[mxn];
vector<int>arr[mxn];
map<pll,int>mp;
set<int>ans;
void dfs(int u,int p){
tin[u]=++timer;
up[u][0]=p;
for(int i=1;i<lg;i++){
up[u][i]=up[up[u][i-1]][i-1];
}
for(int v:vt[u]){
if(v!=p){
dfs(v,u);
}
}
tout[u]=++timer;
}
bool funk(int u,int v){
return tin[u]<=tin[v]&&tout[u]>=tout[v];
}
int lca(int u,int v){
if(funk(u,v)){
return u;
}
for(int i=lg-1;i>=0;i--){
if(!funk(up[u][i],v)){
u=up[u][i];
}
}
return up[u][0];
}
void dfs_sum(int u,int p){
for(int v:vt[u]){
if(v!=p){
dfs_sum(v,u);
sum[u]+=sum[v];
}
}
for(int v:vt[u]){
if(v!=p){
if(sum[v]>=2*k){
ans.insert(mp[{u,v}]);
}
}
}
}
bool f(int u,int v){
return tin[u]<tin[v];
}
void solve(){
cin>>n>>m>>k;
for(int i=1;i<n;i++){
cin>>x>>y;
vt[x].pb(y);
vt[y].pb(x);
mp[{x,y}]=i;
mp[{y,x}]=i;
}
dfs(1,1);
for(int qq=1;qq<=m;qq++){
cin>>x;
for(int i=1;i<=x;i++){
cin>>node[i];
}
sort(node+1,node+x+1,f);
for(int i=2;i<=x;i++){
int q=lca(node[i-1],node[i]);
int u=node[i-1],v=node[i];
sum[u]++,sum[v]++;
sum[q]-=2;
}
int q=lca(node[1],node[x]);
int u=node[1],v=node[x];
sum[u]++,sum[v]++;
sum[q]-=2;
}
dfs_sum(1,-1);
cout<<ans.size()<<endl;
for(int v:ans){
cout<<v<<' ';
}
cout<<endl;
}
int main(){
IOS;
//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... |