제출 #914005

#제출 시각아이디문제언어결과실행 시간메모리
914005VMaksimoski008Railway (BOI17_railway)C++14
100 / 100
85 ms24604 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const short LOG = 17; const int maxn = 1e5 + 5; struct BIT { int n; vector<int> tree, val; void config(int _n) { n = _n + 10; tree.resize(_n+60); val.resize(_n+60); } void add(int p, int v) { val[p] += v; for(p++; p<n; p+=p&-p) tree[p] += v; } int sum(int p) { int ans = 0; for(p++; p>0; p-=p&-p) ans += tree[p]; return ans; } int query(int l, int r) { return sum(r) - sum(l-1); } void set(int p, int v) { add(p, v - val[p]); } }; int n, m, k, timer = 0, i, j; int depth[maxn+1], up[maxn+1][LOG], in[maxn+1], out[maxn+1]; vector<vector<int> > graph; void dfs(int u, int p) { in[u] = timer++; for(i=1; i<LOG; i++) up[u][i] = up[ up[u][i-1] ][i-1]; for(int &v : graph[u]) { if(v == p) continue; depth[v] = depth[u] + 1; up[v][0] = u; dfs(v, u); } out[u] = timer; } int jmp(int x, int d) { for(j=LOG-1; j>=0; j--) if(d & (1 << j)) x = up[x][j]; return x; } int get_lca(int a, int b) { if(depth[a] < depth[b]) swap(a, b); a = jmp(a, depth[a] - depth[b]); if(a == b) return a; for(j=LOG-1; j>=0; j--) if(up[a][j] != up[b][j]) a = up[a][j], b = up[b][j]; return up[a][0]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; graph.resize(n+1); vector<pii> edges; for(i=0; i<n-1; i++) { int a, b; cin >> a >> b; edges.push_back({ a, b }); graph[a].push_back(b); graph[b].push_back(a); } dfs(1, 0); int edge_id[n+1]; for(i=0; i<n-1; i++) { if(depth[edges[i].first] > depth[edges[i].second]) swap(edges[i].first, edges[i].second); edge_id[edges[i].second] = i+1; } BIT bit; bit.config(n); int cnt; int v[n+1]; while(m--) { cin >> cnt; for(i=0; i<cnt; i++) cin >> v[i]; sort(v, v + cnt, [&](int &a, int &b) { return in[a] < in[b]; }); v[cnt] = v[0]; int a, b, lca; for(i=0; i<cnt; i++) { a = v[i], b = v[i+1], lca = get_lca(a, b); bit.add(in[a], 1); bit.add(in[b], 1); bit.add(in[lca], -2); } } vector<int> ans; for(i=1; i<=n; i++) if(bit.query(in[i], out[i]-1) >= 2 * k) ans.push_back(edge_id[i]); cout << ans.size() << '\n'; sort(ans.begin(), ans.end()); for(int &x : ans) cout << x << " "; return 0; }
#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...