제출 #599353

#제출 시각아이디문제언어결과실행 시간메모리
599353pedroslreyRailway (BOI17_railway)C++17
0 / 100
151 ms24776 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXK = 20; vector<pair<int, int>> graph[MAXN]; int dp[MAXN][MAXK]; int prof[MAXN], sub[MAXN], idx[MAXN], in[MAXN], out[MAXN]; int tempo; void dfs_lca(int u, int p) { in[u] = tempo++; for (auto [v, id]: graph[u]) { if (v == p) continue; dp[v][0] = u; for (int k = 1; k < MAXK; ++k) dp[v][k] = dp[dp[v][k-1]][k-1]; prof[v] = prof[u] + 1; dfs_lca(v, u); } out[u] = tempo; } int lca(int a, int b) { if (prof[b] > prof[a]) swap(a, b); int d = prof[a] - prof[b]; for (int k = 0; k < MAXK; ++k) if (d & (1 << k)) a = dp[a][k]; if (a == b) return a; for (int k = MAXK - 1; k >= 0; --k) if (dp[a][k] != dp[b][k]) { a = dp[a][k]; b = dp[b][k]; } return dp[a][0]; } void dfs_sub(int u, int p) { for (auto [v, id]: graph[u]) if (v != p) { idx[v] = id; dfs_sub(v, u); sub[u] += sub[v]; } } int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n-1; ++i) { int u, v; cin >> u >> v; graph[u].emplace_back(v, i); graph[v].emplace_back(u, i); } dfs_lca(1, 1); for (int i = 0; i < m; ++i) { int s; cin >> s; vector<int> xs(s); for (int &x: xs) cin >> x; sort(xs.begin(), xs.end(), [&](int a, int b) { return in[a] < in[b]; }); vector<int> lcas; for (int i = 0; i < s-1; ++i) lcas.push_back(lca(xs[i], xs[i + 1])); sort(lcas.begin(), lcas.end(), [&](int a, int b) { return prof[a] > prof[b]; }); set<int> atual; for (int x: xs) atual.insert(x); for (int u: lcas) { auto beg = atual.lower_bound(in[u]); auto ed = atual.upper_bound(out[u]); vector<int> tmp; while (beg != ed) { ++sub[*beg]; --sub[u]; tmp.push_back(*beg); ++beg; } for (int v: tmp) atual.erase(v); atual.insert(u); } } dfs_sub(1, 1); vector<int> ans; for (int i = 1; i <= n; ++i) if (sub[i] >= k) ans.push_back(idx[i]); sort(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (int x: ans) cout << x << " "; 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...