Submission #1126742

#TimeUsernameProblemLanguageResultExecution timeMemory
1126742doducanhRailway (BOI17_railway)C++20
100 / 100
73 ms23268 KiB
///roadtoVOI2025 ///enjoythejourney #include<bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define ii pair<int,int> #define mp make_pair #define in(x) freopen(x,"r",stdin) #define out(x) freopen(x,"w",stdout) #define bit(x,i) ((x>>i)&1) #define lc (id<<1) #define rc ((id<<1)^1) const int maxn=1e5+7; vector<ii>adj[maxn]; int p[25][maxn]; int in[maxn],out[maxn]; int h[maxn]; int cnt=0; int n,m,k; int t[maxn]; int dinh[maxn]; void dfs(int u,int par) { in[u]=++cnt; for(auto [v,id]:adj[u])if(v!=par){ p[0][v]=u; dinh[v]=id; h[v]=h[u]+1; dfs(v,u); } out[u]=cnt; } int lca(int u,int v) { if(h[u]<h[v])swap(u,v); int diff=h[u]-h[v]; for(int i=20;i>=0;i--)if(bit(diff,i))u=p[i][u]; if(u==v)return u; for(int i=20;i>=0;i--)if(p[i][u]!=p[i][v]){ u=p[i][u]; v=p[i][v]; } return p[0][u]; } void up(int x,int val) { for(;x<maxn;x+=(x&(-x)))t[x]+=val; } int get(int x) { int res=0; if(x<=0)return 0; for(;x;x-=(x&(-x)))res+=t[x]; return res; } void sol() { cin>>n>>m>>k; for(int i=1;i<n;i++){ int u,v; cin>>u>>v; adj[u].push_back({v,i}); adj[v].push_back({u,i}); } dfs(1,0); for(int i=1;i<=20;i++){ for(int j=1;j<=n;j++){ p[i][j]=p[i-1][p[i-1][j]]; } } for(int i=1;i<=m;i++){ int s; cin>>s; vector<int>v; for(int j=1;j<=s;j++){ int x; cin>>x; v.push_back(x); } sort(v.begin(),v.end(),[&](const int &a,const int &b){ return in[a]<in[b]; }); v.push_back(v[0]); for(int i=0;i<v.size()-1;i++){ int l=lca(v[i],v[i+1]); up(in[v[i]],1); up(in[v[i+1]],1); up(in[l],-2); } } vector<int>ans; for(int i=1;i<=n;i++){ if(get(out[i])-get(in[i]-1)>=2*k)ans.push_back(dinh[i]); } sort(ans.begin(),ans.end()); cout<<ans.size()<<"\n"; for(int x:ans){ cout<<x<<" "; } } signed main(void) { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t=1; while(t--){ sol(); } // you should actually read the stuff at the bottom return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#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...