Submission #1004308

#TimeUsernameProblemLanguageResultExecution timeMemory
1004308ayankarimovaRailway (BOI17_railway)C++14
23 / 100
1067 ms115540 KiB
#include <bits/stdc++.h> using namespace std; //#define endl '\n' #define ll long long const ll sz=1e5+5; ll p[sz], lvl[sz], dp[sz][100], ln; vector<ll>g[sz]; set<ll>s; map<pair<ll, ll> , ll>mp; map<pair<ll, ll>, ll>m1; /* {} [] */ void dfs(ll u, ll par) { lvl[u]=lvl[par]+1; dp[u][0]=par; for(int i=1; i<ln; i++){ dp[u][i]=dp[dp[u][i-1]][i-1]; } for(auto v:g[u]){ if(v!=par){ p[v]=u; dfs(v, u); } } } ll lca(ll u, ll v) { if(lvl[u]<lvl[v]) swap(u, v); ll dif=lvl[u]-lvl[v]; for(int i=0; i<ln; i++){ if(dif&(1<<i)){ u=dp[u][i]; } } if(u==v) return u; for(int i=ln-1; i>=0; i--){ if(dp[u][i]!=dp[v][i]){ u=dp[u][i]; v=dp[v][i]; } } return dp[u][0]; } int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll n, m, k; cin>>n>>m>>k; ln=(ceil)(log2(n)); for(int i=1; i<n; i++){ ll a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); m1[{a, b}]=i; m1[{b, a}]=i; } dfs(1, 0); for(int i=1; i<=m; i++){ ll num; cin>>num; ll k[num+1]; for(int j=1; j<=num; j++){ cin>>k[j]; } set<pair<ll, ll>>cur; for(int j=2; j<=num; j++){ ll x=k[j-1], y=k[j]; ll l=lca(x, y); //cout<<x<<' '<<y<<' '<<l<<endl; while(y!=l){ ll a=y, b=p[y]; ll c=a, d=b; //cout<<a<<' '<<b<<endl; if(c>d) swap(c, d); cur.insert({c, d}); y=b; } while(x!=l){ ll a=x, b=p[x]; ll c=a, d=b; if(c>d) swap(c, d); cur.insert({c, d}); x=b; } } for(auto i:cur){ mp[{i.first, i.second}]++; } } for(auto i:mp){ if(i.second>=k){ s.insert(m1[{i.first.first, i.first.second}]); } } cout<<s.size()<<endl; for(auto i:s) cout<<i<<' '; } /* {} [] 6 1 2 1 3 2 3 3 4 6 4 4 5 2 3 2 */
#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...