Submission #945769

#TimeUsernameProblemLanguageResultExecution timeMemory
945769noyancanturkRailway (BOI17_railway)C++17
100 / 100
82 ms18788 KiB
#ifndef Local #pragma GCC optimize("O3,unroll-loops") const int lim=1e5+100; #else const int lim=1e3+3; #endif const int lglim=18; #include "bits/stdc++.h" using namespace std; //#define int int64_t #define pb push_back const int mod=998244353; //const int mod=(1ll<<61)-1; using pii=pair<int,int>; int n,m,k; int tin[lim],tout[lim],dep[lim],tt=0; int lift[lglim][lim]; vector<pii>v[lim]; int pare[lim]; inline void tour(int node,int par){ tin[node]=tt++; dep[node]=dep[par]-1; lift[0][node]=par; int i=1; while(i<lglim&&(lift[i][node]=lift[i-1][lift[i-1][node]]))i++; for(auto [j,c]:v[node]){ if(j^par){ pare[j]=c; tour(j,node); } } tout[node]=tt-1; } inline int lca(int x,int y){ if(dep[y]<dep[x])swap(x,y); if(dep[x]<dep[y]){ for(int i=lglim-1;0<=i;i--){ if(dep[x]+(1<<i)<=dep[y]){ x=lift[i][x]; } if(dep[x]==dep[y])break; } } if(x==y)return x; for(int i=lglim-1;0<=i;i--){ if(lift[i][x]^lift[i][y]){ x=lift[i][x]; y=lift[i][y]; } } return lift[0][x]; } int val[lim]; bool can[lim]; vector<int>ans; inline int dfs(int node,int par){ for(auto [j,c]:v[node]){ if(j^par){ val[node]+=dfs(j,node); } } if(2*k<=val[node]){ can[pare[node]]=1; } return val[node]; } inline void solve(){ cin>>n>>m>>k; for(int i=1;i<=n-1;i++){ int x,y; cin>>x>>y; v[x].pb({y,i}); v[y].pb({x,i}); } tour(1,0); int inds[n+1]; for(int r=0;r<m;r++){ int s; cin>>s; for(int i=0;i<s;i++){ cin>>inds[i]; } sort(inds,inds+s,[&](int i,int j) -> bool { return tin[i]<tin[j]; }); inds[s]=inds[0]; for(int i=0;i<s;i++){ val[inds[i]]++; val[inds[i+1]]++; val[lca(inds[i],inds[i+1])]-=2; } } dfs(1,0); int cnt=0; for(int i=0;i<n;i++){ if(can[i])cnt++; } cout<<cnt<<"\n"; for(int i=0;i<n;i++)if(can[i])cout<<i<<" "; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); #ifdef Local freopen(".in","r",stdin); freopen(".out","w",stdout); #else #endif int t=1; //cin>>t; while (t--) { solve(); } }
#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...