Submission #1172434

#TimeUsernameProblemLanguageResultExecution timeMemory
1172434DanielPr8Railway (BOI17_railway)C++20
0 / 100
73 ms36148 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vll = vector<ll>; using vvl = vector<vll>; using pll = pair<ll,ll>; using vpl = vector<pll>; using vvp = vector<vpl>; #define f first #define s second #define pb push_back #define all(v) v.begin(),v.end() vll ans, sz, dt; vvp g; ll k; vvl pr; void dfs(ll cr){ for(pll i:g[cr]){ if(i.f!=pr[0][cr]){ pr[0][i.f]=cr; dt[i.f]=dt[cr]+1; dfs(i.f); } } } void calc(ll cr){ for(pll i:g[cr]){ if(i.f!=pr[0][cr]){ calc(i.f); sz[cr]+=sz[i.f]; if(sz[i.f]>=k)ans.pb(i.s); } } } ll fpr(ll a, ll b){ for(ll h = 19; h >= 0; --h){ if((1<<h) <= b){ a = pr[h][a]; b -= (1<<h); } } return a; } ll lca(ll a, ll b){ if(dt[a]>dt[b])swap(a,b); b = fpr(b, dt[b]-dt[a]); if(a==b)return a; for(ll h = 19; h >= 0; --h){ if(pr[h][a] != pr[h][b]){ a = pr[h][a]; b = pr[h][b]; } } return pr[0][a]; } int main(){ ios_base::sync_with_stdio(0);cin.tie(NULL); ll n, m, a ,b, s; cin >> n >> m >> k; g = vvp(n); for(ll i = 0; i < n-1; ++i){ cin >> a >> b; a--;b--; g[a].pb({b,i}); g[b].pb({a,i}); } pr = vvl(20, vll(n)); dt = sz = vll(n); dfs(0); for(ll h = 1; h < 20; ++h){ for(ll i = 0; i < n; ++i){ pr[h][i] = pr[h-1][pr[h-1][i]]; } } for(ll i = 0; i < m; ++i){ cin >> s; cin >> a >> b; a--;b--; ll c = lca(a, b); sz[a]++; sz[b]++; sz[c]-=2; } calc(0); cout << ans.size() << "\n"; for(ll i:ans)cout << i+1 << " "; }
#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...