Submission #1097606

#TimeUsernameProblemLanguageResultExecution timeMemory
1097606BLOBVISGODRailway (BOI17_railway)C++17
29 / 100
57 ms20172 KiB
#include "bits/stdc++.h" using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); ++i) #define all(x) x.begin(),x.end() #define sz(x) int(x.size()) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<vi> vvi; int main(){ cin.tie(NULL),cin.sync_with_stdio(false); int n,m,k; cin >> n >> m >> k; int B = __lg(n+1)+2; vector<vector<array<int,2>>> adj(n); vvi par(B+1, vi(n)); vi d(n), ord(n), cnt(n), vis(n), E(n); int cc = 0; auto dfs = [&](int at, int dep, auto&& dfs) -> void { ord[at] = cc; vis[at] = 1; d[at] = dep; for(auto [to,id] : adj[at]) if (!vis[to]){ par[0][to] = at; E[to] = id; dfs(to,dep+1,dfs); } }; rep(i,0,n-1){ int u,v; cin >> u >> v; u--,v--; adj[u].push_back({v,i+1}); adj[v].push_back({u,i+1}); } dfs(0,0,dfs); rep(i,0,B) rep(j,0,n) par[i+1][j] = par[i][par[i][j]]; auto jmp = [&](int at, int k) -> int { for(int i = 0; k; ++i, k/=2) if(k&1) at = par[i][at]; return at; }; auto lca = [&](int u, int v) -> int { if (d[u] < d[v]) swap(u,v); u = jmp(u,d[u]-d[v]); if (u==v) return u; for(int i = B; i>=0; --i) if (par[i][u] != par[i][v]) u = par[i][u], v = par[i][v]; return par[0][u]; }; while (m--){ int t; cin >> t; vi a(t); rep(i,0,t) cin >> a[i], a[i]--; sort(all(a),[&](int u, int v){ return ord[u] < ord[v]; }); int last = a[t-1]; for(auto cur : a){ cnt[cur]++; cnt[last]++; cnt[lca(cur,last)]-=2; last = cur; } } fill(all(vis),0); auto prop = [&](int at, auto&& prop) -> void { vis[at] = 1; for(auto [to,id] : adj[at]) if (!vis[to]){ prop(to,prop); cnt[at] += cnt[to]; } }; prop(0,prop); vi ans; rep(i,0,n) if (cnt[i] >= 2*k) ans.push_back(E[i]); sort(all(ans)); cout << sz(ans) << '\n'; for(auto c : ans) cout << c << ' '; cout << '\n'; }
#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...