# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115233 | PieArmy | Railway (BOI17_railway) | C++17 | 70 ms | 22468 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
int n,m,k;
int tim=0;
int dep[100001],par[100001][17],val[100001],loc[100001];
vector<pair<int,int>>komsu[100001];
vector<int>ans;
void dfs1(int pos){
for(int i=1;i<17;i++){
par[pos][i]=par[par[pos][i-1]][i-1];
}
for(auto x:komsu[pos]){
if(par[pos][0]==x.fr)continue;
dep[x.fr]=dep[pos]+1;
par[x.fr][0]=pos;
dfs1(x.fr);
}
loc[pos]=tim++;
}
void dfs2(int pos,int ed){
for(auto x:komsu[pos]){
if(par[pos][0]==x.fr)continue;
dfs2(x.fr,x.sc);
val[pos]+=val[x.fr];
}
if(val[pos]>=k)ans.pb(ed);
}
int lca(int a,int b){
if(dep[a]<dep[b])swap(a,b);
for(int i=0;i<17;i++){
if((dep[a]-dep[b])&pie(i)){
a=par[a][i];
}
}
if(a!=b){
for(int i=17-1;i>=0;i--){
if(par[a][i]!=par[b][i]){
a=par[a][i];
b=par[b][i];
}
}
a=par[a][0];
}
return a;
}
void code(){
cin>>n>>m>>k;
for(int i=1;i<n;i++){
int x,y;cin>>x>>y;
komsu[x].pb({y,i});
komsu[y].pb({x,i});
}
dfs1(1);
for(int i=1;i<=m;i++){
int s;cin>>s;
vector<int>v;
for(int j=0;j<s;j++){
int x;cin>>x;
v.pb(x);
}
sort(v.begin(),v.end(),[&](const int &x,const int &y){
return loc[x]<loc[y];
});
v.pb(v[0]);
for(int j=0;j<s;j++){
val[v[j]]++;
val[lca(v[j],v[j+1])]--;
}
}
dfs2(1,0);
cout<<ans.size()<<endl;
for(int x:ans)
cout<<x<<" ";
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();cout<<endl;}
return 0;
}
Compilation message (stderr)
# | 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... |