Submission #599094

#TimeUsernameProblemLanguageResultExecution timeMemory
599094radalRailway (BOI17_railway)C++17
100 / 100
136 ms27284 KiB
#include <bits/stdc++.h> //#pragma GCC target("sse,sse2,avx2") #pragma GCC optimize("unroll-loops,O3") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define all(x) (x).begin() , (x).end() #define pb push_back #define endl '\n' #define debug(x) cerr << #x << " : " << x << endl; using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pll; constexpr int N = 1e5+20,mod = 1e9+7,inf = 1e9+10,sq = 500; inline int mkay(int a,int b){ if (a+b >= mod) return a+b-mod; if (a+b < 0) return a+b+mod; return a+b; } inline int poww(int a,int k){ if (k < 0) return 0; int z = 1; while (k){ if (k&1) z = 1ll*z*a%mod; a = 1ll*a*a%mod; k >>= 1; } return z; } int tin[N],T,par[N][20],h[N],calc[N],k; vector<pll> adj[N]; vector<int> ans; inline int lca(int u,int v){ if (h[u] > h[v]) swap(u,v); repr(i,19,0){ if (h[v]-h[u] >= (1 << i)) v = par[v][i]; } if (u == v) return v; repr(i,19,0){ if (par[v][i] != par[u][i]){ v = par[v][i]; u = par[u][i]; } } return par[v][0]; } void dfs2(int v,int p){ for (pll u : adj[v]){ if (u.X != p){ dfs2(u.X,v); if (calc[u.X]/2 >= k) ans.pb(u.Y); calc[v] += calc[u.X]; } } } void dfs(int v,int p){ par[v][0] = p; tin[v] = T; T++; for (pll u : adj[v]){ if (u.X != p){ h[u.X] = h[v]+1; dfs(u.X,v); } } } inline bool cmp(int i,int j){ return (tin[i] < tin[j]); } int main(){ ios :: sync_with_stdio(0); cin.tie(0); int n,m; cin >> n >> m >> k; rep(i,1,n){ int u,v; cin >> u >> v; adj[u].pb({v,i}); adj[v].pb({u,i}); } dfs(1,0); rep(j,1,20){ rep(i,2,n+1) par[i][j] = par[par[i][j-1]][j-1]; } rep(x,0,m){ int s; cin >> s; vector<int> ve; rep(j,0,s){ int v; cin >> v; ve.pb(v); } sort(all(ve),cmp); rep(i,1,s){ int u = ve[i],v = ve[i-1],w = lca(u,v); if (w == v){ calc[u]++; calc[v]--; continue; } calc[v]++; calc[u]++; calc[w] -= 2; } int u = ve[s-1],v = ve[0],w = lca(u,v); if (v == w){ calc[u]++; calc[v]--; continue; } calc[u]++; calc[v]++; calc[w] -= 2; } dfs2(1,0); sort(all(ans)); cout << ans.size() << endl; for (int i : ans) cout << i << ' '; }
#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...