Submission #695175

#TimeUsernameProblemLanguageResultExecution timeMemory
695175amirhoseinfar1385Railway (BOI17_railway)C++17
100 / 100
184 ms29248 KiB
#include<bits/stdc++.h> using namespace std; int n,m,k; const int maxn=100000+5; vector<pair<int,int>>adj[maxn]; struct node{ int res; map<int,int>mp; node(){ res=0; } }; node dp[maxn]; int allv[maxn]; vector<int>mainres; void merge(int u,int v){ int fu=u,fv=v; if(dp[u].mp.size()<dp[v].mp.size()){ swap(u,v); } dp[u].res+=dp[v].res; for(auto x:dp[v].mp){ dp[u].mp[x.first]+=x.second; if(x.second!=allv[x.first]&&dp[u].mp[x.first]==allv[x.first]){ dp[u].res++; } } dp[v].mp.clear(); if(u!=fu){ swap(dp[fu],dp[u]); } } void solve(int u,int par,int w){ for(auto x:adj[u]){ if(x.first!=par){ solve(x.first,u,x.second); merge(u,x.first); } } // cout<<u<<" "<<dp[u].res<<"\n"; int q=dp[u].mp.size(); q-=dp[u].res; if(u!=1&&q>=k){ mainres.push_back(w); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; adj[u].push_back(make_pair(v,i+1)); adj[v].push_back(make_pair(u,i+1)); } for(int i=0;i<m;i++){ cin>>allv[i]; for(int j=0;j<allv[i];j++){ int d; cin>>d; dp[d].mp[i]=1; if(allv[i]==1){ dp[d].res++; } } } solve(1,0,-1); cout<<((int)mainres.size())<<"\n"; sort(mainres.begin(),mainres.end()); for(auto x:mainres){ cout<<x<<" "; } cout<<"\n"; }

Compilation message (stderr)

railway.cpp: In function 'void merge(int, int)':
railway.cpp:18:11: warning: unused variable 'fv' [-Wunused-variable]
   18 |  int fu=u,fv=v;
      |           ^~
#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...