Submission #719380

#TimeUsernameProblemLanguageResultExecution timeMemory
719380n0sk1llRailway (BOI17_railway)C++14
100 / 100
182 ms29632 KiB
#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0) #define mp make_pair #define xx first #define yy second #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define all(x) x.begin(),x.end() #define ff(i,a,b) for (int i = a; i < b; i++) #define fff(i,a,b) for (int i = a; i <= b; i++) #define bff(i,a,b) for (int i = b-1; i >= a; i--) #define bfff(i,a,b) for (int i = b; i >= a; i--) using namespace std; long double typedef ld; unsigned int typedef ui; long long int typedef li; pair<int,int> typedef pii; pair<li,li> typedef pli; pair<ld,ld> typedef pld; vector<vector<int>> typedef graph; unsigned long long int typedef ull; //const int mod = 998244353; const int mod = 1000000007; //Note to self: Check for overflow graph g(100005); int up[18][100005],add[18][100005]; int tin[100005],tout[100005],vcvor[100005]; int VREME=0; void dfs(int p, int q) { up[0][p]=q,tin[p]=++VREME,vcvor[VREME]=p; for (auto it : g[p]) if (it!=q) dfs(it,p); tout[p]=VREME; } void build(int n) { ff(k,1,18) fff(i,1,n) up[k][i]=up[k-1][up[k-1][i]]; } bool anc(int a, int b) { return tin[a]<=tin[b] && tout[a]>=tout[b]; } int Lca(int a, int b) { bff(k,0,18) if (!anc(up[k][a],b)) a=up[k][a]; if (!anc(a,b)) a=up[0][a]; return a; } void append(int a, int b) //b MORA biti parent od a - ovo znaci da razmatramo sve cvorove od a nagore do b. { bff(k,0,18) if (!anc(up[k][a],b)) add[k][a]++,a=up[k][a]; if (!anc(a,b)) add[0][a]++; } void razmotri(int a, int b) { //cout<<a<<" "<<b<<": "<<Lca(a,b)<<endl; int c=Lca(a,b); append(a,c),append(b,c); } void deliftuj(int n) { bff(k,0,17) fff(i,1,n) add[k][i]+=add[k+1][i],add[k][up[k][i]]+=add[k+1][i]; } vector<pii> eds; int main() { FAST; int n,m,request; cin>>n>>m>>request; ff(i,1,n) {int u,v; cin>>u>>v; g[u].pb(v),g[v].pb(u); eds.pb({u,v});} dfs(1,0),tout[0]=VREME,build(n); while (m--) { int s; cin>>s; vector<pii> v(s); ff(i,0,s) cin>>v[i].yy,v[i].xx=tin[v[i].yy]; sort(all(v)); ff(i,1,s) razmotri(v[i-1].yy,v[i].yy); razmotri(v[s-1].yy,v[0].yy); } /*ff(k,0,18) { cout<<k<<": "; fff(i,1,n) cout<<add[k][i]<<" "; cout<<endl; } cout<<endl;*/ deliftuj(n); vector<int> ans; int INDEXLOL=0; for (auto it : eds) { ++INDEXLOL; if (anc(it.xx,it.yy)) swap(it.xx,it.yy); if (add[0][it.xx]>=2*request) ans.pb(INDEXLOL); } cout<<(int)ans.size()<<"\n"; for (auto it : ans) cout<<it<<" "; /*while (true) { int a,b; cin>>a>>b; cout<<"lca: "<<Lca(a,b)<<endl; cout<<"parent("<<a<<","<<b<<") = "<<anc(a,b)<<endl; }*/ } //Note to self: Check for overflow
#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...